40 web3.php作为后端调用erc721的合约的mint函数,怎么传递参数呢

abi只需要传递一个number,就是铸币的数量,但是代码里面加了一个

payable

可以接受一个payableamont作为币价。 是直接找的测试bsc的

$contract = new Contract('https://data-seed-prebsc-1-s1.binance.org:8545')

这个函数源码

function mintYt(uint256 numberOfTokens) public payable {
require(isSaleActive, "Sale is not active");
require(numberOfTokens <= maxPerTx, "No more than 20 tokens per transaction");
require(totalSupply().add(numberOfTokens) <= maxYt, "Purchase would exceed max supply of Yt");
require(pricePerKILLA.mul(numberOfTokens) == msg.value, "Ether value is not correct");

payable(owner()).transfer(msg.value);

for (uint256 i = 0; i < numberOfTokens; i++) {
uint256 mintIndex = totalSupply();
if (totalSupply() < maxYt) {
_safeMint(msg.sender, mintIndex);
}
}
}

这是我构建的调用函数

$data = '0x' . $contract->at($contract_addr)->getData('mintYt','1');

        //设置gasPrice的数值
        $eth->gasPrice(function ($error,$results){
            //$this->error = $error;
            if($error) throw $error;
            $this->result = $results;
        });

        //获取未包含的nonce的数值,本比交易就填这个数值
        $a=$eth->getTransactionCount($from_addr,'pending',function ($error,$results){
            //$this->error = $error;
            if($error) throw $error;
            $this->result = $results;

        });
        $nonce=$this->result;
        $a=get_object_vars($nonce);
        //print_r($a);
        $num=$nonce->value;
        $b=gmp_intval($num);
        print_r($b);
        //gas费用我不知道怎么填,这个数值是随便写的
        $txParams = [
            'from' => $from_addr,
            'to' => $to_addr,
            'value' => '0x0',
            'gas' => 20000000,
            'gasPrice' =>'0x02540be400',
            'data' => $data,
            'chainId'=>'97',
            'nonce'=>1
        ];
        $transaction = new Transaction($txParams);
        //签名
        $signedTransaction = $transaction->sign($from_addr_private_key);
        // Send the transaction
        $eth->sendRawTransaction('0x'. $signedTransaction, function ($err, $tx) {
            if ($err !== null) {
                throw $err;
            }
            echo 'TX: '. $tx . PHP_EOL;
        });

然后不知道怎么接受到msg.sender这个参数,也不知道怎么构建这个mint的txparams,希望大佬告知

然后使用签名的时候,合约本身是没有私钥的啊,但是我用合约创建者的私钥,就变成了合约创建者使用一个mint 的函数,成功了但是并没有调用合约

球球大佬解惑!!!

请先 登录 后评论

最佳答案 2022-05-03 11:28

msg.sender 表示调用者地址, 如果你用外部账号私钥签名交易去调用,EVM 会解析你的账号。 如果你用合约(例如 A)去调用mintYt, 那么在mintYtmsg.sender 就是合约 A 。

mintYt 函数需要在调用的时候,支付 ETH, 因此你在构造txparams需要设置value的值。

请先 登录 后评论

其它 0 个回答

  • 1 关注
  • 0 收藏,2072 浏览
  • aimotee 提出于 2022-05-02 00:08

相似问题