@@ -21,6 +21,7 @@ package templates
2121import (
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// ```
132135func 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.
180183func 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