Skip to content

Commit

Permalink
Merge pull request #14 from onflow/improvement/create-fund
Browse files Browse the repository at this point in the history
Add create account with funding template
  • Loading branch information
sideninja authored Feb 20, 2023
2 parents c4b8c14 + 2ad61f9 commit da8e7c6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var AddContract string
//go:embed templates/cadence/create-account.cdc
var CreateAccount string

//go:embed templates/cadence/create-and-fund-account.cdc
var CreateAccountFunding string

//go:embed templates/cadence/revoke-account-key.cdc
var RemoveAccountKey string

Expand Down
36 changes: 36 additions & 0 deletions templates/cadence/create-and-fund-account.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Crypto
import "FlowToken"
import "FungibleToken"

transaction(publicKeys: [Crypto.KeyListEntry], contracts: {String: String}, fundAmount: UFix64) {
let tokenReceiver: &{FungibleToken.Receiver}
let sentVault: @FungibleToken.Vault

prepare(signer: AuthAccount) {
let account = AuthAccount(payer: signer)

// add all the keys to the account
for key in publicKeys {
account.keys.add(publicKey: key.publicKey, hashAlgorithm: key.hashAlgorithm, weight: key.weight)
}

// add contracts if provided
for contract in contracts.keys {
account.contracts.add(name: contract, code: contracts[contract]!.decodeHex())
}

self.tokenReceiver = account
.getCapability(/public/flowTokenReceiver)!
.borrow<&{FungibleToken.Receiver}>()
?? panic("Unable to borrow receiver reference")

let vaultRef = signer.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Could not borrow reference to the owner's Vault!")

self.sentVault <- vaultRef.withdraw(amount: fundAmount)
}

execute {
self.tokenReceiver.deposit(from: <-self.sentVault)
}
}

0 comments on commit da8e7c6

Please sign in to comment.