-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85f0562
commit 6f4694d
Showing
3 changed files
with
40 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters