market buy add price

This commit is contained in:
CounterFire2023 2023-07-03 13:31:35 +08:00
parent b29fa53af3
commit 2e1c76976d
3 changed files with 511 additions and 455 deletions

File diff suppressed because it is too large Load Diff

View File

@ -36,7 +36,7 @@ module.exports = {
],
contracts: {
minterFactory: '0x1A27515c35a92Fb276c2670fa27C85ffAd75D094',
nftmarket: '0x28cd862Baa826e65a0c0F1179B9A3423dF4d366A',
nftmarket: '0xB9720d2c9D01FE36a1aA1224cbB06BF8e436812E',
nftmall: '0x4d71dE0428328112A78D402A6fdd7ceD1C96B71e',
gamemarket: '0x46e2C612756b702b3d68d89F97c88FFa725F6fab',
gamemall: '0x1D058c7c7451c34BbfF9c0dF1C16b95C5d171d64',

View File

@ -9,6 +9,7 @@ import { abiGameMall } from '../abis/abiGameMall';
import { ZError } from '../common/ZError';
import { abiERC20 } from '../abis/abiERC20';
import { abiERC721 } from '../abis/abiERC721';
import { toBN } from '../util/number.util';
export class JCStandard {
private web3: Web3;
@ -234,7 +235,17 @@ export class JCStandard {
return universalChainCb(logData, contract.methods.cancelOrder(orderId).send({ gas, gasPrice }));
}
async marketBuy({ orderId, gas, estimate }: { orderId: string; gas?: number; estimate: number }) {
async marketBuy({
orderId,
gas,
price,
estimate,
}: {
orderId: string;
price: string;
gas?: number;
estimate: number;
}) {
const cfg = jc.wallet.currentChainCfg;
estimate = parseInt(estimate + '');
const addressMarket = cfg.contracts.nftmarket;
@ -247,6 +258,9 @@ export class JCStandard {
if (!orderInfo.orderId) {
throw new ZError(100, `order info with id: ${orderId} not found`);
}
if (!toBN(price).eq(toBN(orderInfo.price))) {
throw new ZError(101, `price must equal to ${orderInfo.price}`);
}
const tokenInstance = new this.web3.eth.Contract(abiERC20, orderInfo.currency, {
//@ts-ignore
from: jc.wallet.currentAccAddr,
@ -255,10 +269,10 @@ export class JCStandard {
console.log('approved:: ', approved);
let gasApprove = 0;
let gasPrice = await jc.wallet.fetchGasPrice();
if (approved < orderInfo.price) {
gasApprove = await tokenInstance.methods.approve(addressMarket, orderInfo.price).estimateGas();
if (toBN(approved).lt(toBN(price))) {
gasApprove = await tokenInstance.methods.approve(addressMarket, price).estimateGas();
gasApprove = (gasApprove * GAS_BOOST) | 1;
await tokenInstance.methods.approve(addressMarket, orderInfo.price).send({ gas: gasApprove, gasPrice });
await tokenInstance.methods.approve(addressMarket, price).send({ gas: gasApprove, gasPrice });
}
if (!gas) {
gas = await contract.methods.buy(orderId).estimateGas();
@ -269,7 +283,6 @@ export class JCStandard {
if (estimate) {
return jc.wallet.generateGasShow(gas + gasApprove);
}
//TODO:: 记录详细的转账记录, -> seller, -> txFeeAddress, -> txTaxAddress
let details = [
{
address: addressMarket,
@ -295,7 +308,7 @@ export class JCStandard {
title: 'market_buy',
details: details,
};
return universalChainCb(logData, contract.methods.buy(orderId).send({ gas, gasPrice }));
return universalChainCb(logData, contract.methods.buy(orderId, price).send({ gas, gasPrice }));
}
async marketOrderInfo(orderId: string) {
@ -347,7 +360,7 @@ export class JCStandard {
console.log('approved:: ', approved);
let gasApprove = 0;
let gasPrice = await jc.wallet.fetchGasPrice();
if (approved < price) {
if (toBN(approved).lt(toBN(price))) {
gasApprove = await tokenInstance.methods.approve(addressGameMall, price).estimateGas();
gasApprove = (gasApprove * GAS_BOOST) | 1;
console.log('gasApprove:: ', gasApprove);
@ -426,7 +439,7 @@ export class JCStandard {
console.log('approved:: ', approved);
let gasApprove = 0;
let gasPrice = await jc.wallet.fetchGasPrice();
if (approved < price) {
if (toBN(approved).lt(toBN(price))) {
gasApprove = await tokenInstance.methods.approve(addressGameMarket, price).estimateGas();
gasApprove = (gasApprove * GAS_BOOST) | 1;
await tokenInstance.methods.approve(addressGameMarket, price).send({ gas: gasApprove, gasPrice });
@ -508,7 +521,7 @@ export class JCStandard {
let gasPrice = await jc.wallet.fetchGasPrice();
console.log('gas price: ' + gasPrice);
let gasApprove = 0;
if (approved < values[1]) {
if (toBN(approved).lt(toBN(values[1]))) {
gasApprove = await tokenInstance.methods.approve(addressMall, values[1]).estimateGas();
gasApprove = (gasApprove * GAS_BOOST) | 1;
await tokenInstance.methods.approve(addressMall, values[1]).send({ gas: gasApprove, gasPrice });