Geth的进行合约部署和调用合约方法

环境 Ubuntu20
geth : 1.10.5-stable
go: 1.17

前言

还未安装geth的读者可以参考这篇文章 Geth的安装并简单使用篇
我们需要进入geth交互式控制台进行操作

root@192-168-19-133:~# geth --dev console
INFO [10-03|22:25:29.918] Starting Geth in ephemeral dev mode...
INFO [10-03|22:25:29.920] Maximum peer count                       ETH=50 LES=0 total=50
INFO [10-03|22:25:29.920] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
INFO [10-03|22:25:29.920] Set global gas cap                       cap=50,000,000
INFO [10-03|22:25:30.075] Using developer account                  address=0x0869A17CFC7cFdD8E5d2821475f9e5B3DA837847
INFO [10-03|22:25:30.075] Allocated trie memory caches             clean=154.00MiB dirty=256.00MiB
INFO [10-03|22:25:30.075] Writing custom genesis block
INFO [10-03|22:25:30.076] Persisted trie from memory database      nodes=13 size=1.90KiB time="35.761µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [10-03|22:25:30.076] Initialised chain configuration          config="{ChainID: 1337 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: 0 Constantinople: 0 Petersburg: 0 Istanbul: 0, Muir Glacier: 0, Berlin: 0, London: 0, Engine: clique}"
INFO [10-03|22:25:30.076] Initialising Ethereum protocol           network=1337 dbversion=<nil>
INFO [10-03|22:25:30.076] Loaded most recent local header          number=0 hash=64db98..88a9f8 td=1 age=54y6mo1w
INFO [10-03|22:25:30.076] Loaded most recent local full block      number=0 hash=64db98..88a9f8 td=1 age=54y6mo1w
INFO [10-03|22:25:30.076] Loaded most recent local fast block      number=0 hash=64db98..88a9f8 td=1 age=54y6mo1w
WARN [10-03|22:25:30.076] Failed to load snapshot, regenerating    err="missing or corrupted snapshot"
INFO [10-03|22:25:30.076] Rebuilding state snapshot
INFO [10-03|22:25:30.077] Gasprice oracle is ignoring threshold set threshold=2
INFO [10-03|22:25:30.077] Resuming state snapshot generation       root=5ec2e4..313670 accounts=0 slots=0 storage=0.00B elapsed="443.529µs"
WARN [10-03|22:25:30.077] Error reading unclean shutdown markers   error="not found"
INFO [10-03|22:25:30.077] Stored checkpoint snapshot to disk       number=0 hash=64db98..88a9f8
INFO [10-03|22:25:30.077] Starting peer-to-peer node               instance=Geth/v1.10.5-stable-33ca98ec/linux-amd64/go1.17.13
INFO [10-03|22:25:30.078] Generated state snapshot                 accounts=10 slots=0 storage=412.00B elapsed=1.102ms
WARN [10-03|22:25:30.078] P2P server will be useless, neither dialing nor listening
INFO [10-03|22:25:30.082] IPC endpoint opened                      url=/tmp/geth.ipc
INFO [10-03|22:25:30.082] New local node record                    seq=1 id=e36bda3a0591e439 ip=127.0.0.1 udp=0 tcp=0
INFO [10-03|22:25:30.082] Started P2P networking                   self=enode://95e1696271cbf98961dc50be37d4c7bcea61ec3c77b5ea8d6fb0891f2204b834c0d8698e432338a3f6cd5f4af78f5456b16d1020f1a465499c88d3c995bf5865@127.0.0.1:0
INFO [10-03|22:25:30.082] Transaction pool price threshold updated price=1,000,000,000
INFO [10-03|22:25:30.082] Transaction pool price threshold updated price=1
INFO [10-03|22:25:30.082] Etherbase automatically configured       address=0x0869A17CFC7cFdD8E5d2821475f9e5B3DA837847
WARN [10-03|22:25:30.082] Failed to get free disk space            path= err="failed to call Statfs: no such file or directory"
INFO [10-03|22:25:30.082] Commit new mining work                   number=1 sealhash=5ba048..aa9c9c uncles=0 txs=0 gas=0 fees=0 elapsed="112.52µs"
INFO [10-03|22:25:30.083] Sealing paused, waiting for transactions
Welcome to the Geth JavaScript console!

instance: Geth/v1.10.5-stable-33ca98ec/linux-amd64/go1.17.13
coinbase: 0x0869a17cfc7cfdd8e5d2821475f9e5b3da837847
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
 datadir:
 modules: admin:1.0 clique:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

To exit, press ctrl-d

部署合约需要的数据

我们准备部署一个HelloWorld合约到geth上去。

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract HelloWorld{

    string public temp;


    constructor()  {
        temp =  "nihao,geth";
    }

    function set(string memory _temp) public  {
        temp = _temp;
    }

    function get() public  view returns (string memory){
        return  temp;
    }
}

但是部署合约不需要其sol文件,而是需要sol文件编译好后,产生的binabi文件。
我们可以在remix编译器进行准备这两个文件。记得写完合约按住ctrl +s ,保存并编译合约
在这里插入图片描述

