send - 发送合约方法交易

优质
小牛编辑
128浏览
2023-12-01

向合约发送交易来执行指定方法,将改变合约的状态。

调用:

myContract.methods.myMethod([param1[, param2[, ...]]]).send(options[, callback])

参数:

  • options - Object: 选项,包含如下字段:
    • from - String: 交易发送方地址
    • gasPrice - String : 可选,用于本次交易的gas价格,单位:wei
    • gas - Number : 可选,本次交易的gas用量上限,即gas limit
    • value - Number|String|BN|BigNumber: 可选,交易转账金额,单位:wei
  • callback - Function: 可选的回调参数,其参数为交易哈希值和错误对象

返回值:

回调函数中将返回32字节长的交易哈希值。

PromiEvent: 一个Promise对象,当交易收据有效时或者发送交易时解析为新的合约实例。 它同时也是一个事件发生器,声明有以下事件:

  • "transactionHash" 返回 String: 交易发送后得到有效交易哈希值时触发
  • "receipt" 返回 Object: 交易收据有效时触发。
  • "confirmation" 返回 Number, Object: 收到确认时触发
  • "error" 返回 Error: 交易发送过程中如果出现错误则触发此事件。对于out of gas错误,其第二个参数为交易收据

示例代码:

// using the callback
myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'}, function(error, transactionHash){
    ...
});

// using the promise
myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})
.then(function(receipt){
    // receipt can also be a new contract instance, when coming from a "contract.deploy({...}).send()"
});


// using the event emitter
myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})
.on('transactionHash', function(hash){
    ...
})
.on('confirmation', function(confirmationNumber, receipt){
    ...
})
.on('receipt', function(receipt){
    // receipt example
    console.log(receipt);
    > {
        "transactionHash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
        "transactionIndex": 0,
        "blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
        "blockNumber": 3,
        "contractAddress": "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
        "cumulativeGasUsed": 314159,
        "gasUsed": 30234,
        "events": {
            "MyEvent": {
                returnValues: {
                    myIndexedParam: 20,
                    myOtherIndexedParam: '0x123456789...',
                    myNonIndexParam: 'My String'
                },
                raw: {
                    data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
                    topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385']
                },
                event: 'MyEvent',
                signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
                logIndex: 0,
                transactionIndex: 0,
                transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
                blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
                blockNumber: 1234,
                address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
            },
            "MyOtherEvent": {
                ...
            },
            "MyMultipleEvent":[{...}, {...}] // If there are multiple of the same event, they will be in an array
        }
    }
})
.on('error', console.error); // If there's an out of gas error the second parameter is the receipt.