Smart Contract
MotoGPTransfer
A.a49cc0ee46c54bfb.MotoGPTransfer
1import FlowToken from 0x1654653399040a61
2import MotoGPAdmin from 0xa49cc0ee46c54bfb
3import MotoGPPack from 0xa49cc0ee46c54bfb
4import MotoGPCard from 0xa49cc0ee46c54bfb
5import NonFungibleToken from 0x1d7e57aa55817448
6import FungibleToken from 0xf233dcee88fe0abe
7import FlowStorageFees from 0xe467b9dd11fa00df
8import ContractVersion from 0xa49cc0ee46c54bfb
9import PackOpener from 0xa49cc0ee46c54bfb
10
11// Contract for topping up an account's storage capacity when it receives a MotoGP pack or card
12//
13pub contract MotoGPTransfer: ContractVersion {
14
15 pub fun getVersion(): String {
16 return "0.7.8"
17 }
18
19 // The minium amount to top up
20 //
21 access(account) var minFlowTopUp:UFix64
22
23 // The maximum amount to top up
24 //
25 access(account) var maxFlowTopUp:UFix64
26
27 // Vault where the admin stores Flow tokens to pay for top-ups
28 //
29 access(self) var flowVault: @FlowToken.Vault
30
31 access(self) var isPaused: Bool //for future use
32
33 // Transfers packs from one collection to another, with storage top-up if needed
34 //
35 pub fun transferPacks(fromCollection: @MotoGPPack.Collection{NonFungibleToken.Provider, MotoGPPack.IPackCollectionPublic}, toCollection: &MotoGPPack.Collection{MotoGPPack.IPackCollectionPublic}, toAddress: Address) {
36
37 pre {
38 fromCollection.getIDs().length > 0 : "No packs in fromCollection"
39 }
40
41 for id in fromCollection.getIDs() {
42 toCollection.deposit(token: <- fromCollection.withdraw(withdrawID: id))
43 }
44
45 self.topUp(toAddress)
46
47 destroy fromCollection
48 }
49
50 // Transfer cards from one collection to another, with storage top-up if needed
51 //
52 pub fun transferCards(fromCollection: @MotoGPCard.Collection{NonFungibleToken.Provider, MotoGPCard.ICardCollectionPublic}, toCollection: &MotoGPCard.Collection{MotoGPCard.ICardCollectionPublic}, toAddress: Address) {
53 pre {
54 fromCollection.getIDs().length > 0 : "No cards in fromCollection"
55 }
56
57 for id in fromCollection.getIDs() {
58 toCollection.deposit(token: <- fromCollection.withdraw(withdrawID: id))
59 }
60
61 self.topUp(toAddress)
62
63 destroy fromCollection
64 }
65
66 // Transfer a pack to a Pack opener collection, with storage top-up if needed
67 pub fun transferPackToPackOpenerCollection(pack: @MotoGPPack.NFT, toCollection: &PackOpener.Collection{PackOpener.IPackOpenerPublic}, toAddress: Address) {
68 toCollection.deposit(token: <- pack)
69 self.topUp(toAddress)
70 }
71
72 // Admin-controlled method for use in transactions where admin wants to do top-up, e.g. open packs
73 //
74 pub fun topUpFlowForAccount(adminRef: &MotoGPAdmin.Admin, toAddress: Address){
75 pre {
76 adminRef != nil : "AdminRef is nil"
77 }
78 self.topUp(toAddress)
79 }
80
81 // Core logic for topping up an account
82 //
83 access(self) fun topUp(_ toAddress:Address){
84
85 post {
86 (before(self.flowVault.balance) - self.flowVault.balance) <= self.maxFlowTopUp : "Top up exceeds max top up"
87 }
88
89 let toAccount = getAccount(toAddress)
90
91 if (toAccount.storageCapacity < toAccount.storageUsed){
92 let topUpAmount:UFix64 = self.flowForStorage(toAccount.storageUsed - toAccount.storageCapacity)
93 let toVault = toAccount.getCapability(/public/flowTokenReceiver).borrow<&FlowToken.Vault{FungibleToken.Receiver}>()!
94 toVault.deposit(from: <- self.flowVault.withdraw(amount: topUpAmount))
95 }
96 }
97
98 // Converts storage bytes to a FLOW token amount
99 //
100 access(self) fun flowForStorage(_ storage:UInt64): UFix64 {
101 return FlowStorageFees.storageCapacityToFlow(FlowStorageFees.convertUInt64StorageBytesToUFix64Megabytes(storage))
102 }
103
104 pub fun setMinFlowTopUp(adminRef: &MotoGPAdmin.Admin, amount: UFix64){
105 pre {
106 adminRef != nil: "adminRef is nil"
107 }
108 self.minFlowTopUp = amount
109 }
110
111 pub fun setMaxFlowTopUp(adminRef: &MotoGPAdmin.Admin, amount: UFix64){
112 pre {
113 adminRef != nil: "adminRef is nil"
114 }
115 self.maxFlowTopUp = amount
116 }
117
118 pub fun getFlowBalance(): UFix64 {
119 return self.flowVault.balance
120 }
121
122 pub fun depositFlow(from: @FungibleToken.Vault){
123 let vault <- from as! @FlowToken.Vault
124 self.flowVault.deposit(from: <- vault)
125 }
126
127 pub fun withdrawFlow(adminRef: &MotoGPAdmin.Admin, amount: UFix64): @FungibleToken.Vault{
128 pre {
129 adminRef != nil: "adminRef is nil"
130 }
131 return <- self.flowVault.withdraw(amount: amount)
132 }
133
134 init(){
135 self.flowVault <- FlowToken.createEmptyVault() as! @FlowToken.Vault
136 self.minFlowTopUp = 0.0
137 self.maxFlowTopUp = 0.1
138 self.isPaused = false //for future use
139 }
140
141}
142