嗨,我是新来的聪明的合同。我试图用etherjs和nextjs编写一个简单的传输代码。基本上下面是我的代码
import {ethers} from 'ethers';
const ContractAddr = '0xB8c77482e45F1F44dE1745F52C74426C631bDD52'; /* BNB Contract address */
const ContractABI =[{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"}]
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
let numberOfTokens = ethers.utils.parseUnits('0.2', 18)
const Contract = new ethers.Contract(ContractAddr,ContractABI,signer)
const receiver = '0x8CA65A3Ce90d31a88FAa747c695183C2aefe537f';
Contract.transfer(receiver, numberOfTokens).then((transferResult) => {
console.log(transferResult)
alert("sent token")
}).catch((error) => {
console.error('error',error);
});在我的元问题上,它会按我的意愿弹出,如下所示:

但是,当我在BSCscan测试网上检查事务详细信息时,它显示0值--接收方是契约地址而不是接收方。
https://testnet.bscscan.com/tx/0x3278d9668391eee98aadf83e8ae58a0b999f2e9c3e5f47a21f504e1c04b24814
帮助。被困了一个星期,在Stackoverflow寻找类似的问题:(
发布于 2021-12-22 15:46:11
您正在将两种不同的方法混合在一起--发送ERC-20令牌和发送网络的本地货币。
在Ethereum网络上,BNB是一个ERC-20令牌(链接 -请记住您在代码中使用的0xB8c7...地址)。
然而,在BSC网络上,BNB是本地货币。因此,您使用本机事务来传输它--就像在ETH上传输ETH一样。
假设您的代码使用BSC提供程序:
const provider = new ethers.providers.Web3Provider(window.ethereum);
const params = [{
from: senderAddress,
to: '0x8CA65A3Ce90d31a88FAa747c695183C2aefe537f',
value: ethers.utils.parseUnits('0.2', 18),
}];
const transactionResult = await provider.send('eth_sendTransaction', params);https://stackoverflow.com/questions/70443433
复制相似问题