Skip to content

Commit

Permalink
Add LostAndFound contracts (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkline authored Oct 28, 2023
1 parent bf131d5 commit 4eac83d
Show file tree
Hide file tree
Showing 6 changed files with 893 additions and 2 deletions.
62 changes: 62 additions & 0 deletions contracts/lost-and-found/FeeEstimator.cdc
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() {}
}
Loading

0 comments on commit 4eac83d

Please sign in to comment.