ethereum - 错误 : Returned error: The method eth_sendTransaction does not exist

标签 ethereum web3 web3js

我正在尝试使用以下代码将 erc20token 从契约(Contract)地址转移到 eth 地址:

                var _from = "from Address";
                var contAddress = "contract address";
                var _to = "to address";

                var _Amount = '50';
                var txnObject = {
                    "from":_from,
                    "to": _to,
                    "value": web3.utils.toWei(_Amount,'ether'),
                    // "gas": 21000,         (optional)
                    // "gasPrice": 4500000,  (optional)
                    // "data": 'For testing' (optional)
                    // "nonce": 10           (optional)
                }
            
                web3.eth.sendTransaction(txnObject, function(error, result){
                    if(error){
                        console.log( "Transaction error" ,error);
                    }
                    else{
                        var txn_hash = result; //Get transaction hash
                        //$('#Tx').text(txn_hash);
                        alert(txn_hash);
                    }
                });
但我收到此错误:
Transaction error Error: Returned error: The method eth_sendTransaction does not exist/is not available
我已经搜索了很多并尝试了这段代码,但没有奏效。可能是什么原因?这段代码是错误的还是别的什么?
注意:我使用的是 infura ropsten 网络节点。

最佳答案

除了错误消息外,与您的问题相关的其他问题很少。

让我们从错误消息开始。

The method eth_sendTransaction does not exist/is not available

eth_sendTransaction()当您希望您的节点为您签署交易(使用 ulocked 帐户)时使用。但是您连接到不支持此方法的节点。很可能是 Infura 或其他第三方节点提供商,它们不为您持有您帐户的私钥。
因此,您需要在您的应用程序上签署交易,然后将签署的交易广播到您的节点。有几种方法可以实现这一点。
例如,您可以使用 web3.eth.signTransaction() 对 tx 进行签名。 ,然后使用 web3.eth.sendSignedTransaction()将(签名的)tx 提交到您的节点。
或者您可以使用 accounts.wallet.add()方法,然后是 Contract 实例 send()方法 - 请参阅答案的中间部分。

您的代码段创建了常规的 ETH 转移 - 它不会创建 token 转移。
您可以使用 Web3 Contract帮助您构建传输 ERC-20 代币的交易的方法。
// this is one of the other ways to sign a transaction on your end
web3.eth.accounts.wallet.add(privateKeyToTheSenderAddress);

// for this case, you can use a generic ERC-20 ABI JSON interface
const myContract = new web3.eth.Contract(jsonInterface, contractAddress);

// invoke the `transfer()` method on the contract
// sign it using the private key corresponding to the `senderAddress`
// and broadcast the signed tx to your node
myContract.methods.transfer(toAddress, amount).send({from: senderAddress});

我不确定这是否只是问题的措辞不正确,还是逻辑错误。但是你不能转让属于合约的代币,除非合约代码允许你这样做。
为什么?因为你需要发件人的私钥才能转移他们的代币。这是因为transfer()方法通常是这样构建的:
function transfer(address _to, uint256 _amount) external returns (bool) {
    balances[msg.sender] -= _amount; // decreases balance of the transaction sender
    // ...
}
交易发送方需要使用他们的私钥对交易进行签名。如果您希望发送方成为合约(以减少合约的代币余额),则需要拥有其私钥。你没有的(实际上不可能知道合约的私钥)。
但是,如果交易发件人是授权地址,则您的契约(Contract)可能允许减少其余额。例子:
function withdrawTokensFromContract(uint256 _amount) external {
    require(msg.sender == address(Ox123), 'Not authorized');
    balances[address(this)] -= _amount; // decreases balance of the contract
    // ...
}
在这种情况下,如果您使用属于授权地址的私钥签署了交易,则可以减少合约的代币余额。
或者这只是一个错误的措辞,你只是想从 Address A 转移代币(存储在合约地址中)至 Address B .在这种情况下,您可以安全地使用 transfer()答案中间部分描述的方法,并使用属于Address A的私钥对交易进行签名。 .

关于ethereum - 错误 : Returned error: The method eth_sendTransaction does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68236469/

相关文章:

blockchain - 如何在 Solidity 中返回映射列表? (以太坊合约)

javascript - Solidity 和 web3 sha3

node.js - 如何从 Nodejs 文件调用 Web3js 函数

javascript - web3.eth.accounts[0] 返回未定义和 app.vote(1,{ from :web3. eth.accounts[0] }) 给出错误

node.js - 今天用 web3.js 发送以太坊不工作(结果是失败)

ethereum - Solidity 中的地址(1) 是什么?

javascript - 使用 web3 在 MetaMask 中避免 "This gas fee has been suggested by"消息

javascript - 如何在记录之前等待 json 返回值(返回至少需要 30 秒)? JavaScript/ react /表达

bytecode - 以太坊字节码 JUMP 和 JUMPDEST 是如何解决的?

node.js - 如何获得 BEP20 代币的价格?