Skip to content

Commit

Permalink
C1.0 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Nov 18, 2024
1 parent 85f0562 commit 6f4694d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
33 changes: 18 additions & 15 deletions cadence/contract.cdc
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
//A vault resource to be stored in an account that tracks your balance of tokens
pub resource Vault: Provider, Receiver, Balance {
// A vault resource to be stored in an account that tracks your balance of tokens
access(all)
resource Vault: Provider, Receiver, Balance {

// keeps track of the total balance of the account's tokens
pub var balance: UFix64
// Keeps track of the total balance of the account's tokens
access(all)
var balance: UFix64

// initialize the balance at resource creation time
// Initialize the balance at resource creation time
init(balance: UFix64) {
self.balance = balance
}

// withdraw
// Withdraw
//
// Function that takes an integer amount as an argument
// Function that takes an amount as an argument
// and withdraws that amount from the Vault.
//
// It creates a new temporary Vault that is used to hold
// the money that is being transferred. It returns the newly
// created Vault to the context that called so it can be deposited
// the tokens being transferred. It returns the newly
// created Vault to the context that called it so it can be deposited
// elsewhere.
//
pub fun withdraw(amount: UFix64): @Vault {
access(all)
fun withdraw(amount: UFix64): @Vault {
self.balance = self.balance - amount
return <-create Vault(balance: amount)
}

// deposit
// Deposit
//
// Function that takes a Vault object as an argument and adds
// its balance to the balance of the owners Vault.
// its balance to the balance of the owner's Vault.
//
// It is allowed to destroy the sent Vault because the Vault
// was a temporary holder of the tokens. The Vault's balance has
// been consumed and therefore can be destroyed.
pub fun deposit(from: @Vault) {
access(all)
fun deposit(from: @Vault) {
self.balance = self.balance + from.balance
destroy from
}
}
}
37 changes: 20 additions & 17 deletions cadence/transaction.cdc
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
// Setup Account
import ExampleToken from 0x01

// This transaction configures an account to store and receive tokens defined by
// the ExampleToken contract.
transaction {
prepare(acct: AuthAccount) {
// Create a new empty Vault object
let vaultA <- ExampleToken.createEmptyVault()
// Store the vault in the account storage
acct.save<@ExampleToken.Vault>(<-vaultA, to: /storage/MainVault)
prepare(acct: auth(Storage, Capabilities) &Account) {
// Create a new empty Vault object
let vaultA <- ExampleToken.createEmptyVault()
// Store the vault in the account storage
acct.storage.save<@ExampleToken.Vault>(<-vaultA, to: /storage/MainVault)

log("Empty Vault stored")
log("Empty Vault stored")

// Create a public Receiver capability to the Vault
let ReceiverRef = acct.link<&ExampleToken.Vault{ExampleToken.Receiver, ExampleToken.Balance}>(/public/MainReceiver, target: /storage/MainVault)
// Create a public Receiver capability to the Vault
let receiverCap = acct.capabilities.storage.issue<&ExampleToken.Vault{ExampleToken.Receiver, ExampleToken.Balance}>(
/storage/MainVault
)
acct.capabilities.publish(receiverCap, at: /public/MainReceiver)

log("References created")
}
log("References created")
}

post {
// Check that the capabilities were created correctly
getAccount(0x02).getCapability<&ExampleToken.Vault{ExampleToken.Receiver}>(/public/MainReceiver)
.check():
"Vault Receiver Reference was not created correctly"
assert(
getAccount(0x02)
.capabilities
.borrow<&ExampleToken.Vault{ExampleToken.Receiver}>(/public/MainReceiver) != nil,
message: "Vault Receiver Reference was not created correctly"
)
}
}

4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export const tokenVault = {
transactionCode: transactionPath,
transactionExplanation: transactionExplanationPath,
filters: {
difficulty: "beginner"
}
difficulty: "beginner",
},
};

0 comments on commit 6f4694d

Please sign in to comment.