web3 使用额外参数调用 Solidity 合约函数?
web3 calls Solidity contract function with extra parameter?
在http://truffleframework.com/tutorials/pet-shop例子中,有如下合约,函数adopt(uint petId)
只有一个参数
contract Adoption {
address[16] public adopters;
function adopt(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
function getAdopters() public returns (address[16]) {
return adopters;
}
}
然而,在javascript代码app.js
中,handleAdopt
函数使用以下代码调用合约函数。
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
return adoptionInstance.adopt(petId, {from: account});
})
使用额外对象 {from: account}
调用该函数。为什么?这个参数在solidity代码中被丢弃了吗?
顺便说一句,有一个未定义的全局变量web3
?该值是由 MetaMask 扩展分配的吗?
那是 transactionObject
,它描述了所有交易调用的一般信息(gas limit、价格、要发送的以太币数量等)。您发布的 JS 代码使用的是 web3 库。那不是直接调用合约 API。 web3 库将其转换为 RPC。 transactionObject
位于所有合约参数之后。之后还有另一个参数,它是带有合约调用结果的回调(参见 here)。
这些是 docs 中描述的 transactionobject
的所有选项:
- from: String - The address for the sending account. Uses the web3.eth.defaultAccount property, if not specified.
- to: String - (optional) The destination address of the message, left undefined for a contract-creation transaction.
- value: Number|String|BigNumber - (optional) The value transferred for the transaction in Wei, also the endowment if it's a contract-creation transaction.
- gas: Number|String|BigNumber - (optional, default: To-Be-Determined) The amount of gas to use for the transaction (unused gas is refunded).
- gasPrice: Number|String|BigNumber - (optional, default: To-Be-Determined) The price of gas for this transaction in wei, defaults to the mean network gas price.
- data: String - (optional) Either a byte string containing the associated data of the message, or in the case of a contract-creation transaction, the initialisation code.
- nonce: Number - (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.
在http://truffleframework.com/tutorials/pet-shop例子中,有如下合约,函数adopt(uint petId)
只有一个参数
contract Adoption {
address[16] public adopters;
function adopt(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
function getAdopters() public returns (address[16]) {
return adopters;
}
}
然而,在javascript代码app.js
中,handleAdopt
函数使用以下代码调用合约函数。
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
return adoptionInstance.adopt(petId, {from: account});
})
使用额外对象 {from: account}
调用该函数。为什么?这个参数在solidity代码中被丢弃了吗?
顺便说一句,有一个未定义的全局变量web3
?该值是由 MetaMask 扩展分配的吗?
那是 transactionObject
,它描述了所有交易调用的一般信息(gas limit、价格、要发送的以太币数量等)。您发布的 JS 代码使用的是 web3 库。那不是直接调用合约 API。 web3 库将其转换为 RPC。 transactionObject
位于所有合约参数之后。之后还有另一个参数,它是带有合约调用结果的回调(参见 here)。
这些是 docs 中描述的 transactionobject
的所有选项:
- from: String - The address for the sending account. Uses the web3.eth.defaultAccount property, if not specified.
- to: String - (optional) The destination address of the message, left undefined for a contract-creation transaction.
- value: Number|String|BigNumber - (optional) The value transferred for the transaction in Wei, also the endowment if it's a contract-creation transaction.
- gas: Number|String|BigNumber - (optional, default: To-Be-Determined) The amount of gas to use for the transaction (unused gas is refunded).
- gasPrice: Number|String|BigNumber - (optional, default: To-Be-Determined) The price of gas for this transaction in wei, defaults to the mean network gas price.
- data: String - (optional) Either a byte string containing the associated data of the message, or in the case of a contract-creation transaction, the initialisation code.
- nonce: Number - (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.