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

2023-11-02 03:04

本文主要是介绍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.0To exit, press ctrl-d

部署合约需要的数据

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

// SPDX-License-Identifier: GPL-3.0pragma 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"
>

结语

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

这篇关于Geth的进行合约部署和调用合约方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/327655

相关文章

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

Qt 中集成mqtt协议的使用方法

《Qt中集成mqtt协议的使用方法》文章介绍了如何在工程中引入qmqtt库,并通过声明一个单例类来暴露订阅到的主题数据,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一,引入qmqtt 库二,使用一,引入qmqtt 库我是将整个头文件/源文件都添加到了工程中进行编译,这样 跨平台

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在