Go-Ethereum

如何使用非互動模式部署合約?

  • March 27, 2019

我有一份用 remix 編寫的契約,但我正在嘗試使用 Geth 的非互動模式使用geth attach ipc:<path> -exec "<contract deploy info>".

當我處於互動模式時,我可以簡單地將 Remix 中的 web3Deploy 欄位粘貼(沒有換行符)到控制台並創建契約。但是當我嘗試在上面輸入相同的資訊時,我會收到無效 ipc 路徑的錯誤。

(Geth attach 可以在沒有執行契約資訊的情況下工作,所以我知道 ipc 路徑是正確的)

這是我正在使用的 web3Deploy 資訊

var adderContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"newStateNum","type":"int256"}],"name":"changeNum","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":false,"inputs":[{"name":"newName","type":"string"}],"name":"changeName","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":false,"inputs":[{"name":"numToAdd","type":"int256"}],"name":"addToTotal","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":true,"inputs":[],"name":"getNum","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function","stateMutability":"view"}]);
var adder = adderContract.new(
  {
    from: web3.eth.accounts[0], 
    data: '0x6060604052341561000c57fe5b5b61039e8061001c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632f42b8ec1461005c5780635353a2d81461009057806355da0f901461017157806367e0badb146101a5575bfe5b341561006457fe5b61007a60048080359060200190919050506101cb565b6040518082815260200191505060405180910390f35b341561009857fe5b6100e8600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101db565b6040518080602001828103825283818151815260200191508051906020019080838360008314610137575b80518252602083111561013757602082019150602081019050602083039250610113565b505050905090810190601f1680156101635780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561017957fe5b61018f600480803590602001909190505061029a565b6040518082815260200191505060405180910390f35b34156101ad57fe5b6101b56102ae565b6040518082815260200191505060405180910390f35b600081600181905590505b919050565b6101e36102b9565b81600090805190602001906101f99291906102cd565b8054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561028d5780601f106102625761010080835404028352916020019161028d565b820191906000526020600020905b81548152906001019060200180831161027057829003601f168201915b505050505090505b919050565b60008160015401600181905590505b919050565b600060015490505b90565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061030e57805160ff191683800117855561033c565b8280016001018555821561033c579182015b8281111561033b578251825591602001919060010190610320565b5b509050610349919061034d565b5090565b61036f91905b8082111561036b576000816000905550600101610353565b5090565b905600a165627a7a7230582033f26c61cd926713a470828b467ddd81bf625ada1026f21c901b0d5c22257a5f0029', 
    gas: '4700000'
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

這是我用來嘗試以非互動模式部署合約的命令

geth attach ipc:"\\.\pipe\%ethereum_home%\node1\geth.ipc" -exec "var adderContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"newStateNum","type":"int256"}],"name":"changeNum","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":false,"inputs":[{"name":"newName","type":"string"}],"name":"changeName","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":false,"inputs":[{"name":"numToAdd","type":"int256"}],"name":"addToTotal","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":true,"inputs":[],"name":"getNum","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function","stateMutability":"view"}]); var adder = adderContract.new( { from: web3.eth.accounts[0], data: '0x6060604052341561000c57fe5b5b61039e8061001c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632f42b8ec1461005c5780635353a2d81461009057806355da0f901461017157806367e0badb146101a5575bfe5b341561006457fe5b61007a60048080359060200190919050506101cb565b6040518082815260200191505060405180910390f35b341561009857fe5b6100e8600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101db565b6040518080602001828103825283818151815260200191508051906020019080838360008314610137575b80518252602083111561013757602082019150602081019050602083039250610113565b505050905090810190601f1680156101635780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561017957fe5b61018f600480803590602001909190505061029a565b6040518082815260200191505060405180910390f35b34156101ad57fe5b6101b56102ae565b6040518082815260200191505060405180910390f35b600081600181905590505b919050565b6101e36102b9565b81600090805190602001906101f99291906102cd565b8054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561028d5780601f106102625761010080835404028352916020019161028d565b820191906000526020600020905b81548152906001019060200180831161027057829003601f168201915b505050505090505b919050565b60008160015401600181905590505b919050565b600060015490505b90565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061030e57805160ff191683800117855561033c565b8280016001018555821561033c579182015b8281111561033b578251825591602001919060010190610320565b5b509050610349919061034d565b5090565b61036f91905b8082111561036b576000816000905550600101610353565b5090565b905600a165627a7a7230582033f26c61cd926713a470828b467ddd81bf625ada1026f21c901b0d5c22257a5f0029', gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } })"

命令attach和 IPC 路徑應該在選項之後,例如--exec. 所以試試

geth --exec "..." attach ipc:"\\.\pipe\%ethereum_home%\node1\geth.ipc"

請注意,我使用--exec而不是-exec. 此外,您的 javascript 程式碼包含雙引號,因此您需要用單引號替換它們。

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