Smart Contract
monsoonBuyToken
A.8234007b36f8113c.monsoonBuyToken
1/**
2* SPDX-License-Identifier: UNLICENSED
3*/
4
5
6import FungibleToken from 0xf233dcee88fe0abe
7
8pub contract monsoonBuyToken: FungibleToken {
9 // TokensInitialized
10 //
11 // The event that is emitted when the contract is created
12 pub event TokensInitialized(initialSupply: UFix64)
13
14 // TokensWithdrawn
15 //
16 // The event that is emitted when tokens are withdrawn from a Vault
17 pub event TokensWithdrawn(amount: UFix64, from: Address?)
18
19 // TokensDeposited
20 //
21 // The event that is emitted when tokens are deposited to a Vault
22 pub event TokensDeposited(amount: UFix64, to: Address?)
23
24 // TokensMinted
25 //
26 // The event that is emitted when new tokens are minted
27 pub event TokensMinted(amount: UFix64)
28
29 // TokensBurned
30 //
31 // The event that is emitted when tokens are destroyed
32 pub event TokensBurned(amount: UFix64)
33
34 // MinterCreated
35 //
36 // The event that is emitted when a new minter resource is created
37 pub event MinterCreated(allowedAmount: UFix64)
38
39 // Named paths
40 //
41 pub let VaultStoragePath: StoragePath
42 pub let ReceiverPublicPath: PublicPath
43 pub let BalancePublicPath: PublicPath
44 pub let AdminStoragePath: StoragePath
45
46 // Total supply of monsoonBuyTokens in existence
47 pub var totalSupply: UFix64
48
49 // Vault
50 //
51 // Each user stores an instance of only the Vault in their storage
52 // The functions in the Vault and governed by the pre and post conditions
53 // in FungibleToken when they are called.
54 // The checks happen at runtime whenever a function is called.
55 //
56 // Resources can only be created in the context of the contract that they
57 // are defined in, so there is no way for a malicious user to create Vaults
58 // out of thin air. A special Minter resource needs to be defined to mint
59 // new tokens.
60 //
61 pub resource Vault: FungibleToken.Provider, FungibleToken.Receiver, FungibleToken.Balance {
62
63 // The total balance of this vault
64 pub var balance: UFix64
65
66 // initialize the balance at resource creation time
67 init(balance: UFix64) {
68 self.balance = balance
69 }
70
71 // withdraw
72 //
73 // Function that takes an amount as an argument
74 // and withdraws that amount from the Vault.
75 //
76 // It creates a new temporary Vault that is used to hold
77 // the money that is being transferred. It returns the newly
78 // created Vault to the context that called so it can be deposited
79 // elsewhere.
80 //
81 pub fun withdraw(amount: UFix64): @FungibleToken.Vault {
82 self.balance = self.balance - amount
83 emit TokensWithdrawn(amount: amount, from: self.owner?.address)
84 return <-create Vault(balance: amount)
85 }
86
87 // deposit
88 //
89 // Function that takes a Vault object as an argument and adds
90 // its balance to the balance of the owners Vault.
91 //
92 // It is allowed to destroy the sent Vault because the Vault
93 // was a temporary holder of the tokens. The Vault's balance has
94 // been consumed and therefore can be destroyed.
95 //
96 pub fun deposit(from: @FungibleToken.Vault) {
97 let vault <- from as! @monsoonBuyToken.Vault
98 self.balance = self.balance + vault.balance
99 emit TokensDeposited(amount: vault.balance, to: self.owner?.address)
100 vault.balance = 0.0
101 destroy vault
102 }
103
104 destroy() {
105 monsoonBuyToken.totalSupply = monsoonBuyToken.totalSupply - self.balance
106 if(self.balance > 0.0) {
107 emit TokensBurned(amount: self.balance)
108 }
109 }
110 }
111
112 // createEmptyVault
113 //
114 // Function that creates a new Vault with a balance of zero
115 // and returns it to the calling context. A user must call this function
116 // and store the returned Vault in their storage in order to allow their
117 // account to be able to receive deposits of this token type.
118 //
119 pub fun createEmptyVault(): @Vault {
120 return <-create Vault(balance: 0.0)
121 }
122
123 pub resource Administrator {
124
125 // createNewMinter
126 //
127 // Function that creates and returns a new minter resource
128 //
129 pub fun createNewMinter(allowedAmount: UFix64): @Minter {
130 emit MinterCreated(allowedAmount: allowedAmount)
131 return <-create Minter(allowedAmount: allowedAmount)
132 }
133 }
134
135 // Minter
136 //
137 // Resource object that token admin accounts can hold to mint new tokens.
138 //
139 pub resource Minter {
140
141 // The amount of tokens that the minter is allowed to mint
142 pub var allowedAmount: UFix64
143
144 // mintTokens
145 //
146 // Function that mints new tokens, adds them to the total supply,
147 // and returns them to the calling context.
148 //
149 pub fun mintTokens(amount: UFix64): @monsoonBuyToken.Vault {
150 pre {
151 amount > 0.0: "Amount minted must be greater than zero"
152 amount <= self.allowedAmount: "Amount minted must be less than the allowed amount"
153 }
154 monsoonBuyToken.totalSupply = monsoonBuyToken.totalSupply + amount
155 self.allowedAmount = self.allowedAmount - amount
156 emit TokensMinted(amount: amount)
157 return <-create Vault(balance: amount)
158 }
159
160 init(allowedAmount: UFix64) {
161 self.allowedAmount = allowedAmount
162 }
163 }
164
165 init() {
166 // Set our named paths.
167
168 self.VaultStoragePath = /storage/monsoonBuyTokenVault
169 self.ReceiverPublicPath = /public/monsoonBuyTokenReceiver
170 self.BalancePublicPath = /public/monsoonBuyTokenBalance
171 self.AdminStoragePath = /storage/monsoonBuyTokenAdmin
172
173 // Initialize contract state.
174 self.totalSupply = 0.0
175
176 // Create the one true Admin object and deposit it into the conttract account.
177 let admin <- create Administrator()
178 self.account.save(<-admin, to: self.AdminStoragePath)
179
180 // Emit an event that shows that the contract was initialized.
181 emit TokensInitialized(initialSupply: self.totalSupply)
182 }
183}
184