微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 web3Provider 中调用合约方法并使用它发送值?

如何解决如何在 web3Provider 中调用合约方法并使用它发送值?

我使用 walletconnect/Metamask 作为钱包,使用 web3Provider 作为我的“购买代币”功能的提供者。

我正在尝试使用 populateTransaction.METHOD_NAME 从我的钱包到合约方法(使用 BNB 转账)进行交易。

我不确定自己做错了什么,我已经搞砸了 6 个多小时,但我无法弄清楚。 :/

我正在尝试通过调用合约“BuyTokensForBnb”函数来将一些 BNB 转移到合约中(就像我们可以使用 Remix IDE 来做到这一点。) 但我一直遇到错误:/

IMAGE ERROR

 const purchasetokens2 = async () => {
const HODLER = await new ethers.Contract(address,abi,web3providerState);
const tx = [{
  nonce: "0x00",// ignored by MetaMask
  to: HODLER.address,// required except during contract publications. (LINK CONTRACT ADDRESS MAINNET/TESTNET)
  value: "0xde0b6b3a7640000",// Only required to send ether to the recipient from the initiating external account.
  data: '',// Optional,but used for defining smart contract creation and interaction.
  chainId: "0x61" // Used to prevent transaction reuse across blockchains. Auto filled by MetaMask.
}];
let resp = HODLER.populateTransaction.buyTokensForBnb(0,tx)
const signer = await web3providerState.getSigner();
console.log("Non signer",web3providerState);
console.log("Signer,",signer);
console.log("Account:",await signer.getAddress());

let tx2 = await signer.sendTransaction(tx);
let signature = await signer.signMessage("Hello world");
console.log(tx2,signature);
}

BuyTokensForBnb 函数

   function buyTokensForBnb() public payable {
    require(msg.value > minbuy,"MINIMUM PURCHASE IS 0.01 BNB");
    require(msg.value <= maxbuy,"MAX BUY IS 10 BNB");
    require(getPresaleState() == 5 || getPresaleState() == 4,"PRESALE IS NOT RUNNING(Could BE CANCELLED OR ENDED,OR FINALIZED)");
    
    if(block.timestamp > endDate || block.timestamp < startDate) {
          revert("You can't buy this token yet or Presale had already ended,tokens cannot be purchased any longer.");
    } else {
        if(getPresaleState() == 4) {
            //WHITELISTED PRESALE
            PresaleWallet storage presaleWallet = presaleWallets[msg.sender];
            if(presaleWallet.whitelisted == false) {
                if(block.timestamp >= whitelistedEndDate) {
                    presaleState = PresaleState.LIVE;
                    generateReceiptAndUserPurchasedTokens(msg.sender,msg.value);
                } else {
                  revert('refundED,YOUR WALLET IS NOT WHITELISTED');   
                }
            } else {
                //make purchase,update tokens.
                generateReceiptAndUserPurchasedTokens(msg.sender,msg.value);
            }
        } else if(getPresaleState() == 5) {
            generateReceiptAndUserPurchasedTokens(msg.sender,msg.value);
        } else {
            revert('refundED,PRESALE IS NOT LIVE OR IN WHITELISTED MODE');
        }
    }
}

任何帮助将不胜感激!

问候丹尼尔:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?