forked from geniesinc/flow-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
247 lines (192 loc) · 7.33 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* 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/crypto"
"github.com/onflow/flow-go-sdk/examples"
"github.com/onflow/flow-go-sdk/templates"
)
const GreatTokenContractFile = "./great-token.cdc"
func main() {
DeployContractDemo()
}
func DeployContractDemo() {
// Connect to an emulator running locally
ctx := context.Background()
flowClient, err := http.NewClient(http.EmulatorHost)
examples.Handle(err)
serviceAcctAddr, serviceAcctKey, serviceSigner := examples.ServiceAccount(flowClient)
myPrivateKey := examples.RandomPrivateKey()
myAcctKey := flow.NewAccountKey().
FromPrivateKey(myPrivateKey).
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold)
mySigner, err := crypto.NewInMemorySigner(myPrivateKey, myAcctKey.HashAlgo)
examples.Handle(err)
referenceBlockID := examples.GetReferenceBlockId(flowClient)
createAccountTx, err := templates.CreateAccount([]*flow.AccountKey{myAcctKey}, nil, serviceAcctAddr)
examples.Handle(err)
createAccountTx.SetProposalKey(
serviceAcctAddr,
serviceAcctKey.Index,
serviceAcctKey.SequenceNumber,
)
createAccountTx.SetReferenceBlockID(referenceBlockID)
createAccountTx.SetPayer(serviceAcctAddr)
err = createAccountTx.SignEnvelope(serviceAcctAddr, serviceAcctKey.Index, serviceSigner)
examples.Handle(err)
err = flowClient.SendTransaction(ctx, *createAccountTx)
examples.Handle(err)
accountCreationTxRes := examples.WaitForSeal(ctx, flowClient, createAccountTx.ID())
examples.Handle(accountCreationTxRes.Error)
// Successful Tx, increment sequence number
serviceAcctKey.SequenceNumber++
var myAddress flow.Address
for _, event := range accountCreationTxRes.Events {
if event.Type == flow.EventAccountCreated {
accountCreatedEvent := flow.AccountCreatedEvent(event)
myAddress = accountCreatedEvent.Address()
}
}
fmt.Println("My Address:", myAddress.Hex())
examples.FundAccountInEmulator(flowClient, myAddress, 100.0)
serviceAcctKey.SequenceNumber++
// Deploy the Great NFT contract
nftCode := examples.ReadFile(GreatTokenContractFile)
deployContractTx, err := templates.CreateAccount(nil,
[]templates.Contract{{
Name: "GreatToken",
Source: nftCode,
}}, serviceAcctAddr)
examples.Handle(err)
deployContractTx.SetProposalKey(
serviceAcctAddr,
serviceAcctKey.Index,
serviceAcctKey.SequenceNumber,
)
// we can set the same reference block id. We shouldn't be to far away from it
deployContractTx.SetReferenceBlockID(referenceBlockID)
deployContractTx.SetPayer(serviceAcctAddr)
err = deployContractTx.SignEnvelope(serviceAcctAddr, serviceAcctKey.Index, serviceSigner)
examples.Handle(err)
examples.Handle(err)
err = flowClient.SendTransaction(ctx, *deployContractTx)
examples.Handle(err)
deployContractTxResp := examples.WaitForSeal(ctx, flowClient, deployContractTx.ID())
examples.Handle(deployContractTxResp.Error)
// Successful Tx, increment sequence number
serviceAcctKey.SequenceNumber++
var nftAddress flow.Address
for _, event := range deployContractTxResp.Events {
if event.Type == flow.EventAccountCreated {
accountCreatedEvent := flow.AccountCreatedEvent(event)
nftAddress = accountCreatedEvent.Address()
}
}
fmt.Println("My Address:", nftAddress.Hex())
examples.FundAccountInEmulator(flowClient, nftAddress, 100.0)
serviceAcctKey.SequenceNumber++
// Next, instantiate the minter
createMinterScript := GenerateCreateMinterScript(nftAddress, 1, 2)
createMinterTx := flow.NewTransaction().
SetScript(createMinterScript).
SetProposalKey(myAddress, myAcctKey.Index, myAcctKey.SequenceNumber).
SetPayer(myAddress).
SetReferenceBlockID(referenceBlockID).
AddAuthorizer(myAddress)
err = createMinterTx.SignEnvelope(myAddress, myAcctKey.Index, mySigner)
examples.Handle(err)
err = flowClient.SendTransaction(ctx, *createMinterTx)
examples.Handle(err)
createMinterTxResp := examples.WaitForSeal(ctx, flowClient, deployContractTx.ID())
examples.Handle(createMinterTxResp.Error)
// Successful Tx, increment sequence number
myAcctKey.SequenceNumber++
mintScript := GenerateMintScript(nftAddress)
// Mint the NFT
mintTx := flow.NewTransaction().
SetScript(mintScript).
SetProposalKey(myAddress, myAcctKey.Index, myAcctKey.SequenceNumber).
SetPayer(myAddress).
SetReferenceBlockID(referenceBlockID).
AddAuthorizer(myAddress)
err = mintTx.SignEnvelope(myAddress, myAcctKey.Index, mySigner)
examples.Handle(err)
err = flowClient.SendTransaction(ctx, *mintTx)
examples.Handle(err)
mintTxResp := examples.WaitForSeal(ctx, flowClient, mintTx.ID())
examples.Handle(mintTxResp.Error)
// Successful Tx, increment sequence number
myAcctKey.SequenceNumber++
fmt.Println("NFT minted!")
result, err := flowClient.ExecuteScriptAtLatestBlock(ctx, GenerateGetNFTIDScript(nftAddress, myAddress), nil)
examples.Handle(err)
myTokenID := result.(cadence.Int)
fmt.Printf("You now own the Great NFT with ID: %d\n", myTokenID.Int())
}
// GenerateCreateMinterScript Creates a script that instantiates
// a new GreatNFTMinter instance and stores it in memory.
// Initial ID and special mod are arguments to the GreatNFTMinter constructor.
// The GreatNFTMinter must have been deployed already.
func GenerateCreateMinterScript(nftAddr flow.Address, initialID, specialMod int) []byte {
template := `
import GreatToken from 0x%s
transaction {
prepare(acct: AuthAccount) {
let minter <- GreatToken.createGreatNFTMinter(firstID: %d, specialMod: %d)
acct.save(<-minter, to: /storage/GreatNFTMinter)
}
}
`
return []byte(fmt.Sprintf(template, nftAddr, initialID, specialMod))
}
// GenerateMintScript Creates a script that mints an NFT and put it into storage.
// The minter must have been instantiated already.
func GenerateMintScript(nftCodeAddr flow.Address) []byte {
template := `
import GreatToken from 0x%s
transaction {
prepare(acct: AuthAccount) {
let minter = acct.borrow<&GreatToken.GreatNFTMinter>(from: /storage/GreatNFTMinter)!
if let nft <- acct.load<@GreatToken.GreatNFT>(from: /storage/GreatNFT) {
destroy nft
}
acct.save(<-minter.mint(), to: /storage/GreatNFT)
acct.link<&GreatToken.GreatNFT>(/public/GreatNFT, target: /storage/GreatNFT)
}
}
`
return []byte(fmt.Sprintf(template, nftCodeAddr.String()))
}
// GenerateGetNFTIDScript creates a script that retrieves an NFT from storage and returns its ID.
func GenerateGetNFTIDScript(nftCodeAddr, userAddr flow.Address) []byte {
template := `
import GreatToken from 0x%s
pub fun main(): Int {
let acct = getAccount(0x%s)
let nft = acct.getCapability(/public/GreatNFT)!.borrow<&GreatToken.GreatNFT>()!
return nft.id()
}
`
return []byte(fmt.Sprintf(template, nftCodeAddr, userAddr))
}