blockchain33
Advanced tools
Comparing version 0.0.36 to 0.0.37
@@ -146,2 +146,4 @@ import "reflect-metadata"; | ||
calcFee(net: NET, token: Token, privateKey: string, amount: number, to: string): Promise<ethers.BigNumber>; | ||
private static gasPriceCache; | ||
getGasPrice(_net: NET | number): Promise<ethers.BigNumber>; | ||
getERC20Contract(token: Token): ethers.Contract; | ||
@@ -148,0 +150,0 @@ send(net: NET, token: Token, privateKey: string, amount: number, to: string, nativeBalanceBN?: ethers.BigNumber, fee?: ethers.BigNumber): Promise<ethers.providers.TransactionResponse>; |
@@ -390,3 +390,3 @@ "use strict"; | ||
const provider = this.getProvider(net, privateKey); | ||
const gasPrice = await this.getLimitter(net).schedule(() => provider.getGasPrice()); | ||
const gasPrice = await this.getGasPrice(net); | ||
const wallet = new ethers_1.ethers.Wallet(privateKey, provider); | ||
@@ -409,2 +409,20 @@ try { | ||
} | ||
async getGasPrice(_net) { | ||
const net = Number.isInteger(_net) ? this.getNet(_net) : _net; | ||
const date = new Date(); | ||
date.setMinutes(date.getMinutes() - 1); | ||
const cache = Blockchain.gasPriceCache.find(x => x.chainId === net.id); | ||
if (cache && cache.date > date) | ||
return cache.gasPrice; | ||
const provider = this.getProvider(net); | ||
const gasPrice = await this.getLimitter(net).schedule(() => provider.getGasPrice()); | ||
if (cache) { | ||
cache.gasPrice = gasPrice; | ||
cache.date = new Date(); | ||
} | ||
else { | ||
Blockchain.gasPriceCache.push({ gasPrice, date: new Date(), chainId: net.id }); | ||
} | ||
return gasPrice; | ||
} | ||
getERC20Contract(token) { | ||
@@ -416,2 +434,3 @@ return new ethers_1.ethers.Contract(token.address, erc20_1.default); | ||
const wallet = new ethers_1.ethers.Wallet(privateKey, provider); | ||
const gasPrice = await this.getGasPrice(net); | ||
if (token.address === ethers_1.ethers.constants.AddressZero) { | ||
@@ -426,3 +445,3 @@ let value = ethers_1.ethers.BigNumber.from(ethers_1.ethers.utils.parseUnits(amount.toFixed(18), net.decimals)); | ||
try { | ||
return await this.getLimitter(net).schedule(() => wallet.sendTransaction({ to, value })); | ||
return await this.getLimitter(net).schedule(() => wallet.sendTransaction({ to, value, gasPrice })); | ||
} | ||
@@ -436,3 +455,3 @@ catch (err) { | ||
try { | ||
return await this.getLimitter(net).schedule(() => this.getERC20Contract(token).connect(wallet).transfer(to, value)); | ||
return await this.getLimitter(net).schedule(() => this.getERC20Contract(token).connect(wallet).transfer(to, value, { gasPrice })); | ||
} | ||
@@ -482,41 +501,48 @@ catch (err) { | ||
const contract = this.getSwapRouterContract(net, privateKey, router); | ||
const gasPrice = await this.getGasPrice(net); | ||
if (!deadline) | ||
deadline = new Date().getTime(); | ||
return this.getLimitter(net).schedule(contract.swapETHForExactTokens(amountOut, path, to, deadline, { value })); | ||
return this.getLimitter(net).schedule(contract.swapETHForExactTokens(amountOut, path, to, deadline, { value, gasPrice })); | ||
} | ||
async swapExactETHForTokens(net, privateKey, value, amountOutMin, path, to, deadline, router = net_i_1.SwapRouterVersion.UNISWAP_V2) { | ||
const contract = this.getSwapRouterContract(net, privateKey, router); | ||
const gasPrice = await this.getGasPrice(net); | ||
if (!deadline) | ||
deadline = new Date().getTime(); | ||
return this.getLimitter(net).schedule(contract.swapExactETHForTokens(amountOutMin, path, to, deadline, { value })); | ||
return this.getLimitter(net).schedule(contract.swapExactETHForTokens(amountOutMin, path, to, deadline, { value, gasPrice })); | ||
} | ||
async swapExactTokensForTokens(net, privateKey, amountIn, amountOutMin, path, to, deadline, router = net_i_1.SwapRouterVersion.UNISWAP_V2) { | ||
const contract = this.getSwapRouterContract(net, privateKey, router); | ||
const gasPrice = await this.getGasPrice(net); | ||
if (!deadline) | ||
deadline = new Date().getTime(); | ||
return this.getLimitter(net).schedule(contract.swapExactTokensForTokens(amountIn, amountOutMin, path, to, deadline)); | ||
return this.getLimitter(net).schedule(contract.swapExactTokensForTokens(amountIn, amountOutMin, path, to, deadline, { gasPrice })); | ||
} | ||
async swapExactTokensForTokensSupportingFeeOnTransferTokens(net, privateKey, amountIn, amountOutMin, path, to, deadline, router = net_i_1.SwapRouterVersion.UNISWAP_V2) { | ||
const contract = this.getSwapRouterContract(net, privateKey, router); | ||
const gasPrice = await this.getGasPrice(net); | ||
if (!deadline) | ||
deadline = new Date().getTime(); | ||
return this.getLimitter(net).schedule(() => contract.swapExactTokensForTokensSupportingFeeOnTransferTokens(amountIn, amountOutMin, path, to, deadline)); | ||
return this.getLimitter(net).schedule(() => contract.swapExactTokensForTokensSupportingFeeOnTransferTokens(amountIn, amountOutMin, path, to, deadline, { gasPrice })); | ||
} | ||
async swapTokensForExactTokens(net, privateKey, amountOut, amountInMax, path, to, deadline, router = net_i_1.SwapRouterVersion.UNISWAP_V2) { | ||
const contract = this.getSwapRouterContract(net, privateKey, router); | ||
const gasPrice = await this.getGasPrice(net); | ||
if (!deadline) | ||
deadline = new Date().getTime(); | ||
return this.getLimitter(net).schedule(() => contract.swapTokensForExactTokens(amountOut, amountInMax, path, to, deadline)); | ||
return this.getLimitter(net).schedule(() => contract.swapTokensForExactTokens(amountOut, amountInMax, path, to, deadline, { gasPrice })); | ||
} | ||
async swapTokensForExactETH(net, privateKey, amountOut, amountInMax, path, to, deadline, router = net_i_1.SwapRouterVersion.UNISWAP_V2) { | ||
const contract = this.getSwapRouterContract(net, privateKey, router); | ||
const gasPrice = await this.getGasPrice(net); | ||
if (!deadline) | ||
deadline = new Date().getTime(); | ||
return this.getLimitter(net).schedule(contract.swapTokensForExactETH(amountOut, amountInMax, path, to, deadline)); | ||
return this.getLimitter(net).schedule(contract.swapTokensForExactETH(amountOut, amountInMax, path, to, deadline, { gasPrice })); | ||
} | ||
async swapExactTokensForETH(net, privateKey, amountIn, amountOutMin, path, to, deadline, router = net_i_1.SwapRouterVersion.UNISWAP_V2) { | ||
const contract = this.getSwapRouterContract(net, privateKey, router); | ||
const gasPrice = await this.getGasPrice(net); | ||
if (!deadline) | ||
deadline = new Date().getTime(); | ||
return this.getLimitter(net).schedule(contract.swapExactTokensForETH(amountIn, amountOutMin, path, to, deadline)); | ||
return this.getLimitter(net).schedule(contract.swapExactTokensForETH(amountIn, amountOutMin, path, to, deadline, { gasPrice })); | ||
} | ||
@@ -544,2 +570,3 @@ async getAllowance(net, tokenAddress, ownerAddress, spenderAddress) { | ||
net = net; | ||
const gasPrice = await this.getGasPrice(net); | ||
const limitter = this.getLimitter(net.id); | ||
@@ -554,3 +581,3 @@ if (net.symbol === net_i_1.Symbol.TRX) { | ||
const contract = new ethers_1.Contract(tokenAddress, erc20_1.default, wallet); | ||
return await limitter.schedule(() => contract.approve(spender, amount)); | ||
return await limitter.schedule(() => contract.approve(spender, amount, { gasPrice })); | ||
} | ||
@@ -702,2 +729,3 @@ } | ||
Blockchain.delayedCallersCache = []; | ||
Blockchain.gasPriceCache = []; | ||
__decorate([ | ||
@@ -704,0 +732,0 @@ (0, cron_1.Cron)(cron_1.Expression.EVERY_10_SECONDS), |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "0.0.36", | ||
"version": "0.0.37", | ||
"description": "utilities for All blockchain like EVM (ETH)", | ||
@@ -14,0 +14,0 @@ "main": "dist/blockchain.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
568645
5548