@@ -186,6 +186,10 @@ Store.abi Store.bin Store.go Store.sol
186
186
touch store_main.go
187
187
```
188
188
189
+ 下面的例子先部署合约,在部署过程中设置的` Store.sol ` 合约中有一个公开的名为` version ` 的全局变量,这种公开的成员将自动创建` getter ` 函数,然后调用` Version() ` 来获取version的值。
190
+
191
+ 写入智能合约需要我们用私钥来对交易事务进行签名,我们创建的智能合约有一个名为` SetItem ` 的外部方法,它接受solidity` bytes32 ` 类型的两个参数(key,value)。 这意味着在Go文件中需要传递一个长度为32个字节的字节数组。
192
+
189
193
``` go
190
194
package main
191
195
@@ -195,7 +199,7 @@ import (
195
199
196
200
" github.com/FISCO-BCOS/go-sdk/client"
197
201
" github.com/FISCO-BCOS/go-sdk/conf"
198
- " github.com/FISCO-BCOS/go-sdk/.ci/ store" // import store
202
+ " github.com/FISCO-BCOS/go-sdk/store" // import store
199
203
)
200
204
201
205
func main (){
@@ -214,47 +218,15 @@ func main(){
214
218
}
215
219
fmt.Println (" contract address: " , address.Hex ()) // the address should be saved, will use in next example
216
220
fmt.Println (" transaction hash: " , tx.Hash ().Hex ())
217
- _ = instance
218
- }
219
- ```
220
-
221
- ## 加载智能合约并调用查询接口
222
-
223
- 在部署过程中设置的` Store.sol ` 合约中有一个公开的名为` version ` 的全局变量,这种公开的成员将自动创建` getter ` 函数。下面的例子中我们加载上一步部署的合约,然后调用` Version() ` 来获取version的值。
224
-
225
- ``` go
226
- package main
227
-
228
- import (
229
- " encoding/hex"
230
- " fmt"
231
- " log"
232
-
233
- " github.com/FISCO-BCOS/go-sdk/.ci/store" // import store
234
- " github.com/FISCO-BCOS/go-sdk/client"
235
- " github.com/FISCO-BCOS/go-sdk/conf"
236
- " github.com/ethereum/go-ethereum/common"
237
- )
238
-
239
- func main () {
240
- privateKey , err := hex.DecodeString (" 145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58" )
241
- if err != nil {
242
- log.Fatalf (" decode hex failed of %v " , err)
243
- }
244
- config := &conf.Config {IsHTTP: true , ChainID: 1 , IsSMCrypto: false , GroupID: 1 ,
245
- PrivateKey: privateKey, NodeURL: " http://localhost:8545" }
246
- client , err := client.Dial (config)
247
- if err != nil {
248
- log.Fatal (err)
249
- }
250
221
251
222
// load the contract
252
- contractAddress := common.HexToAddress (" contract addree in hex" ) // get contract address from deploy
253
- instance , err := store.NewStore (contractAddress, client)
254
- if err != nil {
255
- log.Fatal (err)
256
- }
223
+ // contractAddress := common.HexToAddress("contract address in hex String")
224
+ // instance, err := store.NewStore(contractAddress, client)
225
+ // if err != nil {
226
+ // log.Fatal(err)
227
+ // }
257
228
229
+ fmt.Println (" ================================" )
258
230
storeSession := &store.StoreSession {Contract: instance, CallOpts: *client.GetCallOpts (), TransactOpts: *client.GetTransactOpts ()}
259
231
260
232
version , err := storeSession.Version ()
@@ -263,48 +235,9 @@ func main() {
263
235
}
264
236
265
237
fmt.Println (" version :" , version) // "Store deployment 1.0"
266
- }
267
-
268
- ```
269
-
270
- ## 调用智能合约写接口
271
-
272
- 写入智能合约需要我们用私钥来对交易事务进行签名,我们创建的智能合约有一个名为` SetItem ` 的外部方法,它接受solidity` bytes32 ` 类型的两个参数(key,value)。 这意味着在Go文件中需要传递一个长度为32个字节的字节数组。新建` contract_write.go ` 来测试写入智能合约:
273
-
274
- ``` go
275
- package main
276
-
277
- import (
278
- " encoding/hex"
279
- " fmt"
280
- " log"
281
-
282
- " github.com/FISCO-BCOS/go-sdk/.ci/store" // import store
283
- " github.com/FISCO-BCOS/go-sdk/client"
284
- " github.com/FISCO-BCOS/go-sdk/conf"
285
- " github.com/ethereum/go-ethereum/common"
286
- )
287
-
288
- func main () {
289
- privateKey , err := hex.DecodeString (" 145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58" )
290
- if err != nil {
291
- log.Fatalf (" decode hex failed of %v " , err)
292
- }
293
- config := &conf.Config {IsHTTP: false , ChainID: 1 , CAFile: " ca.crt" , Key: " sdk.key" , Cert: " sdk.crt" , IsSMCrypto: false , GroupID: 1 , PrivateKey: privateKey, NodeURL: " 127.0.0.1:20200" }
294
- client , err := client.Dial (config)
295
- if err != nil {
296
- log.Fatal (err)
297
- }
298
-
299
- // load the contract
300
- contractAddress := common.HexToAddress (" contract addree in hex" ) // get contract address from deploy
301
- instance , err := store.NewStore (contractAddress, client)
302
- if err != nil {
303
- log.Fatal (err)
304
- }
305
-
306
- storeSession := &store.StoreSession {Contract: instance, CallOpts: *client.GetCallOpts (), TransactOpts: *client.GetTransactOpts ()}
307
238
239
+ // contract write interface demo
240
+ fmt.Println (" ================================" )
308
241
key := [32 ]byte {}
309
242
value := [32 ]byte {}
310
243
copy (key[:], []byte (" foo" ))
@@ -324,7 +257,6 @@ func main() {
324
257
log.Fatal (err)
325
258
}
326
259
327
- fmt.Println (string (result[:])) // "bar"
260
+ fmt.Println (" get item: " + string (result[:])) // "bar"
328
261
}
329
-
330
262
```
0 commit comments