移除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);
}
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`;
return POST_JSON(url, { token: eth, chain });
return POST_JSON(url, { token: eth, chain, env });
}
export function queryFiatList() {

View File

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

View File

@ -212,9 +212,19 @@ export const AllChains = [
type: "Mainnet",
rpc: "https://rpc.ankr.com/arbitrum",
id: 42161,
network: "ARBITRUM",
symbol: "ETH",
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",
type: "Mainnet",
@ -255,14 +265,7 @@ export const AllChains = [
symbol: "MATIC",
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",
type: "Mainnet",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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