/*
事件是使用EVM日志内置功能的方便工具,在DAPP的接口中,它可以反过来调用Javascript的监听事件的回调。
事件在合约中可被继承。当被调用时,会触发参数存储到交易的日志中(一种区块链上的特殊数据结构)。
这些日志与合约的地址关联,并合并到区块链中,只要区块可以访问就一直存在
*/
myContract.once(event[, options], callback) //单次订阅合约事件
myContract.events.MyEvent([options][, callback]) //订阅合约事件
myContract.events.allEvents([options][, callback]) //订阅合约全部事件
myContract.getPastEvents(event[, options][, callback]) //读取合约历史事件
options - Object: 可选,用于部署的选项,包含以下字段:
filter - Object : 可选,按索引参数过滤事件。例如 {filter: {myNumber: [12,13]}} 表示 “myNumber” 为12或13的所有事件
fromBlock - Number: 可选,仅监听该选项指定编号的块中发生的事件
topics - Array : 可选,用来手动为事件过滤器设定主题。如果设置过filter属性和事件签名,那么(topic[0])将不会自动设置
callback - Function: 可选,该回调函数触发时,其第二给参数为事件对象,第一个参数为错误对象
底层的日志接口(Low-level Interface to Logs)
通过函数log0,log1,log2,log3,log4,可以直接访问底层的日志组件。logi表示总共有带i + 1个参数
log3(
msg.value,
0x50cb9fe53daa9737b786ab3646f04d0150dc50ef4e75f59509d83667ad5adb20,
msg.sender,
_id
);
调用:
myContract.deploy(options)
参数:
options - Object: 用于部署的配置选项,包含以下字段:
data - String: 合约的字节码
arguments - Array : 可选,在部署时将传入合约的构造函数
返回值:
Object: 交易对象,包含以下字段:
arguments: Array - 之前传入方法的参数,可修改
send: Function - 用来部署合约,其返回的promise对象将解析为新的合约实例,而非交易收据!
estimateGas: Function - 用来估算用于部署的gas用量
encodeABI: Function - 用来编码部署的ABI数据,即合约数据 + 构造函数参数
myContract.deploy({
data: '0x12345...',
arguments: [123, 'My String']
})
.send({
from: '0x1234567890123456789012345678901234567891',
gas: 1500000,
gasPrice: '30000000000000'
}, function(error, transactionHash){ ... })
.on('error', function(error){ ... })
.on('transactionHash', function(transactionHash){ ... })
.on('receipt', function(receipt){
console.log(receipt.contractAddress) // 收据中包含了新的合约地址
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // 新地址的合约实例
});
// data是合约自身的一个可选配置项
myContract.options.data = '0x12345...';
myContract.deploy({
arguments: [123, 'My String']
})
.send({
from: '0x1234567890123456789012345678901234567891',
gas: 1500000,
gasPrice: '30000000000000'
})
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // instance with the new contract address
});
// 编码
myContract.deploy({
data: '0x12345...',
arguments: [123, 'My String']
})
.encodeABI();
> '0x12345...0000012345678765432'
// 估算gas
myContract.deploy({
data: '0x12345...',
arguments: [123, 'My String']
})
.estimateGas(function(err, gas){
console.log(gas);
});
/*
new web3.eth.Contract(jsonInterface[, address][, options])
参数:
jsonInterface(abi) - Object: 要实例化的合约的json接口
address - String: 可选,要调用的合约的地址,也可以在之后使用 myContract.options.address = '0x1234..' 来指定该地址
options - Object : 可选,合约的配置对象,其中某些字段用作调用和交易的回调:
from - String: 交易发送方地址
gasPrice - String: 用于交易的gas价格,单位:wei
gas - Number: 交易可用的最大gas量,即gas limit
data - String: 合约的字节码,部署合约时需要
*/
truffle(develop)> tokenContract.options
{ address: [Getter/Setter], jsonInterface: [Getter/Setter] }
truffle(develop)> tokenContract.options.jsonInterface[1]
{ constant: false,
inputs:
[ { name: '_from', type: 'address' },
{ name: '_to', type: 'address' },
{ name: '_value', type: 'uint256' } ],
name: 'transferFrom',
outputs: [ { name: '', type: 'bool' } ],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
signature: '0x23b872dd' }