Skip to content

Commit 2b0b443

Browse files
authored
Merge pull request #346 from onflow/improvement/create-fund
Create and fund account
2 parents e3aee95 + 0f5cd24 commit 2b0b443

3 files changed

Lines changed: 68 additions & 15 deletions

File tree

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ go 1.18
55
require (
66
cloud.google.com/go/kms v1.4.0
77
github.com/ethereum/go-ethereum v1.9.13
8-
github.com/onflow/cadence v0.34.0
9-
github.com/onflow/flow-go/crypto v0.24.3
8+
github.com/onflow/cadence v0.31.3
9+
github.com/onflow/flow-go/crypto v0.24.4
1010
github.com/onflow/flow/protobuf/go/flow v0.3.1
11-
github.com/onflow/sdks v0.4.4
11+
github.com/onflow/sdks v0.5.0
1212
github.com/pkg/errors v0.9.1
1313
github.com/stretchr/testify v1.7.5
1414
google.golang.org/api v0.70.0

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,14 @@ github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXW
286286
github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
287287
github.com/onflow/atree v0.4.0 h1:+TbNisavAkukAKhgQ4plWnvR9o5+SkwPIsi3jaeAqKs=
288288
github.com/onflow/atree v0.4.0/go.mod h1:7Qe1xaW0YewvouLXrugzMFUYXNoRQ8MT/UsVAWx1Ndo=
289-
github.com/onflow/cadence v0.34.0 h1:e8qtl73snt7ZTmOtzird9+tdLXGWqJ00RhIlVxlN53U=
290-
github.com/onflow/cadence v0.34.0/go.mod h1:hhktaaXlJmxnfLgH2HG0cftcUWScdfjO/CTZkzaom/g=
291-
github.com/onflow/flow-go/crypto v0.24.3 h1:5puosmiy853m1GPmBLJr4PiLVcCzE4n5o60hRPo9kYA=
292-
github.com/onflow/flow-go/crypto v0.24.3/go.mod h1:dkVL98P6GHR48iD9zCB6XlnkJX8IQd00FKgt1reV90w=
289+
github.com/onflow/cadence v0.31.3 h1:lTHTBnoFi/ahGk1cQ9JAC+tfYir4kjRVIqtTVCjbe6E=
290+
github.com/onflow/cadence v0.31.3/go.mod h1:oRgWkvau1RH15m3NuDlZCPHFQzwvC72jEstCGu8OJ98=
291+
github.com/onflow/flow-go/crypto v0.24.4 h1:SwEtoVS2TidCIHYCZMgQ7U2YsqhI9upnw94fhdHTubM=
292+
github.com/onflow/flow-go/crypto v0.24.4/go.mod h1:dkVL98P6GHR48iD9zCB6XlnkJX8IQd00FKgt1reV90w=
293293
github.com/onflow/flow/protobuf/go/flow v0.3.1 h1:4I8ykG6naR3n8Or6eXrZDaGVaoztb3gP2KJ6XKyDufg=
294294
github.com/onflow/flow/protobuf/go/flow v0.3.1/go.mod h1:gQxYqCfkI8lpnKsmIjwtN2mV/N2PIwc1I+RUK4HPIc8=
295-
github.com/onflow/sdks v0.4.4 h1:aJPGJJLAN+mlBWAQxsyuJXeRRMFeLwU6Mp4e/YL6bdU=
296-
github.com/onflow/sdks v0.4.4/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU=
295+
github.com/onflow/sdks v0.5.0 h1:2HCRibwqDaQ1c9oUApnkZtEAhWiNY2GTpRD5+ftdkN8=
296+
github.com/onflow/sdks v0.5.0/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU=
297297
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
298298
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
299299
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=

templates/accounts.go

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package templates
2121
import (
2222
"encoding/hex"
2323
"fmt"
24+
"strings"
2425

2526
"github.com/onflow/cadence"
2627
jsoncdc "github.com/onflow/cadence/encoding/json"
@@ -123,11 +124,13 @@ func newPublicKeyValue(pubKey crypto.PublicKey) (cadence.Struct, error) {
123124
//
124125
// example:
125126
// ```go
126-
// key := AccountKeyToCadenceCryptoKey(accountKey)
127+
//
128+
// key := AccountKeyToCadenceCryptoKey(accountKey)
127129
//
128130
// return flow.NewTransaction().
129131
// SetScript([]byte(templates.AddAccountKey)).
130132
// AddRawArgument(jsoncdc.MustEncode(key))
133+
//
131134
// ```
132135
func AccountKeyToCadenceCryptoKey(key *flow.AccountKey) (cadence.Value, error) {
133136
weight, _ := cadence.NewUFix64(fmt.Sprintf("%d.0", key.Weight))
@@ -178,6 +181,16 @@ func AccountKeyToCadenceCryptoKey(key *flow.AccountKey) (cadence.Value, error) {
178181
// The final argument is the address of the account that will pay the account creation fee.
179182
// This account is added as a transaction authorizer and therefore must sign the resulting transaction.
180183
func CreateAccount(accountKeys []*flow.AccountKey, contracts []Contract, payer flow.Address) (*flow.Transaction, error) {
184+
return CreateAccountAndFund(accountKeys, contracts, payer, "", "")
185+
}
186+
187+
func CreateAccountAndFund(
188+
accountKeys []*flow.AccountKey,
189+
contracts []Contract,
190+
payer flow.Address,
191+
amount string,
192+
network flow.ChainID,
193+
) (*flow.Transaction, error) {
181194
keyList := make([]cadence.Value, len(accountKeys))
182195

183196
contractKeyPairs := make([]cadence.KeyValuePair, len(contracts))
@@ -199,12 +212,52 @@ func CreateAccount(accountKeys []*flow.AccountKey, contracts []Contract, payer f
199212

200213
cadencePublicKeys := cadence.NewArray(keyList)
201214
cadenceContracts := cadence.NewDictionary(contractKeyPairs)
215+
script := templates.CreateAccount
202216

203-
return flow.NewTransaction().
204-
SetScript([]byte(templates.CreateAccount)).
205-
AddAuthorizer(payer).
206-
AddRawArgument(jsoncdc.MustEncode(cadencePublicKeys)).
207-
AddRawArgument(jsoncdc.MustEncode(cadenceContracts)), nil
217+
args := [][]byte{
218+
jsoncdc.MustEncode(cadencePublicKeys),
219+
jsoncdc.MustEncode(cadenceContracts),
220+
}
221+
222+
// if we have provided amount and network then we do funding as well
223+
if amount != "" && network == flow.Mainnet || network == flow.Testnet {
224+
script = templates.CreateAccountFunding
225+
// replace the imports on supported networks
226+
replaces := map[flow.ChainID]map[string]string{
227+
flow.Testnet: {
228+
"FlowToken": "0x7e60df042a9c0868", // https://developers.flow.com/flow/core-contracts/flow-token
229+
"FungibleToken": "0x9a0766d93b6608b7", // https://developers.flow.com/flow/core-contracts/fungible-token
230+
},
231+
flow.Mainnet: {
232+
"FlowToken": "0x1654653399040a61",
233+
"FungibleToken": "0xf233dcee88fe0abe",
234+
},
235+
}[network]
236+
237+
for contract, address := range replaces {
238+
script = strings.ReplaceAll(
239+
script,
240+
fmt.Sprintf(`"%s"`, contract),
241+
fmt.Sprintf(`%s from %s`, contract, address),
242+
)
243+
}
244+
245+
val, err := cadence.NewUFix64(amount)
246+
if err != nil {
247+
return nil, err
248+
}
249+
args = append(args, jsoncdc.MustEncode(val))
250+
}
251+
252+
tx := flow.NewTransaction().
253+
SetScript([]byte(script)).
254+
AddAuthorizer(payer)
255+
256+
for _, arg := range args {
257+
tx.AddRawArgument(arg)
258+
}
259+
260+
return tx, nil
208261
}
209262

210263
// UpdateAccountContract generates a transaction that updates a contract deployed at an account.

0 commit comments

Comments
 (0)