forked from geniesinc/flow-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (86 loc) · 2.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Flow Go SDK
*
* Copyright 2019 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"github.com/onflow/flow-go-sdk/access/http"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/examples"
)
func main() {
prepareDemo()
demo()
}
func demo() {
ctx := context.Background()
flowClient, err := http.NewClient(http.EmulatorHost)
examples.Handle(err)
script := []byte(`
pub fun main(a: Int): Int {
return a + 10
}
`)
args := []cadence.Value{cadence.NewInt(5)}
value, err := flowClient.ExecuteScriptAtLatestBlock(ctx, script, args)
examples.Handle(err)
fmt.Printf("\nValue: %s", value.String())
complexScript := []byte(`
pub struct User {
pub var balance: UFix64
pub var address: Address
pub var name: String
init(name: String, address: Address, balance: UFix64) {
self.name = name
self.address = address
self.balance = balance
}
}
pub fun main(name: String): User {
return User(
name: name,
address: 0x1,
balance: 10.0
)
}
`)
args = []cadence.Value{cadence.String("Dete")}
value, err = flowClient.ExecuteScriptAtLatestBlock(ctx, complexScript, args)
printComplexScript(value, err)
}
type User struct {
balance uint64
address flow.Address
name string
}
func printComplexScript(value cadence.Value, err error) {
examples.Handle(err)
fmt.Printf("\nString value: %s", value.String())
s := value.(cadence.Struct)
u := User{
balance: s.Fields[0].ToGoValue().(uint64),
address: s.Fields[1].ToGoValue().([flow.AddressLength]byte),
name: s.Fields[2].ToGoValue().(string),
}
fmt.Printf("\nName: %s", u.name)
fmt.Printf("\nAddress: %s", u.address.String())
fmt.Printf("\nBalance: %d", u.balance)
}
func prepareDemo() {
}