请问 getData('transferFrom', $params); 里面的$params参数要怎么传入?
hhun opened this issue · 1 comments
hhun commented
我运行以下代码的结果是 由$ownerAddress账号转移原生代币到$toAddress,而不是由$fromAddress转移指定代币到$toAddress。
请问想要实现 由$fromAddress转移指定代币到$toAddress要怎么写?
$fromAddress已经授权了$ownerAddress指定代币
运行以下代码结果为交易类型为EVM转账
而没有执行合约调用
<?php
$ownerAddress = '';
$ownerPrivateKey = '';
$fromAddress = '';
$toAddress = '';
$amount = '1';
$web3 = new Web3('https://exchainrpc.okex.org/');
$eth = $web3->getEth();
$nonce = 0;
$eth->getTransactionCount($ownerAddress, function ($err, $count) use (&$nonce) {
if ($err !== null) {
throw new Exception($err->getMessage());
}
$nonce = $count;
});
$contract = new Contract($eth->getProvider(), $abi);
$contractInstance = $contract->at('0xaa27badaa3c9ec9081b8f6c9cdd2bf375f143780');
// 函数数据
$functionData = $contractInstance->getData('transferFrom', $fromAddress, $toAddress, $amount);
$rawTx = [
'nonce' => '0x' . $nonce->toHex(),
'from' => $ownerAddress,
'to' => $toAddress,
'value' => '0x0',
'gas' => '0x' . $estimatedGas->toHex(),
'gasPrice' => '0x' . $gasPrice->toHex(),
'chainId' => 1,
'data' => '0x' . $functionData
];
$transaction = new Transaction($rawTx);
// 离线签名
$signedTransaction = $transaction->sign($ownerPrivateKey);
// 广播交易
$transactionHash = '';
$eth->sendRawTransaction('0x' . $signedTransaction, function ($err, $tx) use (&$transactionHash) {
if ($err !== null) {
throw new Exception($err->getMessage());
}
$transactionHash = $tx;
});
echo $transactionHash;
@sc0Vu @1099511627776 @amateescu @sinabs @Arul- @MariusMez @samyan
hhun commented
问题已经解决,将$rawTx
里的to
设置为合约地址,执行就是触发合约了。