修改senderc20token的gas费, 增加crypto购买token的方法
This commit is contained in:
parent
ba0d976e42
commit
2f745f2216
390
src/abis/abiTokenMall.ts
Normal file
390
src/abis/abiTokenMall.ts
Normal file
@ -0,0 +1,390 @@
|
||||
import { AbiItem } from 'web3-utils';
|
||||
|
||||
export let abiTokenMall: AbiItem[] = [
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '_tokenAddress',
|
||||
type: 'address',
|
||||
},
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '_seller',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'constructor',
|
||||
},
|
||||
{
|
||||
anonymous: false,
|
||||
inputs: [
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'address',
|
||||
name: 'erc20',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'AddERC20Suppout',
|
||||
type: 'event',
|
||||
},
|
||||
{
|
||||
anonymous: false,
|
||||
inputs: [
|
||||
{
|
||||
indexed: true,
|
||||
internalType: 'address',
|
||||
name: 'buyer',
|
||||
type: 'address',
|
||||
},
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'address',
|
||||
name: 'tokenAddress',
|
||||
type: 'address',
|
||||
},
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'address',
|
||||
name: 'currency',
|
||||
type: 'address',
|
||||
},
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'uint256',
|
||||
name: 'amount',
|
||||
type: 'uint256',
|
||||
},
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'uint256',
|
||||
name: 'tokenAmount',
|
||||
type: 'uint256',
|
||||
},
|
||||
],
|
||||
name: 'BuyTransaction',
|
||||
type: 'event',
|
||||
},
|
||||
{
|
||||
anonymous: false,
|
||||
inputs: [
|
||||
{
|
||||
indexed: true,
|
||||
internalType: 'address',
|
||||
name: 'previousOwner',
|
||||
type: 'address',
|
||||
},
|
||||
{
|
||||
indexed: true,
|
||||
internalType: 'address',
|
||||
name: 'newOwner',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'OwnershipTransferred',
|
||||
type: 'event',
|
||||
},
|
||||
{
|
||||
anonymous: false,
|
||||
inputs: [
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'address',
|
||||
name: 'erc20',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'RemoveERC20Suppout',
|
||||
type: 'event',
|
||||
},
|
||||
{
|
||||
anonymous: false,
|
||||
inputs: [
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'address',
|
||||
name: 'seller',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'SellerUpdated',
|
||||
type: 'event',
|
||||
},
|
||||
{
|
||||
anonymous: false,
|
||||
inputs: [
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'address',
|
||||
name: 'tokenAddress',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'TokenAddressUpdated',
|
||||
type: 'event',
|
||||
},
|
||||
{
|
||||
anonymous: false,
|
||||
inputs: [
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'address',
|
||||
name: 'currency',
|
||||
type: 'address',
|
||||
},
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'uint256',
|
||||
name: 'price',
|
||||
type: 'uint256',
|
||||
},
|
||||
{
|
||||
indexed: false,
|
||||
internalType: 'uint256',
|
||||
name: 'pricePre',
|
||||
type: 'uint256',
|
||||
},
|
||||
],
|
||||
name: 'UpdateTokenPrice',
|
||||
type: 'event',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: 'erc20',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'addERC20Support',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'erc20Supported',
|
||||
outputs: [
|
||||
{
|
||||
internalType: 'bool',
|
||||
name: '',
|
||||
type: 'bool',
|
||||
},
|
||||
],
|
||||
stateMutability: 'view',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [],
|
||||
name: 'executor',
|
||||
outputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
stateMutability: 'view',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [],
|
||||
name: 'feeToAddress',
|
||||
outputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
stateMutability: 'view',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [],
|
||||
name: 'owner',
|
||||
outputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
stateMutability: 'view',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'prices',
|
||||
outputs: [
|
||||
{
|
||||
internalType: 'uint256',
|
||||
name: '',
|
||||
type: 'uint256',
|
||||
},
|
||||
],
|
||||
stateMutability: 'view',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: 'erc20',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'removeERC20Support',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [],
|
||||
name: 'renounceOwnership',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [],
|
||||
name: 'seller',
|
||||
outputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
stateMutability: 'view',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '_feeToAddress',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'setFeeToAddress',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [],
|
||||
name: 'tokenAddress',
|
||||
outputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
stateMutability: 'view',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: 'newOwner',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'transferOwnership',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: 'account',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'updateExecutor',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '_tokenAddress',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'updateTokenAddress',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: '_seller',
|
||||
type: 'address',
|
||||
},
|
||||
],
|
||||
name: 'updateSeller',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: 'currency',
|
||||
type: 'address',
|
||||
},
|
||||
{
|
||||
internalType: 'uint256',
|
||||
name: 'price',
|
||||
type: 'uint256',
|
||||
},
|
||||
],
|
||||
name: 'updateTokenPrice',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
{
|
||||
inputs: [
|
||||
{
|
||||
internalType: 'address',
|
||||
name: 'currency',
|
||||
type: 'address',
|
||||
},
|
||||
{
|
||||
internalType: 'uint256',
|
||||
name: 'amount',
|
||||
type: 'uint256',
|
||||
},
|
||||
],
|
||||
name: 'buyToken',
|
||||
outputs: [],
|
||||
stateMutability: 'nonpayable',
|
||||
type: 'function',
|
||||
},
|
||||
];
|
@ -10,6 +10,14 @@ export function queryTokenUsdPrice(eth: string, chain: string, env: string) {
|
||||
const url = `${PAY_API_HOST}/pay/alchemy/crypto_price`;
|
||||
return POST_JSON(url, { token: eth, chain, env });
|
||||
}
|
||||
/**
|
||||
* check if user can buy eth from treasury
|
||||
* @returns
|
||||
*/
|
||||
export function checkBuyTreasury() {
|
||||
const url = `${PAY_API_HOST}/pay/alchemy/can_i_buy_treasury`;
|
||||
return GET_JSON(url);
|
||||
}
|
||||
|
||||
export function queryFiatList() {
|
||||
const url = `${PAY_API_HOST}/pay/alchemy/fait_list`;
|
||||
|
@ -54,6 +54,7 @@ module.exports = {
|
||||
nftmall: '0x4d71dE0428328112A78D402A6fdd7ceD1C96B71e',
|
||||
gamemarket: '0x46e2C612756b702b3d68d89F97c88FFa725F6fab',
|
||||
gamemall: '0x1D058c7c7451c34BbfF9c0dF1C16b95C5d171d64',
|
||||
tokenmall: '0x22557f3fe7fbA53C66a35a3D70527D014704fd4D',
|
||||
},
|
||||
gasInfo: {
|
||||
nftApprove: 49340,
|
||||
|
@ -119,4 +119,15 @@ export class PaySvr {
|
||||
}
|
||||
|
||||
// end of google pay
|
||||
|
||||
// begin of iOS purchase
|
||||
public async queryIOSProducts(productIds: string[]) {
|
||||
let str = await new NativeSvr().queryProducts(productIds.join(','));
|
||||
return str;
|
||||
}
|
||||
|
||||
public async beginIOSPurchase(productId: string, orderId: string) {
|
||||
let result = await new NativeSvr().buyProduct(productId, orderId);
|
||||
}
|
||||
// end of iOS purchase
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import { abiERC1155 } from '../abis/abiERC1155';
|
||||
import { universalChainCb } from '../util/chain.util';
|
||||
import { timeoutFetch } from '../util/net.util';
|
||||
import { getFormattedIpfsUrl } from '../util/wallet.util';
|
||||
import { GAS_BOOST } from '../config/constants';
|
||||
|
||||
export const ERC1155 = 'ERC1155';
|
||||
export const ERC1155_INTERFACE_ID = '0xd9b67a26';
|
||||
@ -181,7 +182,7 @@ export class ERC1155Standard {
|
||||
if (!gas) {
|
||||
gas = await contract.methods.safeBatchTransferFrom(from, to, tokenIds, amounts, []).estimateGas();
|
||||
}
|
||||
gas = (gas * 1.1) | 1;
|
||||
gas = (gas * GAS_BOOST) | 1;
|
||||
if (estimate) {
|
||||
return jc.wallet.generateGasShow(gas);
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import { abiERC20 } from '../abis/abiERC20';
|
||||
import { BN, toUtf8 } from 'ethereumjs-util';
|
||||
import { universalChainCb } from '../util/chain.util';
|
||||
import { toWeiBn } from '../util/number.util';
|
||||
import { GAS_BOOST } from '../config/constants';
|
||||
|
||||
export class ERC20Standard {
|
||||
private web3: Web3;
|
||||
@ -142,7 +143,7 @@ export class ERC20Standard {
|
||||
from,
|
||||
to,
|
||||
amount,
|
||||
decimal = 18,
|
||||
decimal,
|
||||
gas,
|
||||
estimate,
|
||||
}: {
|
||||
@ -150,16 +151,23 @@ export class ERC20Standard {
|
||||
from: string;
|
||||
to: string;
|
||||
amount: number | string;
|
||||
decimal?: number;
|
||||
decimal?: string;
|
||||
gas?: number;
|
||||
estimate: number;
|
||||
}) {
|
||||
const contract = new this.web3.eth.Contract(abiERC20, address);
|
||||
let amountBN = toWeiBn(amount, decimal);
|
||||
if (!decimal) {
|
||||
decimal = await this.getTokenDecimals(address);
|
||||
}
|
||||
let amountBN = toWeiBn(amount, +decimal);
|
||||
if (!gas) {
|
||||
gas = await contract.methods.transfer(to, '0').estimateGas();
|
||||
let toBalance = await this.getBalanceOf(address, to);
|
||||
if (toBalance.eq(new BN(0))) {
|
||||
gas += 12000; // SSTORE new value is more expensive
|
||||
}
|
||||
}
|
||||
gas = (gas * 1.1) | 1;
|
||||
gas = (gas * GAS_BOOST) | 1;
|
||||
if (estimate) {
|
||||
return jc.wallet.generateGasShow(gas);
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import { abiERC721 } from '../abis/abiERC721';
|
||||
import { universalChainCb } from '../util/chain.util';
|
||||
import { timeoutFetch } from '../util/net.util';
|
||||
import { getFormattedIpfsUrl } from '../util/wallet.util';
|
||||
import { GAS_BOOST } from '../config/constants';
|
||||
|
||||
export const ERC721 = 'ERC721';
|
||||
export const ERC721_INTERFACE_ID = '0x80ac58cd';
|
||||
@ -278,7 +279,7 @@ export class ERC721Standard {
|
||||
if (!gas) {
|
||||
gas = await contract.methods.safeTransferFrom(from, to, tokenId).estimateGas();
|
||||
}
|
||||
gas = (gas * 1.1) | 1;
|
||||
gas = (gas * GAS_BOOST) | 1;
|
||||
if (estimate) {
|
||||
return jc.wallet.generateGasShow(gas);
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import { ZError } from '../common/ZError';
|
||||
import { abiERC20 } from '../abis/abiERC20';
|
||||
import { abiERC721 } from '../abis/abiERC721';
|
||||
import { toBN } from '../util/number.util';
|
||||
import { abiTokenMall } from '../abis/abiTokenMall';
|
||||
|
||||
export class JCStandard {
|
||||
private web3: Web3;
|
||||
@ -479,6 +480,77 @@ export class JCStandard {
|
||||
);
|
||||
}
|
||||
// end of game item market
|
||||
// begin of token Mall
|
||||
/**
|
||||
*
|
||||
* @param values: [orderId, price, startTime, saltNonce]
|
||||
* @returns
|
||||
*/
|
||||
async buyTokenWithErc20({ currency, amount, estimate }: { currency: string; amount: string; estimate: number }) {
|
||||
const cfg = jc.wallet.currentChainCfg;
|
||||
estimate = parseInt(estimate + '');
|
||||
const addressMall = cfg.contracts.tokenmall;
|
||||
console.log('addressMall:: ', addressMall);
|
||||
const contract = new this.web3.eth.Contract(abiTokenMall, addressMall, {
|
||||
//@ts-ignore
|
||||
from: jc.wallet.currentAccAddr,
|
||||
});
|
||||
const tokenInstance = new this.web3.eth.Contract(abiERC20, currency, {
|
||||
//@ts-ignore
|
||||
from: jc.wallet.currentAccAddr,
|
||||
});
|
||||
let approved = await tokenInstance.methods.allowance(jc.wallet.currentAccAddr, addressMall).call();
|
||||
console.log('approved:: ', approved);
|
||||
let gasPrice = await jc.wallet.fetchGasPrice();
|
||||
console.log('gas price: ' + gasPrice);
|
||||
let gasApprove = 0;
|
||||
if (toBN(approved).lt(toBN(amount))) {
|
||||
gasApprove = await tokenInstance.methods.approve(addressMall, amount).estimateGas();
|
||||
gasApprove = (gasApprove * GAS_BOOST) | 1;
|
||||
await tokenInstance.methods.approve(addressMall, amount).send({ gas: gasApprove, gasPrice });
|
||||
}
|
||||
let gas = await contract.methods.buyToken(currency, amount).estimateGas();
|
||||
gas = (gas * GAS_BOOST) | 1;
|
||||
if (estimate) {
|
||||
return jc.wallet.generateGasShow(gas + gasApprove);
|
||||
}
|
||||
|
||||
let details: any = [
|
||||
{
|
||||
address: addressMall,
|
||||
from: jc.wallet.currentAccAddr,
|
||||
to: addressMall,
|
||||
},
|
||||
{
|
||||
address: currency,
|
||||
from: jc.wallet.currentAccAddr,
|
||||
to: addressMall,
|
||||
value: amount,
|
||||
},
|
||||
];
|
||||
let tokenAddress = await contract.methods.tokenAddress().call();
|
||||
let currencyDecimals = await tokenInstance.methods.decimals().call();
|
||||
let price = await contract.methods.prices(currency).call();
|
||||
let tokenAmount = toBN(price)
|
||||
.mul(toBN(amount))
|
||||
.mul(toBN(10).pow(toBN(10).sub(toBN(currencyDecimals))))
|
||||
.div(toBN('1000000'));
|
||||
console.log(`buyTokenWithErc20: currency: ${currency}, amount: ${amount}, tokenAmount: ${tokenAmount}`);
|
||||
details.push({
|
||||
address: tokenAddress,
|
||||
from: addressMall,
|
||||
to: jc.wallet.currentAccAddr,
|
||||
value: tokenAmount,
|
||||
});
|
||||
const logData = {
|
||||
gas,
|
||||
title: 'mall_buy_token',
|
||||
details: details,
|
||||
};
|
||||
return universalChainCb(logData, contract.methods.buyToken(currency, amount).send({ gas, gasPrice }));
|
||||
}
|
||||
// end of token Mall
|
||||
|
||||
// begin of NFT Mall
|
||||
/**
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user