Skip to content

Commit

Permalink
Debug tx
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Dec 17, 2024
1 parent da8fc14 commit 5d698c0
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions cadence/transactions/withdraw_token.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,52 @@ import "ExampleToken"
// that owns a Vault
transaction {

// Temporary Vault object that holds the balance being transferred
var temporaryVault: @ExampleToken.Vault
var receiver: Capability<&{ExampleToken.Receiver}>
var receiverRef: &{ExampleToken.Receiver}
let mintingRef: &ExampleToken.VaultMinter

prepare(acct: auth(Storage, Capabilities) &Account) {
// Step 1: Check if a Vault exists, else create one
// Check if a Vault exists, else create one
if acct.storage.borrow<&ExampleToken.Vault>(from: /storage/MainVault) == nil {
let newVault <- ExampleToken.createEmptyVault()
acct.storage.save<@ExampleToken.Vault>(<-newVault, to: /storage/MainVault)
log("New Vault created and saved to storage")
}

// Step 2: Mint 50 tokens into the Vault using VaultMinter
let minterRef = acct.storage.borrow<&ExampleToken.VaultMinter>(
// Mint 50 tokens into the Vault using VaultMinter
let minter = acct.storage.borrow<&ExampleToken.VaultMinter>(
from: /storage/CadenceFungibleTokenTutorialMinter
) ?? panic("Could not borrow a reference to the VaultMinter")

let recipientCap = acct.capabilities.storage.issue<auth(ExampleToken.Receiver) &ExampleToken.Vault>(
) ?? panic("Could not borrow a reference to the minter")
self.mintingRef = minter

// Issue a Receiver capability for the caller's Vault
let receiverCap = acct.capabilities.storage.issue<&{ExampleToken.Receiver}>(
/storage/MainVault
)
minterRef.mintTokens(amount: 50.0, recipient: recipientCap)
self.receiver = receiverCap

self.mintingRef.mintTokens(amount: 30.0, recipient: self.receiver)
log("Minted 50 tokens into the Vault")

// Step 3: Borrow the Vault with Withdraw entitlement
// Borrow the Vault with Withdraw entitlement
let vaultRef = acct.storage.borrow<auth(ExampleToken.Withdraw) &ExampleToken.Vault>(
from: /storage/MainVault
) ?? panic("Could not borrow a reference to the sender's Vault")

// Step 4: Withdraw 10 tokens into the temporary Vault
// Withdraw 10 tokens into the temporary Vault
self.temporaryVault <- vaultRef.withdraw(amount: 10.0)
log("Withdrawn 10 tokens into temporary Vault")
}


execute {
// Get the recipient's public account object
let recipient = getAccount(0x01)
log("Withdrawn 10 tokens into temporary Vault")

// Borrow the recipient's Receiver capability for their Vault
let receiverRef = recipient.capabilities.get<&ExampleToken.Vault>(
/public/MainReceiver
).borrow() ?? panic("Could not borrow a Receiver reference from the recipient's Vault")
self.receiverRef = acct.storage.borrow<&{ExampleToken.Receiver}>(
from: /storage/MainVault
) ?? panic("Could not borrow a Receiver reference to the account's Vault")

// Deposit the tokens into the recipient's Vault
receiverRef.deposit(from: <-self.temporaryVault)
}

log("Transfer succeeded!")
execute {
self.receiverRef.deposit(from: <-self.temporaryVault)
log("Tokens successfully deposited!")
}
}

0 comments on commit 5d698c0

Please sign in to comment.