移除estimateGas时的gas, 增加一些常用方法

This commit is contained in:
zhl 2023-05-31 17:19:26 +08:00
parent 6cc9b2ee3a
commit c85848ceec
10 changed files with 54 additions and 36 deletions

View File

@ -6,9 +6,9 @@ export function reqAlchemyPrePay(data: any) {
return POST_JSON(url, data); return POST_JSON(url, data);
} }
export function queryEthUsdPrice(eth: string, chain: string) { export function queryTokenUsdPrice(eth: string, chain: string, env: string) {
const url = `${WALLET_API_HOST}/pay/alchemy/crypto_price`; const url = `${WALLET_API_HOST}/pay/alchemy/crypto_price`;
return POST_JSON(url, { token: eth, chain }); return POST_JSON(url, { token: eth, chain, env });
} }
export function queryFiatList() { export function queryFiatList() {

View File

@ -3,7 +3,7 @@ module.exports = {
{ {
type: "eth", type: "eth",
address: "eth", address: "eth",
symbol: "MATIC", symbol: "AGOR",
decimal: 18, decimal: 18,
}, },
{ {

View File

@ -212,9 +212,19 @@ export const AllChains = [
type: "Mainnet", type: "Mainnet",
rpc: "https://rpc.ankr.com/arbitrum", rpc: "https://rpc.ankr.com/arbitrum",
id: 42161, id: 42161,
network: "ARBITRUM",
symbol: "ETH", symbol: "ETH",
explorerurl: "https://arbiscan.io/", explorerurl: "https://arbiscan.io/",
}, },
{
name: "Arbitrum Goerli",
type: "Testnet",
rpc: "https://endpoints.omniatech.io/v1/arbitrum/goerli/public",
id: 421613,
network: "AGOR",
symbol: "AGOR",
explorerurl: "https://goerli-rollup-explorer.arbitrum.io",
},
{ {
name: "Celo Mainnet RPC", name: "Celo Mainnet RPC",
type: "Mainnet", type: "Mainnet",
@ -255,14 +265,7 @@ export const AllChains = [
symbol: "MATIC", symbol: "MATIC",
explorerurl: "https://mumbai.polygonscan.com/", explorerurl: "https://mumbai.polygonscan.com/",
}, },
{
name: "Arbitrum Goerli",
type: "Testnet",
rpc: "https://endpoints.omniatech.io/v1/arbitrum/goerli/public",
id: 421613,
symbol: "AGOR",
explorerurl: "https://goerli-rollup-explorer.arbitrum.io",
},
{ {
name: "Harmony Mainnet RPC", name: "Harmony Mainnet RPC",
type: "Mainnet", type: "Mainnet",

View File

@ -297,7 +297,11 @@ export default class JCWallet {
let price = await new PaySvr().getGasPrice(chainData.id); let price = await new PaySvr().getGasPrice(chainData.id);
let ehtBN = safeNumberToBN(price).mul(safeNumberToBN(gas)); let ehtBN = safeNumberToBN(price).mul(safeNumberToBN(gas));
let ethSymbol = chainData.type !== "Testnet" ? chainData.symbol : "ETH"; let ethSymbol = chainData.type !== "Testnet" ? chainData.symbol : "ETH";
let fiatPrice = await new PaySvr().queryTokenPrice(ethSymbol, ethSymbol); let network =
chainData.type !== "Testnet"
? chainData.network || chainData.symbol
: "ETH";
let fiatPrice = await new PaySvr().queryTokenPrice(network, ethSymbol);
let priceFloat = parseFloat(fiatPrice) * 100; let priceFloat = parseFloat(fiatPrice) * 100;
let fiatPriceBN = safeNumberToBN(priceFloat); let fiatPriceBN = safeNumberToBN(priceFloat);
let usd = fromTokenMinimalUnit(ehtBN.mul(fiatPriceBN), 20); let usd = fromTokenMinimalUnit(ehtBN.mul(fiatPriceBN), 20);
@ -313,8 +317,12 @@ export default class JCWallet {
*/ */
public async calcTokenPrice(tokenName: string, amount: string) { public async calcTokenPrice(tokenName: string, amount: string) {
let chainData = this.currentChain; let chainData = this.currentChain;
let ethSymbol = chainData.type !== "Testnet" ? chainData.symbol : "ETH"; let network =
let fiatPrice = await new PaySvr().queryTokenPrice(ethSymbol, tokenName); chainData.type !== "Testnet"
? chainData.network || chainData.symbol
: "ETH";
let env = this.env;
let fiatPrice = await new PaySvr().queryTokenPrice(network, tokenName, env);
let priceFloat = parseFloat(fiatPrice) * 100; let priceFloat = parseFloat(fiatPrice) * 100;
return priceFloat * parseFloat(amount); return priceFloat * parseFloat(amount);
} }

View File

@ -1,6 +1,6 @@
import { import {
queryEthUsdPrice,
queryFiatList, queryFiatList,
queryTokenUsdPrice,
reqAlchemyPrePay, reqAlchemyPrePay,
} from "../api/PayApi"; } from "../api/PayApi";
import { singleton } from "../decorator/singleton.decorator"; import { singleton } from "../decorator/singleton.decorator";
@ -46,28 +46,36 @@ export class PaySvr {
return this.priceMap.get(key); return this.priceMap.get(key);
} }
private async updateTokenPrice(chain: string, tokenName: string) { private async updateTokenPrice(
const key = `crypto_usd_${chain}_${tokenName}`; chain: string,
let priceData = await queryEthUsdPrice(tokenName, chain); tokenName: string,
env: string
) {
const key = `crypto_usd_${chain}_${tokenName}_${env}`;
let priceData = await queryTokenUsdPrice(tokenName, chain, env);
console.log("ETH price data", JSON.stringify(priceData)); console.log("ETH price data", JSON.stringify(priceData));
let price = priceData.data.price; let price = priceData.data.price;
this.priceMap.set(key, price + ""); this.priceMap.set(key, price + "");
} }
public async queryTokenPrice(chain: string, tokenName: string) { public async queryTokenPrice(
chain: string,
tokenName: string,
env: string = "release"
) {
chain = chain.toUpperCase(); chain = chain.toUpperCase();
tokenName = tokenName.toUpperCase(); tokenName = tokenName.toUpperCase();
const key = `crypto_usd_${chain}_${tokenName}`; const key = `crypto_usd_${chain}_${tokenName}_${env}`;
if (!this.priceMap.has(key)) { if (!this.priceMap.has(key)) {
try { try {
await this.updateTokenPrice(chain, tokenName); await this.updateTokenPrice(chain, tokenName, env);
} catch (err) { } catch (err) {
console.log("ETH price err", err); console.log("ETH price err", err);
} }
} else { } else {
setImmediate(async () => { setImmediate(async () => {
try { try {
await this.updateTokenPrice(chain, tokenName); await this.updateTokenPrice(chain, tokenName, env);
} catch (err) { } catch (err) {
console.log("ETH price err", err); console.log("ETH price err", err);
} }

View File

@ -209,7 +209,7 @@ export class ERC1155Standard {
if (!gas) { if (!gas) {
gas = await contract.methods gas = await contract.methods
.safeBatchTransferFrom(from, to, tokenIds, amounts, []) .safeBatchTransferFrom(from, to, tokenIds, amounts, [])
.estimateGas({ gas: 1000000 }); .estimateGas();
} }
gas = (gas * 1.1) | 1; gas = (gas * 1.1) | 1;
if (estimate) { if (estimate) {

View File

@ -159,9 +159,7 @@ export class ERC20Standard {
const contract = new this.web3.eth.Contract(abiERC20, address); const contract = new this.web3.eth.Contract(abiERC20, address);
const amountBN = Web3.utils.toBN(Web3.utils.toWei(amount + "")); const amountBN = Web3.utils.toBN(Web3.utils.toWei(amount + ""));
if (!gas) { if (!gas) {
gas = await contract.methods gas = await contract.methods.transfer(to, "0").estimateGas();
.transfer(to, amountBN)
.estimateGas({ gas: 1000000 });
} }
gas = (gas * 1.1) | 1; gas = (gas * 1.1) | 1;
if (estimate) { if (estimate) {

View File

@ -306,7 +306,7 @@ export class ERC721Standard {
if (!gas) { if (!gas) {
gas = await contract.methods gas = await contract.methods
.safeTransferFrom(from, to, tokenId) .safeTransferFrom(from, to, tokenId)
.estimateGas({ gas: 1000000 }); .estimateGas();
} }
gas = (gas * 1.1) | 1; gas = (gas * 1.1) | 1;
if (estimate) { if (estimate) {

View File

@ -38,7 +38,7 @@ export class JCStandard {
try { try {
gas = await contract.methods gas = await contract.methods
.buy721NFT(addresses, values, signature) .buy721NFT(addresses, values, signature)
.estimateGas({ gas: SAMPLE_GAS }); .estimateGas();
} catch (err) {} } catch (err) {}
gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS;
} }
@ -115,7 +115,7 @@ export class JCStandard {
try { try {
gas = await contract.methods gas = await contract.methods
.buy1155NFT(addresses, values, ids, amounts, signature) .buy1155NFT(addresses, values, ids, amounts, signature)
.estimateGas({ gas: SAMPLE_GAS }); .estimateGas();
} catch (err) {} } catch (err) {}
gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS;
} }
@ -197,7 +197,7 @@ export class JCStandard {
try { try {
gas = await contract.methods gas = await contract.methods
.evolve721NFT(nftAddress, tokenIds, startTime, nonce, signature) .evolve721NFT(nftAddress, tokenIds, startTime, nonce, signature)
.estimateGas({ gas: SAMPLE_GAS }); .estimateGas();
} catch (err) {} } catch (err) {}
gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS;
} }
@ -272,7 +272,7 @@ export class JCStandard {
try { try {
gas = await contract.methods gas = await contract.methods
.evolveChip(tokenIds, startTime, nonce, signature) .evolveChip(tokenIds, startTime, nonce, signature)
.estimateGas({ gas: SAMPLE_GAS }); .estimateGas();
} catch (err) {} } catch (err) {}
gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS;
} }
@ -356,7 +356,7 @@ export class JCStandard {
try { try {
gas = await contract.methods gas = await contract.methods
.mintShardBatchUser(tokenIds, amounts, startTime, nonce, signature) .mintShardBatchUser(tokenIds, amounts, startTime, nonce, signature)
.estimateGas({ gas: SAMPLE_GAS }); .estimateGas();
} catch (err) {} } catch (err) {}
gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS;
} }
@ -450,7 +450,7 @@ export class JCStandard {
nonce, nonce,
signature signature
) )
.estimateGas({ gas: SAMPLE_GAS }); .estimateGas();
} catch (err) {} } catch (err) {}
gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS; gas = gas ? (gas * 1.1) | 1 : SAMPLE_GAS;
} }
@ -565,10 +565,10 @@ export class JCStandard {
try { try {
gas1 = await chipInstance.methods gas1 = await chipInstance.methods
.setApprovalForAll(lockerAddress, true) .setApprovalForAll(lockerAddress, true)
.estimateGas({ gas: SAMPLE_GAS }); .estimateGas();
gas0 = await contract.methods gas0 = await contract.methods
.pluginChip(addresses, values, chipIds, slots, signature) .pluginChip(addresses, values, chipIds, slots, signature)
.estimateGas({ gas: SAMPLE_GAS }); .estimateGas();
} catch (err) {} } catch (err) {}
if (!gas) { if (!gas) {
gas1 = gas1 ? (gas1 * 1.1) | 1 : SAMPLE_GAS; gas1 = gas1 ? (gas1 * 1.1) | 1 : SAMPLE_GAS;
@ -663,7 +663,7 @@ export class JCStandard {
try { try {
gas = await contract.methods gas = await contract.methods
.unplugChip(addresses, values, chipIds, slots, signature) .unplugChip(addresses, values, chipIds, slots, signature)
.estimateGas({ gas: 1000000 }); .estimateGas();
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }

View File

@ -3,6 +3,7 @@ export interface IChainData {
type: string; type: string;
rpc: string; rpc: string;
id: number; id: number;
network?: string;
symbol: string; symbol: string;
explorerurl: string; explorerurl: string;
decimals?: number; decimals?: number;