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