Web3js

如何確定交易是否用於合約創建?

  • November 1, 2017

我想監控乙太坊並記住區塊鏈上發布的每份合約(基本上和 Etherscan 一樣)。如何使用 NodeJS 和 web3 做到這一點?如何確定是否使用了特定交易來發布新合約?

可以獲取輸入數據,但是如何分析呢?

我感興趣的不是確定該地址是否是契約地址,而是有興趣找到專門的 CONTRACT CREATION 交易,而不必將所有契約都保存在我的數據庫中。

這可以通過檢查來完成

  • 交易的“收件人”地址,創建合約時為零。
  • 交易收據中的“contractAddress”欄位。

例如,看看下面的交易,注意“to”欄位和“contractAddress”欄位。

> web3.eth.getTransaction("0xf04c0c82bd8e4733e4bcc0ac8e8becf60fad0d99e83883c34887937956a40c3b")

{
塊雜湊:“0x99efca1a0553673c039a4bdb09f529c0e4feadddf1bdd08753e42a8229774875”,
區塊編號:90913,
來自:“0x5e0320bb4d82ab8bb5d7291f2c67d1c99abb3c05”,
氣體:1000000,
氣價:54606694457,
雜湊:“0xf04c0c82bd8e4733e4bcc0ac8e8becf60fad0d99e83883c34887937956a40c3b”,
輸入: ”0x60606040526040516020806102518339016040526060805190602001505b80600060005060003373ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050819055505b506101ef806100626000396000f30060606040526000357c01000000000000000000000000000000000000000000000000000000009004806390b98a1114610044578063bbd39ac01461007157610042565b005b61005b6004803590602001803590602001506100b3565b6040518082815260200191505060405180910390f35b610082600480359060200150610098565b6040518082815260200191505060405180910390f35b60006000506020528060005260406000206000915090505481565b600081600060005060003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000505410156100f557600090506101e9565b81600060005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282825054039250508190555081600060005060008573ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505401925050819055507f16cdf1707799c6655baac6e210f52b94b7cec08adcaf9ede7dfe8649da926146338484604051808473ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a1600190506101e9565b92915050560000000000000000000000000000000000000000000000000000000000002710“
隨機數:6,
r: "0x524cd0612444308f830c542bdbbc523c5855449103951819028427b9167fd534",
s: "0x25aa71510d0ca63a1e01d6dad21cfda9755e3792adc3c7554a98c3d5d42fb1c1",
     **至:空,**
交易指數:1,
v: "0x1c",
值:0
}
> web3.eth.getTransactionReceipt("0xf04c0c82bd8e4733e4bcc0ac8e8becf60fad0d99e83883c34887937956a40c3b")
{
塊雜湊:“0x99efca1a0553673c039a4bdb09f529c0e4feadddf1bdd08753e42a8229774875”,
區塊編號:90913,
     **契約地址:“0xc4a23a06e6642fa00989b89575f0b9f783259159”,**
累計GasUsed:197451,
來自:“0x5e0320bb4d82ab8bb5d7291f2c67d1c99abb3c05”,
氣體使用:176451,
日誌:[],
logsBloom:“0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”
根:“0x76ca24aac6c47b5e49b38a227f4892b29aca17cfa273ff57c67ca4f5bd7a37d5”,
     **至:空,**
交易雜湊:“0xf04c0c82bd8e4733e4bcc0ac8e8becf60fad0d99e83883c34887937956a40c3b”,
交易指數:1
}

我們無法直接從地址判斷是否是合約地址。這就是合約地址的計算方式:乙太坊合約的地址是如何計算的?

有一個 web3.js 方法web.eth.getcode來獲取地址的乙太坊虛擬機字節碼。如果它不是契約,則該方法將返回0x0。其他任何東西都是契約。

引用自:https://ethereum.stackexchange.com/questions/29573