@flowtyio/flow-contracts 0.0.11 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,62 @@
1
+ import "FungibleToken"
2
+ import "FlowStorageFees"
3
+ import "FlowToken"
4
+
5
+ /*
6
+ FeeEstimator
7
+
8
+ Small contract that allows other contracts to estimate how much storage cost a resource might take up.
9
+ This is done by storing a resource in the FeeEstimator, recording the difference in available balance,
10
+ then returning the difference and the original item being estimated.
11
+
12
+ Consumers of this contract would then need to pop the resource out of the DepositEstimate resource to get it back
13
+ */
14
+ pub contract FeeEstimator {
15
+ pub resource DepositEstimate {
16
+ pub var item: @AnyResource?
17
+ pub var storageFee: UFix64
18
+
19
+ init(item: @AnyResource, storageFee: UFix64) {
20
+ self.item <- item
21
+ self.storageFee = storageFee
22
+ }
23
+
24
+ pub fun withdraw(): @AnyResource {
25
+ let resource <- self.item <- nil
26
+ return <-resource!
27
+ }
28
+
29
+ destroy() {
30
+ pre {
31
+ self.item == nil: "cannot destroy with non-null item"
32
+ }
33
+
34
+ destroy self.item
35
+ }
36
+ }
37
+
38
+ pub fun hasStorageCapacity(_ addr: Address, _ storageFee: UFix64): Bool {
39
+ return FlowStorageFees.defaultTokenAvailableBalance(addr) > storageFee
40
+ }
41
+
42
+ pub fun estimateDeposit(
43
+ item: @AnyResource,
44
+ ): @DepositEstimate {
45
+ let storageUsedBefore = FeeEstimator.account.storageUsed
46
+ FeeEstimator.account.save(<-item, to: /storage/temp)
47
+ let storageUsedAfter = FeeEstimator.account.storageUsed
48
+
49
+ let storageDiff = storageUsedAfter - storageUsedBefore
50
+ let storageFee = FeeEstimator.storageUsedToFlowAmount(storageDiff)
51
+ let loadedItem <- FeeEstimator.account.load<@AnyResource>(from: /storage/temp)!
52
+ let estimate <- create DepositEstimate(item: <-loadedItem, storageFee: storageFee)
53
+ return <- estimate
54
+ }
55
+
56
+ pub fun storageUsedToFlowAmount(_ storageUsed: UInt64): UFix64 {
57
+ let storageMB = FlowStorageFees.convertUInt64StorageBytesToUFix64Megabytes(storageUsed)
58
+ return FlowStorageFees.storageCapacityToFlow(storageMB)
59
+ }
60
+
61
+ init() {}
62
+ }