-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf131d5
commit 4eac83d
Showing
6 changed files
with
893 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import "FungibleToken" | ||
import "FlowStorageFees" | ||
import "FlowToken" | ||
|
||
/* | ||
FeeEstimator | ||
Small contract that allows other contracts to estimate how much storage cost a resource might take up. | ||
This is done by storing a resource in the FeeEstimator, recording the difference in available balance, | ||
then returning the difference and the original item being estimated. | ||
Consumers of this contract would then need to pop the resource out of the DepositEstimate resource to get it back | ||
*/ | ||
pub contract FeeEstimator { | ||
pub resource DepositEstimate { | ||
pub var item: @AnyResource? | ||
pub var storageFee: UFix64 | ||
|
||
init(item: @AnyResource, storageFee: UFix64) { | ||
self.item <- item | ||
self.storageFee = storageFee | ||
} | ||
|
||
pub fun withdraw(): @AnyResource { | ||
let resource <- self.item <- nil | ||
return <-resource! | ||
} | ||
|
||
destroy() { | ||
pre { | ||
self.item == nil: "cannot destroy with non-null item" | ||
} | ||
|
||
destroy self.item | ||
} | ||
} | ||
|
||
pub fun hasStorageCapacity(_ addr: Address, _ storageFee: UFix64): Bool { | ||
return FlowStorageFees.defaultTokenAvailableBalance(addr) > storageFee | ||
} | ||
|
||
pub fun estimateDeposit( | ||
item: @AnyResource, | ||
): @DepositEstimate { | ||
let storageUsedBefore = FeeEstimator.account.storageUsed | ||
FeeEstimator.account.save(<-item, to: /storage/temp) | ||
let storageUsedAfter = FeeEstimator.account.storageUsed | ||
|
||
let storageDiff = storageUsedAfter - storageUsedBefore | ||
let storageFee = FeeEstimator.storageUsedToFlowAmount(storageDiff) | ||
let loadedItem <- FeeEstimator.account.load<@AnyResource>(from: /storage/temp)! | ||
let estimate <- create DepositEstimate(item: <-loadedItem, storageFee: storageFee) | ||
return <- estimate | ||
} | ||
|
||
pub fun storageUsedToFlowAmount(_ storageUsed: UInt64): UFix64 { | ||
let storageMB = FlowStorageFees.convertUInt64StorageBytesToUFix64Megabytes(storageUsed) | ||
return FlowStorageFees.storageCapacityToFlow(storageMB) | ||
} | ||
|
||
init() {} | ||
} |
Oops, something went wrong.