在geth创建一个合约对象

首先我们需要准备好abi文件

[
	{
		"inputs": [],
		"stateMutability": "nonpayable",
		"type": "constructor"
	},
	{
		"inputs": [],
		"name": "get",
		"outputs": [
			{
				"internalType": "string",
				"name": "",
				"type": "string"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "string",
				"name": "_temp",
				"type": "string"
			}
		],
		"name": "set",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "temp",
		"outputs": [
			{
				"internalType": "string",
				"name": "",
				"type": "string"
			}
		],
		"stateMutability": "view",
		"type": "function"
	}
]

可能这个格式在geth的交互式命令行会报错【因为空格会识别导致换行】
我们可以在json解析网站进行压缩,变成一行数据
在这里插入图片描述

然后进行创建对象
web.eth.contract(abi文件)

> var helloWorld = web3.eth.contract([{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"get","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"_temp","type":"string"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"temp","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}])
undefined

回车出现undefined说明此命令成功

使用合约对象创建合约实例

这时候需要bin文件和一个合约用户,合约用户我们选择用 accounts[0]
在这里插入图片描述

然后我们使用 合约对象.new({from: 部署合约用户地址, data: bin文件, gas : 消耗的gas量}, 加一个部署成功后的回调方法)
注意,在remix复制的bin文件是十六进制的,但是它没有加前缀0x,我们进行部署的时候,需要将其加上

> var contractByHelloWorld = helloWorld.new({from: web3.eth.accounts[0] ,data:'0x60806040523480156200001157600080fd5b506040518060400160405280600a81526020017f6e6968616f2c676574680000000000000000000000000000000000000000000081525060009081620000589190620002d9565b50620003c0565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620000e157607f821691505b602082108103620000f757620000f662000099565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302620001617fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000122565b6200016d868362000122565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b6000620001ba620001b4620001ae8462000185565b6200018f565b62000185565b9050919050565b6000819050919050565b620001d68362000199565b620001ee620001e582620001c1565b8484546200012f565b825550505050565b600090565b62000205620001f6565b62000212818484620001cb565b505050565b5b818110156200023a576200022e600082620001fb565b60018101905062000218565b5050565b601f82111562000289576200025381620000fd565b6200025e8462000112565b810160208510156200026e578190505b620002866200027d8562000112565b83018262000217565b50505b505050565b600082821c905092915050565b6000620002ae600019846008026200028e565b1980831691505092915050565b6000620002c983836200029b565b9150826002028217905092915050565b620002e4826200005f565b67ffffffffffffffff8111156200030057620002ff6200006a565b5b6200030c8254620000c8565b620003198282856200023e565b600060209050601f8311600181146200035157600084156200033c578287015190505b620003488582620002bb565b865550620003b8565b601f1984166200036186620000fd565b60005b828110156200038b5784890151825560018201915060208501945060208101905062000364565b86831015620003ab5784890151620003a7601f8916826200029b565b8355505b6001600288020188555050505b505050505050565b61073380620003d06000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80634ed3885e14610046578063673402e5146100625780636d4ce63c14610080575b600080fd5b610060600480360381019061005b919061032b565b61009e565b005b61006a6100b1565b60405161007791906103f3565b60405180910390f35b61008861013f565b60405161009591906103f3565b60405180910390f35b80600090816100ad919061062b565b5050565b600080546100be90610444565b80601f01602080910402602001604051908101604052809291908181526020018280546100ea90610444565b80156101375780601f1061010c57610100808354040283529160200191610137565b820191906000526020600020905b81548152906001019060200180831161011a57829003601f168201915b505050505081565b60606000805461014e90610444565b80601f016020809104026020016040519081016040528092919081815260200182805461017a90610444565b80156101c75780601f1061019c576101008083540402835291602001916101c7565b820191906000526020600020905b8154815290600101906020018083116101aa57829003601f168201915b5050505050905090565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b610238826101ef565b810181811067ffffffffffffffff8211171561025757610256610200565b5b80604052505050565b600061026a6101d1565b9050610276828261022f565b919050565b600067ffffffffffffffff82111561029657610295610200565b5b61029f826101ef565b9050602081019050919050565b82818337600083830152505050565b60006102ce6102c98461027b565b610260565b9050828152602081018484840111156102ea576102e96101ea565b5b6102f58482856102ac565b509392505050565b600082601f830112610312576103116101e5565b5b81356103228482602086016102bb565b91505092915050565b600060208284031215610341576103406101db565b5b600082013567ffffffffffffffff81111561035f5761035e6101e0565b5b61036b848285016102fd565b91505092915050565b600081519050919050565b600082825260208201905092915050565b60005b838110156103ae578082015181840152602081019050610393565b60008484015250505050565b60006103c582610374565b6103cf818561037f565b93506103df818560208601610390565b6103e8816101ef565b840191505092915050565b6000602082019050818103600083015261040d81846103ba565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061045c57607f821691505b60208210810361046f5761046e610415565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026104d77fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8261049a565b6104e1868361049a565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b600061052861052361051e846104f9565b610503565b6104f9565b9050919050565b6000819050919050565b6105428361050d565b61055661054e8261052f565b8484546104a7565b825550505050565b600090565b61056b61055e565b610576818484610539565b505050565b5b8181101561059a5761058f600082610563565b60018101905061057c565b5050565b601f8211156105df576105b081610475565b6105b98461048a565b810160208510156105c8578190505b6105dc6105d48561048a565b83018261057b565b50505b505050565b600082821c905092915050565b6000610602600019846008026105e4565b1980831691505092915050565b600061061b83836105f1565b9150826002028217905092915050565b61063482610374565b67ffffffffffffffff81111561064d5761064c610200565b5b6106578254610444565b61066282828561059e565b600060209050601f8311600181146106955760008415610683578287015190505b61068d858261060f565b8655506106f5565b601f1984166106a386610475565b60005b828110156106cb578489015182556001820191506020850194506020810190506106a6565b868310156106e857848901516106e4601f8916826105f1565b8355505b6001600288020188555050505b50505050505056fea2646970667358221220449aa5d0303ec1aeba92eb4c743f556c8c8b8afbbce61588e7bcf9eeb0d9f1fd64736f6c63430008120033',gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } })
INFO [10-03|23:32:52.868] Setting new local account                address=0x0869A17CFC7cFdD8E5d2821475f9e5B3DA837847
INFO [10-03|23:32:52.868] Submitted contract creation              hash=0xff9564323cd840f060323931e8734266114eada7a6ea75be61dbe6d24121d5cc from=0x0869A17CFC7cFdD8E5d2821475f9e5B3DA837847 nonce=0 contract=0xE9982Bd796DFA1a9641D2724d2466593fF636C22 value=0
null [object Object]
INFO [10-03|23:32:52.869] Commit new mining work                   number=1 sealhash=f0420b..268c9c uncles=0 txs=1 gas=486,036 fees=4.86036e-13 elapsed="392.553µs"
undefined
> INFO [10-03|23:32:52.869] Successfully sealed new block            number=1 sealhash=f0420b..268c9c hash=91728b..c87c6a elapsed="395.22µs"
INFO [10-03|23:32:52.869] 🔨 mined potential block                  number=1 hash=91728b..c87c6a
INFO [10-03|23:32:52.869] Commit new mining work                   number=2 sealhash=f3fc8d..425a7f uncles=0 txs=0 gas=0       fees=0           elapsed="184.106µs"
INFO [10-03|23:32:52.869] Sealing paused, waiting for transactions
null [object Object]
Contract mined! address: 0xe9982bd796dfa1a9641d2724d2466593ff636c22 transactionHash: 0xff9564323cd840f060323931e8734266114eada7a6ea75be61dbe6d24121d5cc

