增加获取实时订单信息的方法

This commit is contained in:
CounterFire2023 2023-06-20 10:03:30 +08:00
parent 01c4789cbe
commit faa45df02c

View File

@ -1,16 +1,16 @@
import assert from "assert"; import assert from 'assert';
import Web3 from "web3"; import Web3 from 'web3';
import { abiMinterFactory } from "../abis/abiUserMinterFactory"; import { abiMinterFactory } from '../abis/abiUserMinterFactory';
import { GAS_BOOST, ZERO_ADDRESS } from "../config/constants"; import { GAS_BOOST, ZERO_ADDRESS } from '../config/constants';
import { getAddressByName, universalChainCb } from "../util/chain.util"; import { getAddressByName, universalChainCb } from '../util/chain.util';
import { SAMPLE_GAS } from "./ChainCommon"; import { SAMPLE_GAS } from './ChainCommon';
import { abiMarket } from "../abis/abiMarket"; import { abiMarket } from '../abis/abiMarket';
import { abiMall } from "../abis/abiMall"; import { abiMall } from '../abis/abiMall';
import { abiGameMarket } from "../abis/abiGameMarket"; import { abiGameMarket } from '../abis/abiGameMarket';
import { abiGameMall } from "../abis/abiGameMall"; import { abiGameMall } from '../abis/abiGameMall';
import { ZError } from "../common/ZError"; import { ZError } from '../common/ZError';
import { abiERC20 } from "../abis/abiERC20"; import { abiERC20 } from '../abis/abiERC20';
import { abiERC721 } from "../abis/abiERC721"; import { abiERC721 } from '../abis/abiERC721';
export class JCStandard { export class JCStandard {
private web3: Web3; private web3: Web3;
@ -38,19 +38,13 @@ export class JCStandard {
}) { }) {
const cfg = jc.wallet.currentChainCfg; const cfg = jc.wallet.currentChainCfg;
const addressFactory = cfg.contracts.minterFactory; const addressFactory = cfg.contracts.minterFactory;
console.log("addressFactory:: ", addressFactory); console.log('addressFactory:: ', addressFactory);
const contract = new this.web3.eth.Contract( const contract = new this.web3.eth.Contract(abiMinterFactory, addressFactory, {
abiMinterFactory, //@ts-ignore
addressFactory, from: jc.wallet.currentAccAddr,
{ });
//@ts-ignore
from: jc.wallet.currentAccAddr,
}
);
if (!gas) { if (!gas) {
gas = await contract.methods gas = await contract.methods.mintNft(address, tokenIds, startTime, saltNonce, signature).estimateGas();
.mintNft(address, tokenIds, startTime, saltNonce, signature)
.estimateGas();
} }
gas = (gas * GAS_BOOST) | 1; gas = (gas * GAS_BOOST) | 1;
if (estimate) { if (estimate) {
@ -68,14 +62,12 @@ export class JCStandard {
} }
const logData = { const logData = {
gas, gas,
title: "mint_nft", title: 'mint_nft',
details: details, details: details,
}; };
return universalChainCb( return universalChainCb(
logData, logData,
contract.methods contract.methods.mintNft(address, tokenIds, startTime, saltNonce, signature).send({ gas })
.mintNft(address, tokenIds, startTime, saltNonce, signature)
.send({ gas })
); );
} }
// begin of NFT Market // begin of NFT Market
@ -97,9 +89,9 @@ export class JCStandard {
estimate: number; estimate: number;
}) { }) {
const cfg = jc.wallet.currentChainCfg; const cfg = jc.wallet.currentChainCfg;
estimate = parseInt(estimate + ""); estimate = parseInt(estimate + '');
const addressMarket = cfg.contracts.nftmarket; const addressMarket = cfg.contracts.nftmarket;
console.log("addressMarket:: ", addressMarket); console.log('addressMarket:: ', addressMarket);
const contract = new this.web3.eth.Contract(abiMarket, addressMarket, { const contract = new this.web3.eth.Contract(abiMarket, addressMarket, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccAddr, from: jc.wallet.currentAccAddr,
@ -137,21 +129,14 @@ export class JCStandard {
]; ];
const logData = { const logData = {
gas, gas,
title: "market_sell", title: 'market_sell',
details: details, details: details,
}; };
if (approved != addressMarket) { if (approved != addressMarket) {
await tokenInstance.methods await tokenInstance.methods.approve(addressMarket, tokenId).send({ gas: gasApprove });
.approve(addressMarket, tokenId) console.log('approve done');
.send({ gas: gasApprove });
console.log("approve done");
} }
return universalChainCb( return universalChainCb(logData, contract.methods.sell(nftToken, currency, tokenId, price, amount).send({ gas }));
logData,
contract.methods
.sell(nftToken, currency, tokenId, price, amount)
.send({ gas })
);
} }
async marketUpdatePrice({ async marketUpdatePrice({
@ -166,9 +151,9 @@ export class JCStandard {
estimate: number; estimate: number;
}) { }) {
const cfg = jc.wallet.currentChainCfg; const cfg = jc.wallet.currentChainCfg;
estimate = parseInt(estimate + ""); estimate = parseInt(estimate + '');
const addressMarket = cfg.contracts.nftmarket; const addressMarket = cfg.contracts.nftmarket;
console.log("addressMarket:: ", addressMarket); console.log('addressMarket:: ', addressMarket);
const contract = new this.web3.eth.Contract(abiMarket, addressMarket, { const contract = new this.web3.eth.Contract(abiMarket, addressMarket, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccAddr, from: jc.wallet.currentAccAddr,
@ -192,28 +177,17 @@ export class JCStandard {
]; ];
const logData = { const logData = {
gas, gas,
title: "market_update_price", title: 'market_update_price',
details: details, details: details,
}; };
return universalChainCb( return universalChainCb(logData, contract.methods.updatePrice(orderId, price).send({ gas }));
logData,
contract.methods.updatePrice(orderId, price).send({ gas })
);
} }
async marketCancelOrder({ async marketCancelOrder({ orderId, gas, estimate }: { orderId: string; gas?: number; estimate: number }) {
orderId,
gas,
estimate,
}: {
orderId: string;
gas?: number;
estimate: number;
}) {
const cfg = jc.wallet.currentChainCfg; const cfg = jc.wallet.currentChainCfg;
estimate = parseInt(estimate + ""); estimate = parseInt(estimate + '');
const addressMarket = cfg.contracts.nftmarket; const addressMarket = cfg.contracts.nftmarket;
console.log("addressMarket:: ", addressMarket); console.log('addressMarket:: ', addressMarket);
const contract = new this.web3.eth.Contract(abiMarket, addressMarket, { const contract = new this.web3.eth.Contract(abiMarket, addressMarket, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccAddr, from: jc.wallet.currentAccAddr,
@ -237,28 +211,17 @@ export class JCStandard {
]; ];
const logData = { const logData = {
gas, gas,
title: "market_cancel_order", title: 'market_cancel_order',
details: details, details: details,
}; };
return universalChainCb( return universalChainCb(logData, contract.methods.cancelOrder(orderId).send({ gas }));
logData,
contract.methods.cancelOrder(orderId).send({ gas })
);
} }
async marketBuy({ async marketBuy({ orderId, gas, estimate }: { orderId: string; gas?: number; estimate: number }) {
orderId,
gas,
estimate,
}: {
orderId: string;
gas?: number;
estimate: number;
}) {
const cfg = jc.wallet.currentChainCfg; const cfg = jc.wallet.currentChainCfg;
estimate = parseInt(estimate + ""); estimate = parseInt(estimate + '');
const addressMarket = cfg.contracts.nftmarket; const addressMarket = cfg.contracts.nftmarket;
console.log("addressMarket:: ", addressMarket); console.log('addressMarket:: ', addressMarket);
const contract = new this.web3.eth.Contract(abiMarket, addressMarket, { const contract = new this.web3.eth.Contract(abiMarket, addressMarket, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccAddr, from: jc.wallet.currentAccAddr,
@ -267,18 +230,12 @@ export class JCStandard {
if (!orderInfo.orderId) { if (!orderInfo.orderId) {
throw new ZError(100, `order info with id: ${orderId} not found`); throw new ZError(100, `order info with id: ${orderId} not found`);
} }
const tokenInstance = new this.web3.eth.Contract( const tokenInstance = new this.web3.eth.Contract(abiERC20, orderInfo.currency, {
abiERC20, //@ts-ignore
orderInfo.currency, from: jc.wallet.currentAccAddr,
{ });
//@ts-ignore let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressMarket).call();
from: jc.wallet.currentAccAddr, console.log('approved:: ', approved);
}
);
let approved = await tokenInstance.methods
.allowance(jc.wallet.currentAccAddr, addressMarket)
.call();
console.log("approved:: ", approved);
let gasApprove = 0; let gasApprove = 0;
if (approved < orderInfo.price) { if (approved < orderInfo.price) {
gasApprove = cfg.gasInfo.cecApprove; gasApprove = cfg.gasInfo.cecApprove;
@ -305,20 +262,26 @@ export class JCStandard {
]; ];
const logData = { const logData = {
gas, gas,
title: "market_buy", title: 'market_buy',
details: details, details: details,
}; };
if (approved < orderInfo.price) { if (approved < orderInfo.price) {
await tokenInstance.methods await tokenInstance.methods.approve(addressMarket, orderInfo.price).send({ gas: gasApprove });
.approve(addressMarket, orderInfo.price)
.send({ gas: gasApprove });
} }
return universalChainCb( return universalChainCb(logData, contract.methods.buy(orderId).send({ gas }));
logData,
contract.methods.buy(orderId).send({ gas })
);
} }
async marketOrderInfo(orderId: string) {
const cfg = jc.wallet.currentChainCfg;
const addressMarket = cfg.contracts.nftmarket;
console.log('addressMarket:: ', addressMarket);
const contract = new this.web3.eth.Contract(abiMarket, addressMarket, {
//@ts-ignore
from: jc.wallet.currentAccAddr,
});
let orderInfo = await contract.methods.orderInfos(orderId).call();
return orderInfo;
}
// end of NFT Market // end of NFT Market
// begin of game item mall // begin of game item mall
@ -342,9 +305,9 @@ export class JCStandard {
estimate: number; estimate: number;
}) { }) {
const cfg = jc.wallet.currentChainCfg; const cfg = jc.wallet.currentChainCfg;
estimate = parseInt(estimate + ""); estimate = parseInt(estimate + '');
const addressGameMall = cfg.contracts.gamemall; const addressGameMall = cfg.contracts.gamemall;
console.log("addressGameMall:: ", addressGameMall); console.log('addressGameMall:: ', addressGameMall);
const contract = new this.web3.eth.Contract(abiGameMall, addressGameMall, { const contract = new this.web3.eth.Contract(abiGameMall, addressGameMall, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccAddr, from: jc.wallet.currentAccAddr,
@ -353,10 +316,8 @@ export class JCStandard {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccAddr, from: jc.wallet.currentAccAddr,
}); });
let approved = await tokenInstance.methods let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressGameMall).call();
.allowance(jc.wallet.currentAccAddr, addressGameMall) console.log('approved:: ', approved);
.call();
console.log("approved:: ", approved);
let gasApprove = 0; let gasApprove = 0;
if (approved < price) { if (approved < price) {
gasApprove = cfg.gasInfo.cecApprove; gasApprove = cfg.gasInfo.cecApprove;
@ -383,25 +344,20 @@ export class JCStandard {
]; ];
const logData = { const logData = {
gas, gas,
title: "game_mall_buy", title: 'game_mall_buy',
details: details, details: details,
}; };
if (approved < price) { if (approved < price) {
await tokenInstance.methods await tokenInstance.methods.approve(addressGameMall, price).send({ gas: gasApprove });
.approve(addressGameMall, price)
.send({ gas: gasApprove });
} }
return universalChainCb( return universalChainCb(
logData, logData,
contract.methods contract.methods.buy(orderId, currency, price, startTime, saltNonce, signature).send({ gas })
.buy(orderId, currency, price, startTime, saltNonce, signature)
.send({ gas })
); );
} }
// end of game item mall // end of game item mall
// begin of game item market // begin of game item market
// TODO:: test
async gameMarketBuy({ async gameMarketBuy({
orderId, orderId,
seller, seller,
@ -424,31 +380,25 @@ export class JCStandard {
estimate: number; estimate: number;
}) { }) {
const cfg = jc.wallet.currentChainCfg; const cfg = jc.wallet.currentChainCfg;
estimate = parseInt(estimate + ""); estimate = parseInt(estimate + '');
const addressGameMarket = cfg.contracts.gamemarket; const addressGameMarket = cfg.contracts.gamemarket;
console.log("addressGameMarket:: ", addressGameMarket); console.log('addressGameMarket:: ', addressGameMarket);
const contract = new this.web3.eth.Contract( const contract = new this.web3.eth.Contract(abiGameMarket, addressGameMarket, {
abiGameMarket, //@ts-ignore
addressGameMarket, from: jc.wallet.currentAccAddr,
{ });
//@ts-ignore
from: jc.wallet.currentAccAddr,
}
);
const tokenInstance = new this.web3.eth.Contract(abiERC20, currency, { const tokenInstance = new this.web3.eth.Contract(abiERC20, currency, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccAddr, from: jc.wallet.currentAccAddr,
}); });
let approved = await tokenInstance.methods let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressGameMarket).call();
.allowance(jc.wallet.currentAccAddr, addressGameMarket) console.log('approved:: ', approved);
.call();
console.log("approved:: ", approved);
let gasApprove = 0; let gasApprove = 0;
if (approved < price) { if (approved < price) {
gasApprove = cfg.gasInfo.cecApprove; gasApprove = cfg.gasInfo.cecApprove;
} }
gasApprove = (gasApprove * GAS_BOOST) | 1; gasApprove = (gasApprove * GAS_BOOST) | 1;
console.log("gasApprove: " + gasApprove); console.log('gasApprove: ' + gasApprove);
if (!gas) { if (!gas) {
// gas = await contract.methods // gas = await contract.methods
// .buy(orderId, seller, currency, price, startTime, saltNonce, signature) // .buy(orderId, seller, currency, price, startTime, saltNonce, signature)
@ -456,7 +406,7 @@ export class JCStandard {
gas = cfg.gasInfo.gameMarketBuy; gas = cfg.gasInfo.gameMarketBuy;
} }
gas = (gas * GAS_BOOST) | 1; gas = (gas * GAS_BOOST) | 1;
console.log("gas: " + gas); console.log('gas: ' + gas);
if (estimate) { if (estimate) {
return jc.wallet.generateGasShow(gas + gasApprove); return jc.wallet.generateGasShow(gas + gasApprove);
@ -472,19 +422,15 @@ export class JCStandard {
]; ];
const logData = { const logData = {
gas, gas,
title: "game_market_buy", title: 'game_market_buy',
details: details, details: details,
}; };
if (approved < price) { if (approved < price) {
await tokenInstance.methods await tokenInstance.methods.approve(addressGameMarket, price).send({ gas: gasApprove });
.approve(addressGameMarket, price)
.send({ gas: gasApprove });
} }
return universalChainCb( return universalChainCb(
logData, logData,
contract.methods contract.methods.buy(orderId, seller, currency, price, startTime, saltNonce, signature).send({ gas })
.buy(orderId, seller, currency, price, startTime, saltNonce, signature)
.send({ gas })
); );
} }
// end of game item market // end of game item market
@ -502,9 +448,9 @@ export class JCStandard {
estimate: number; estimate: number;
}) { }) {
const cfg = jc.wallet.currentChainCfg; const cfg = jc.wallet.currentChainCfg;
estimate = parseInt(estimate + ""); estimate = parseInt(estimate + '');
const addressMall = cfg.contracts.nftmall; const addressMall = cfg.contracts.nftmall;
console.log("addressMall:: ", addressMall); console.log('addressMall:: ', addressMall);
const contract = new this.web3.eth.Contract(abiMall, addressMall, { const contract = new this.web3.eth.Contract(abiMall, addressMall, {
//@ts-ignore //@ts-ignore
from: jc.wallet.currentAccAddr, from: jc.wallet.currentAccAddr,
@ -515,17 +461,13 @@ export class JCStandard {
gas = (gas * GAS_BOOST) | 1; gas = (gas * GAS_BOOST) | 1;
const tokenInstance = new this.web3.eth.Contract(abiERC20, addresses[2]); const tokenInstance = new this.web3.eth.Contract(abiERC20, addresses[2]);
let gasApprove = await tokenInstance.methods let gasApprove = await tokenInstance.methods.approve(addressMall, values[1]).estimateGas();
.approve(addressMall, values[1])
.estimateGas();
gasApprove = (gasApprove * GAS_BOOST) | 1; gasApprove = (gasApprove * GAS_BOOST) | 1;
if (estimate) { if (estimate) {
return jc.wallet.generateGasShow(gas + gasApprove); return jc.wallet.generateGasShow(gas + gasApprove);
} }
await tokenInstance.methods await tokenInstance.methods.approve(addressMall, values[1]).sent({ gas: gasApprove });
.approve(addressMall, values[1])
.sent({ gas: gasApprove });
let details = [ let details = [
{ {
@ -549,13 +491,10 @@ export class JCStandard {
]; ];
const logData = { const logData = {
gas, gas,
title: "mall_buy_nft", title: 'mall_buy_nft',
details: details, details: details,
}; };
return universalChainCb( return universalChainCb(logData, contract.methods.buyNFT(addresses, values).send({ gas }));
logData,
contract.methods.buyNFT(addresses, values).send({ gas })
);
} }
// end of NFT Mall // end of NFT Mall
} }