Skip to content

Commit 049871f

Browse files
authored
update README.md (#88)
1 parent aab31f5 commit 049871f

File tree

1 file changed

+14
-82
lines changed

1 file changed

+14
-82
lines changed

README.md

Lines changed: 14 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ Store.abi Store.bin Store.go Store.sol
186186
touch store_main.go
187187
```
188188

189+
下面的例子先部署合约,在部署过程中设置的`Store.sol`合约中有一个公开的名为`version`的全局变量,这种公开的成员将自动创建`getter`函数,然后调用`Version()`来获取version的值。
190+
191+
写入智能合约需要我们用私钥来对交易事务进行签名,我们创建的智能合约有一个名为`SetItem`的外部方法,它接受solidity`bytes32`类型的两个参数(key,value)。 这意味着在Go文件中需要传递一个长度为32个字节的字节数组。
192+
189193
```go
190194
package main
191195

@@ -195,7 +199,7 @@ import (
195199

196200
"github.com/FISCO-BCOS/go-sdk/client"
197201
"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
199203
)
200204

201205
func main(){
@@ -214,47 +218,15 @@ func main(){
214218
}
215219
fmt.Println("contract address: ", address.Hex()) // the address should be saved, will use in next example
216220
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-
}
250221

251222
// 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+
// }
257228

229+
fmt.Println("================================")
258230
storeSession := &store.StoreSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}
259231

260232
version, err := storeSession.Version()
@@ -263,48 +235,9 @@ func main() {
263235
}
264236

265237
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()}
307238

239+
// contract write interface demo
240+
fmt.Println("================================")
308241
key := [32]byte{}
309242
value := [32]byte{}
310243
copy(key[:], []byte("foo"))
@@ -324,7 +257,6 @@ func main() {
324257
log.Fatal(err)
325258
}
326259

327-
fmt.Println(string(result[:])) // "bar"
260+
fmt.Println("get item: " + string(result[:])) // "bar"
328261
}
329-
330262
```

0 commit comments

Comments
 (0)