diff --git a/src/config/cfg_421613_dev.js b/src/config/cfg_421613_dev.js index 08565f6..8430336 100644 --- a/src/config/cfg_421613_dev.js +++ b/src/config/cfg_421613_dev.js @@ -36,9 +36,17 @@ module.exports = { ], contracts: { minterFactory: "0x1A27515c35a92Fb276c2670fa27C85ffAd75D094", - nftmarket: "0x1d33b4E045ce595B6346C3D0e334814dd4Af5418", + nftmarket: "0x28cd862Baa826e65a0c0F1179B9A3423dF4d366A", nftmall: "0x38Bf9f3C29D8384B6A79435745AE796cd2465545", gamemarket: "0x46e2C612756b702b3d68d89F97c88FFa725F6fab", gamemall: "0x1D058c7c7451c34BbfF9c0dF1C16b95C5d171d64", }, + gasInfo: { + nftApprove: 49340, + cecApprove: 46962, + marketSellNFT: 270740, + marketCancelOrder: 119027, + marketUpdatePrice: 41272, + marketBuy: 207735, + }, }; diff --git a/src/config/constants.ts b/src/config/constants.ts index fe99e9c..66a39d1 100644 --- a/src/config/constants.ts +++ b/src/config/constants.ts @@ -17,3 +17,5 @@ export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; export const AVAILABLE_CHAINS = [80001, 421613, 137, 42161]; export const BASE_TOKEN_URI = "https://market.cebg.games/api/nft/info/"; + +export const GAS_BOOST = 1.2; diff --git a/src/standards/JCStandard.ts b/src/standards/JCStandard.ts index bf23f6a..5d32651 100644 --- a/src/standards/JCStandard.ts +++ b/src/standards/JCStandard.ts @@ -1,7 +1,7 @@ import assert from "assert"; import Web3 from "web3"; import { abiMinterFactory } from "../abis/abiUserMinterFactory"; -import { ZERO_ADDRESS } from "../config/constants"; +import { GAS_BOOST, ZERO_ADDRESS } from "../config/constants"; import { getAddressByName, universalChainCb } from "../util/chain.util"; import { SAMPLE_GAS } from "./ChainCommon"; import { abiMarket } from "../abis/abiMarket"; @@ -10,6 +10,7 @@ import { abiGameMarket } from "../abis/abiGameMarket"; import { abiGameMall } from "../abis/abiGameMall"; import { ZError } from "../common/ZError"; import { abiERC20 } from "../abis/abiERC20"; +import { abiERC721 } from "../abis/abiERC721"; export class JCStandard { private web3: Web3; @@ -51,7 +52,7 @@ export class JCStandard { .mintNft(address, tokenIds, startTime, saltNonce, signature) .estimateGas(); } - gas = (gas * 1.1) | 1; + gas = (gas * GAS_BOOST) | 1; if (estimate) { return jc.wallet.generateGasShow(gas); } @@ -96,20 +97,34 @@ export class JCStandard { estimate: number; }) { const cfg = jc.wallet.currentChainCfg; + estimate = parseInt(estimate + ""); const addressMarket = cfg.contracts.nftmarket; console.log("addressMarket:: ", addressMarket); const contract = new this.web3.eth.Contract(abiMarket, addressMarket, { //@ts-ignore from: jc.wallet.currentAccAddr, }); - if (!gas) { - gas = await contract.methods - .sell(nftToken, currency, tokenId, price, amount) - .estimateGas(); + const tokenInstance = new this.web3.eth.Contract(abiERC721, nftToken, { + //@ts-ignore + from: jc.wallet.currentAccAddr, + }); + let approved = await tokenInstance.methods.getApproved(tokenId); + let gasApprove = 0; + if (approved != addressMarket) { + gasApprove = cfg.gasInfo.nftApprove; + gasApprove = (gasApprove * GAS_BOOST) | 1; } - gas = (gas * 1.1) | 1; + + if (!gas) { + gas = cfg.gasInfo.marketSellNFT; + // gas = await contract.methods + // .sell(nftToken, currency, tokenId, price, amount) + // .estimateGas(); + } + gas = (gas * GAS_BOOST) | 1; + if (estimate) { - return jc.wallet.generateGasShow(gas); + return jc.wallet.generateGasShow(gas + gasApprove); } let details = [ @@ -125,6 +140,12 @@ export class JCStandard { title: "market_sell", details: details, }; + if (approved != addressMarket) { + await tokenInstance.methods + .approve(addressMarket, tokenId) + .send({ gas: gasApprove }); + console.log("approve done"); + } return universalChainCb( logData, contract.methods @@ -145,6 +166,7 @@ export class JCStandard { estimate: number; }) { const cfg = jc.wallet.currentChainCfg; + estimate = parseInt(estimate + ""); const addressMarket = cfg.contracts.nftmarket; console.log("addressMarket:: ", addressMarket); const contract = new this.web3.eth.Contract(abiMarket, addressMarket, { @@ -152,9 +174,10 @@ export class JCStandard { from: jc.wallet.currentAccAddr, }); if (!gas) { - gas = await contract.methods.updatePrice(orderId, price).estimateGas(); + // gas = await contract.methods.updatePrice(orderId, price).estimateGas(); + gas = cfg.gasInfo.marketUpdatePrice; } - gas = (gas * 1.1) | 1; + gas = (gas * GAS_BOOST) | 1; if (estimate) { return jc.wallet.generateGasShow(gas); } @@ -188,6 +211,7 @@ export class JCStandard { estimate: number; }) { const cfg = jc.wallet.currentChainCfg; + estimate = parseInt(estimate + ""); const addressMarket = cfg.contracts.nftmarket; console.log("addressMarket:: ", addressMarket); const contract = new this.web3.eth.Contract(abiMarket, addressMarket, { @@ -196,8 +220,9 @@ export class JCStandard { }); if (!gas) { gas = await contract.methods.cancelOrder(orderId).estimateGas(); + // gas = cfg.gasInfo.marketCancelOrder; } - gas = (gas * 1.1) | 1; + gas = (gas * GAS_BOOST) | 1; if (estimate) { return jc.wallet.generateGasShow(gas); } @@ -231,37 +256,45 @@ export class JCStandard { estimate: number; }) { const cfg = jc.wallet.currentChainCfg; + estimate = parseInt(estimate + ""); const addressMarket = cfg.contracts.nftmarket; console.log("addressMarket:: ", addressMarket); const contract = new this.web3.eth.Contract(abiMarket, addressMarket, { //@ts-ignore from: jc.wallet.currentAccAddr, }); - if (!gas) { - gas = await contract.methods.buy(orderId).estimateGas(); - } - gas = (gas * 1.1) | 1; - // begin of increase allowance let orderInfo = await contract.methods.orderInfos(orderId).call(); if (!orderInfo.orderId) { throw new ZError(100, `order info with id: ${orderId} not found`); } const tokenInstance = new this.web3.eth.Contract( abiERC20, - orderInfo.currency + orderInfo.currency, + { + //@ts-ignore + from: jc.wallet.currentAccAddr, + } ); - let gasApprove = await tokenInstance.methods - .approve(addressMarket, orderInfo.price) - .estimateGas(); - gasApprove = (gasApprove * 1.1) | 1; + let approved = await tokenInstance.methods + .allowance(jc.wallet.currentAccAddr, addressMarket) + .call(); + console.log("approved:: ", approved); + let gasApprove = 0; + if (approved < orderInfo.price) { + gasApprove = cfg.gasInfo.cecApprove; + } + gasApprove = (gasApprove * GAS_BOOST) | 1; + if (!gas) { + // gas = await contract.methods.buy(orderId).estimateGas(); + gas = cfg.gasInfo.marketBuy; + } + gas = (gas * GAS_BOOST) | 1; + // begin of increase allowance + if (estimate) { return jc.wallet.generateGasShow(gas + gasApprove); } - await tokenInstance.methods - .approve(addressMarket, orderInfo.price) - .send({ gas: gasApprove }); - let details = [ { address: addressMarket, @@ -275,6 +308,11 @@ export class JCStandard { title: "market_buy", details: details, }; + if (approved < orderInfo.price) { + await tokenInstance.methods + .approve(addressMarket, orderInfo.price) + .send({ gas: gasApprove }); + } return universalChainCb( logData, contract.methods.buy(orderId).send({ gas }) @@ -296,6 +334,7 @@ export class JCStandard { estimate: number; }) { const cfg = jc.wallet.currentChainCfg; + estimate = parseInt(estimate + ""); const addressMall = cfg.contracts.nftmall; console.log("addressMall:: ", addressMall); const contract = new this.web3.eth.Contract(abiMall, addressMall, { @@ -305,13 +344,13 @@ export class JCStandard { if (!gas) { gas = await contract.methods.buyNFT(addresses, values).estimateGas(); } - gas = (gas * 1.1) | 1; + gas = (gas * GAS_BOOST) | 1; const tokenInstance = new this.web3.eth.Contract(abiERC20, addresses[2]); let gasApprove = await tokenInstance.methods .approve(addressMall, values[1]) .estimateGas(); - gasApprove = (gasApprove * 1.1) | 1; + gasApprove = (gasApprove * GAS_BOOST) | 1; if (estimate) { return jc.wallet.generateGasShow(gas + gasApprove); @@ -375,6 +414,7 @@ export class JCStandard { estimate: number; }) { const cfg = jc.wallet.currentChainCfg; + estimate = parseInt(estimate + ""); const addressGameMarket = cfg.contracts.gamemarket; console.log("addressGameMarket:: ", addressGameMarket); const contract = new this.web3.eth.Contract( @@ -390,13 +430,13 @@ export class JCStandard { .buy(orderId, seller, currency, price, startTime, saltNonce, signature) .estimateGas(); } - gas = (gas * 1.1) | 1; + gas = (gas * GAS_BOOST) | 1; const tokenInstance = new this.web3.eth.Contract(abiERC20, currency); let gasApprove = await tokenInstance.methods .approve(addressGameMarket, price) .estimateGas(); - gasApprove = (gasApprove * 1.1) | 1; + gasApprove = (gasApprove * GAS_BOOST) | 1; if (estimate) { return jc.wallet.generateGasShow(gas + gasApprove); @@ -448,6 +488,7 @@ export class JCStandard { estimate: number; }) { const cfg = jc.wallet.currentChainCfg; + estimate = parseInt(estimate + ""); const addressGameMall = cfg.contracts.gamemall; console.log("addressGameMall:: ", addressGameMall); const contract = new this.web3.eth.Contract(abiGameMall, addressGameMall, { @@ -459,12 +500,12 @@ export class JCStandard { .buy(orderId, currency, price, startTime, saltNonce, signature) .estimateGas(); } - gas = (gas * 1.1) | 1; + gas = (gas * GAS_BOOST) | 1; const tokenInstance = new this.web3.eth.Contract(abiERC20, currency); let gasApprove = await tokenInstance.methods .approve(addressGameMall, price) .estimateGas(); - gasApprove = (gasApprove * 1.1) | 1; + gasApprove = (gasApprove * GAS_BOOST) | 1; if (estimate) { return jc.wallet.generateGasShow(gas + gasApprove); }