Smart Contract
SdmToken
A.c8c340cebd11f690.SdmToken
1/*
2 Description: smart Contract for DarkCountry Shadow Dime Token
3
4*/
5
6import FungibleToken from 0xf233dcee88fe0abe
7
8pub contract SdmToken: FungibleToken {
9
10 /// Total supply of SdmTokens in existence
11 pub var totalSupply: UFix64
12
13 /// TokensInitialized
14 ///
15 /// The event that is emitted when the contract is created
16 pub event TokensInitialized(initialSupply: UFix64)
17
18 /// TokensWithdrawn
19 ///
20 /// The event that is emitted when tokens are withdrawn from a Vault
21 pub event TokensWithdrawn(amount: UFix64, from: Address?)
22
23 /// TokensDeposited
24 ///
25 /// The event that is emitted when tokens are deposited to a Vault
26 pub event TokensDeposited(amount: UFix64, to: Address?)
27
28 /// TokensMinted
29 ///
30 /// The event that is emitted when new tokens are minted
31 pub event TokensMinted(amount: UFix64)
32
33 /// TokensBurned
34 ///
35 /// The event that is emitted when tokens are destroyed
36 pub event TokensBurned(amount: UFix64)
37
38 /// MinterCreated
39 ///
40 /// The event that is emitted when a new minter resource is created
41 pub event MinterCreated(allowedAmount: UFix64)
42
43 /// BurnerCreated
44 ///
45 /// The event that is emitted when a new burner resource is created
46 pub event BurnerCreated()
47
48 /// Vault
49 ///
50 /// Each user stores an instance of only the Vault in their storage
51 /// The functions in the Vault and governed by the pre and post conditions
52 /// in FungibleToken when they are called.
53 /// The checks happen at runtime whenever a function is called.
54 ///
55 /// Resources can only be created in the context of the contract that they
56 /// are defined in, so there is no way for a malicious user to create Vaults
57 /// out of thin air. A special Minter resource needs to be defined to mint
58 /// new tokens.
59 ///
60 pub resource Vault: FungibleToken.Provider, FungibleToken.Receiver, FungibleToken.Balance {
61
62 /// The total balance of this vault
63 pub var balance: UFix64
64
65 // initialize the balance at resource creation time
66 init(balance: UFix64) {
67 self.balance = balance
68 }
69
70 /// withdraw
71 ///
72 /// Function that takes an amount as an argument
73 /// and withdraws that amount from the Vault.
74 ///
75 /// It creates a new temporary Vault that is used to hold
76 /// the money that is being transferred. It returns the newly
77 /// created Vault to the context that called so it can be deposited
78 /// elsewhere.
79 ///
80 pub fun withdraw(amount: UFix64): @FungibleToken.Vault {
81 self.balance = self.balance - amount
82 emit TokensWithdrawn(amount: amount, from: self.owner?.address)
83 return <-create Vault(balance: amount)
84 }
85
86 /// deposit
87 ///
88 /// Function that takes a Vault object as an argument and adds
89 /// its balance to the balance of the owners Vault.
90 ///
91 /// It is allowed to destroy the sent Vault because the Vault
92 /// was a temporary holder of the tokens. The Vault's balance has
93 /// been consumed and therefore can be destroyed.
94 ///
95 pub fun deposit(from: @FungibleToken.Vault) {
96 let vault <- from as! @SdmToken.Vault
97 self.balance = self.balance + vault.balance
98 emit TokensDeposited(amount: vault.balance, to: self.owner?.address)
99 vault.balance = 0.0
100 destroy vault
101 }
102
103 destroy() {
104 SdmToken.totalSupply = SdmToken.totalSupply - self.balance
105 }
106 }
107
108 /// createEmptyVault
109 ///
110 /// Function that creates a new Vault with a balance of zero
111 /// and returns it to the calling context. A user must call this function
112 /// and store the returned Vault in their storage in order to allow their
113 /// account to be able to receive deposits of this token type.
114 ///
115 pub fun createEmptyVault(): @Vault {
116 return <-create Vault(balance: 0.0)
117 }
118
119 pub resource Administrator {
120
121 /// createNewMinter
122 ///
123 /// Function that creates and returns a new minter resource
124 ///
125 pub fun createNewMinter(allowedAmount: UFix64): @Minter {
126 emit MinterCreated(allowedAmount: allowedAmount)
127 return <-create Minter(allowedAmount: allowedAmount)
128 }
129
130 /// createNewBurner
131 ///
132 /// Function that creates and returns a new burner resource
133 ///
134 pub fun createNewBurner(): @Burner {
135 emit BurnerCreated()
136 return <-create Burner()
137 }
138 }
139
140 /// Minter
141 ///
142 /// Resource object that token admin accounts can hold to mint new tokens.
143 ///
144 pub resource Minter {
145
146 /// The amount of tokens that the minter is allowed to mint
147 pub var allowedAmount: UFix64
148
149 /// mintTokens
150 ///
151 /// Function that mints new tokens, adds them to the total supply,
152 /// and returns them to the calling context.
153 ///
154 pub fun mintTokens(amount: UFix64): @SdmToken.Vault {
155 pre {
156 amount > 0.0: "Amount minted must be greater than zero"
157 amount <= self.allowedAmount: "Amount minted must be less than the allowed amount"
158 }
159 SdmToken.totalSupply = SdmToken.totalSupply + amount
160 self.allowedAmount = self.allowedAmount - amount
161 emit TokensMinted(amount: amount)
162 return <-create Vault(balance: amount)
163 }
164
165 init(allowedAmount: UFix64) {
166 self.allowedAmount = allowedAmount
167 }
168 }
169
170 /// Burner
171 ///
172 /// Resource object that token admin accounts can hold to burn tokens.
173 ///
174 pub resource Burner {
175
176 /// burnTokens
177 ///
178 /// Function that destroys a Vault instance, effectively burning the tokens.
179 ///
180 /// Note: the burned tokens are automatically subtracted from the
181 /// total supply in the Vault destructor.
182 ///
183 pub fun burnTokens(from: @FungibleToken.Vault) {
184 let vault <- from as! @SdmToken.Vault
185 let amount = vault.balance
186 destroy vault
187 emit TokensBurned(amount: amount)
188 }
189 }
190
191 init() {
192 self.totalSupply = 100000000000.0
193
194 // Create the Vault with the total supply of tokens and save it in storage
195 //
196 let vault <- create Vault(balance: self.totalSupply)
197 self.account.save(<-vault, to: /storage/sdmTokenVault)
198
199 // Create a public capability to the stored Vault that only exposes
200 // the `deposit` method through the `Receiver` interface
201 //
202 self.account.link<&{FungibleToken.Receiver}>(
203 /public/sdmTokenReceiver,
204 target: /storage/sdmTokenVault
205 )
206
207 // Create a public capability to the stored Vault that only exposes
208 // the `balance` field through the `Balance` interface
209 //
210 self.account.link<&SdmToken.Vault{FungibleToken.Balance}>(
211 /public/sdmTokenBalance,
212 target: /storage/sdmTokenVault
213 )
214
215 let admin <- create Administrator()
216 self.account.save(<-admin, to: /storage/sdmTokenAdmin)
217
218 // Emit an event that shows that the contract was initialized
219 //
220 emit TokensInitialized(initialSupply: self.totalSupply)
221 }
222}