1 以太坊浏览器上面查询智能合约创建信息
在https://etherscan.io上查询一个智能合约地址,信息显示如图所示。
其中有一条
Contract Creator: 0x0a88fb486012021369048452d7d5fbeb705da3c6at txn 0xe441928ac4300472d55a4ec2af81ec8533888c08170f4cc5ab9eab974facf200
可以查询到该合约创建者和创建的交易Hash。可是用web3.js是没有这种根据合约地址查询创建信息的API接口。所以只能采取曲线查询的方式。
2 在私链上面发布智能合约再查询
用truffle在发布智能合约:
~/mycoin$ sudo truffle migrate --reset --network test
Using network 'test'.
Running migration: 1_initial_migration.js
Replacing Operate...
... 0xba211783e05078cd81b5d46b0e442396cc49e892e4fcf42189fd3df4bf7e1b10
Operate: 0xcad15f625d1f88c8f5e8a99cb767cb4a6047cda4
Saving artifacts...
geth控制台显示:
INFO [08-11|17:32:45.124] Submitted contract creation fullhash=0xba211783e05078cd81b5d46b0e442396cc49e892e4fcf42189fd3df4bf7e1b10 contract=0xCAD15f625d1f88C8f5E8a99cB767cb4a6047cda4
INFO [08-11|17:32:45.124] contract create or call
> miner.start()
INFO [08-11|17:34:39.830] Updated mining threads threads=0
INFO [08-11|17:34:39.830] Transaction pool price threshold updated price=12000000000000
INFO [08-11|17:34:39.830] Starting mining operation
null
> INFO [08-11|17:34:39.831] contract create or call
INFO [08-11|17:34:39.831] Commit new mining work number=6 txs=1 uncles=0 elapsed=1.120ms
> miner.stopINFO [08-11|17:34:42.142] Successfully sealed new block number=6 hash=679ca4…241eec
INFO [08-11|17:34:42.142]
根据交易Hash查询交易:
> eth.getTransaction("0xba211783e05078cd81b5d46b0e442396cc49e892e4fcf42189fd3df4bf7e1b10")
{
blockHash: "0x679ca4d75b77cb78038e73bf83ab53b67f3718449f27e1d73fef977374241eec",
blockNumber: 6,
from: "0xc004fdeb4dac9827c695c672daa2afb0ed2d0779",
gas: 4712388,
gasPrice: 100000000000,
hash: "0xba211783e05078cd81b5d46b0e442396cc49e892e4fcf42189fd3df4bf7e1b10",
input: "0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506104a78061005e6000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806388d695b21461005c578063a9059cbb1461010e578063d0e30db014610168575b600080fd5b341561006757600080fd5b6100f460048080359060200190820180359060200190808060200260200160405190810160405280939291908181526020018383602002808284378201915050505050509190803590602001908201803590602001908080602002602001604051908101604052809392919081815260200183836020028082843782019150505050505091905050610172565b604051808215151515815260200191505060405180910390f35b341561011957600080fd5b61014e600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610338565b604051808215151515815260200191505060405180910390f35b610170610479565b005b60008060008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156101d257600080fd5b600085511180156101e4575083518551145b15156101ef57600080fd5b600091505b845182101561032c57838281518110151561020b57fe5b90602001906020020151905060008111801561023e5750803073ffffffffffffffffffffffffffffffffffffffff163110155b151561024657fe5b848281518110151561025457fe5b9060200190602002015173ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561029d57600080fd5b7f473edf73b107bf5d270ea55a7ea4ce98a1b5618dd196e00d5a48e101299b26d485838151811015156102cc57fe5b9060200190602002015182604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390a181806001019250506101f4565b60019250505092915050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561039557600080fd5b6000821180156103bc5750813073ffffffffffffffffffffffffffffffffffffffff163110155b15156103c457fe5b8273ffffffffffffffffffffffffffffffffffffffff166108fc839081150290604051600060405180830381858888f19350505050151561040457600080fd5b7f473edf73b107bf5d270ea55a7ea4ce98a1b5618dd196e00d5a48e101299b26d48383604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390a16001905092915050565b5600a165627a7a723058205fb39c49499010a26981f1a6be8277513c924d6944baa145319bce9a14432c490029",
nonce: 1,
r: "0x77a36a3baa4ddd0256bf5af5d584ab555ca27d652e338efa1aa005e9e1b95aa",
s: "0x2371c2c14733ed36960dacfe95c7e1fe28a8acbc328f3ac5a3ba35d886b8e5cd",
to: null,
transactionIndex: 0,
v: "0x10a",
value: 0
}
返回结果是看不到合约地址的。但是可以查到合约发起人,还有一条很关键的信息,to字段为null,说明这是一条创建智能合约的交易。根据交易Hash查询receipt:
> eth.getTransactionReceipt("0xba211783e05078cd81b5d46b0e442396cc49e892e4fcf42189fd3df4bf7e1b10")
{
blockHash: "0x679ca4d75b77cb78038e73bf83ab53b67f3718449f27e1d73fef977374241eec",
blockNumber: 6,
contractAddress: "0xcad15f625d1f88c8f5e8a99cb767cb4a6047cda4",
cumulativeGasUsed: 311742,
from: "0xc004fdeb4dac9827c695c672daa2afb0ed2d0779",
gasUsed: 311742,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
root: "0xff6ce64173777d02b14cfa6beaf59da25e680129ffbf058bbe53bdcdea4dcd59",
to: null,
transactionHash: "0xba211783e05078cd81b5d46b0e442396cc49e892e4fcf42189fd3df4bf7e1b10",
transactionIndex: 0
}
>
终于查询到合约地址了,结果与truffle端显示的一致!
3 java服务器端如何根据合约地址查询
第二步中查询的基本思路是先根据交易Hash使用eth.getTransaction(hash)查询,如果结果中的to字段为空,则说明是创建智能合约的交易,再使用eth.getTransactionRecept(hash)查询。java端使用web3j库使用transaction事件监听是监听不到truffle发起的合约创建交易的。web3j可以监听block事件,根据block查询其中的transactions字段,transactions字段包含了block中打包的所有交易,包括合约创建交易。然后将结果存入数据库中。以后直接从数据库中进行查询。