这样合约就部署成功了,地址是0xE9982Bd796DFA1a9641D2724d2466593fF636C22

当然,我们不需要使用地址,因为我们创建了个合约实例contractByHelloWorld ,直接使用此就可以进行调用方法

调用合约实例的方法

set 方法 因为其方法需要交易并付gas ,所以格式是 合约实例.方法名. sendTransaction(“参数”,“参数” … , {from: 调用用户地址})

> contractByHelloWorld.set.sendTransaction("你好,geth",{from: eth.accounts[0]})
INFO [10-03|23:44:26.521] Submitted transaction                    hash=0xa37e32c323a475264a7cf231b02908e56590bc285f174255d3fef4de237fba72 from=0x0869A17CFC7cFdD8E5d2821475f9e5B3DA837847 nonce=1 recipient=0xE9982Bd796DFA1a9641D2724d2466593fF636C22 value=0
"0xa37e32c323a475264a7cf231b02908e56590bc285f174255d3fef4de237fba72"
> INFO [10-03|23:44:26.521] Commit new mining work                   number=2 sealhash=38978f..c36e7b uncles=0 txs=1 gas=28146   fees=2.8146e-14  elapsed="237.563µs"
INFO [10-03|23:44:26.521] Successfully sealed new block            number=2 sealhash=38978f..c36e7b hash=a319ef..4ae588 elapsed="385.036µs"
INFO [10-03|23:44:26.521] 🔨 mined potential block                  number=2 hash=a319ef..4ae588
INFO [10-03|23:44:26.522] Commit new mining work                   number=3 sealhash=f2bead..39aa1b uncles=0 txs=0 gas=0       fees=0           elapsed="129.179µs"
INFO [10-03|23:44:26.522] Sealing paused, waiting for transactions

get 方法 不需要交易 所以格式是 合约实例.方法名.call(“参数”,“参数”…)

> contractByHelloWorld.get.call()
"你好,geth"
>

结语

这是简单的部署合约,但是有一个问题,我们这个合约是无参构造的合约,部署的时候不需要携带参数,但是如果合约部署的时候是有参的呢 ?

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

已久依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值