Smart Contract
Escrow
A.0e964e9e2b53ed06.Escrow
1import NonFungibleToken from 0x1d7e57aa55817448
2import FungibleToken from 0xf233dcee88fe0abe
3import FlowToken from 0x1654653399040a61
4import FlowTransactionScheduler from 0xe467b9dd11fa00df
5
6
7access(all)
8contract Escrow {
9
10 access(self) var handlerId: UInt64
11 access(self) let address: Address
12
13 access(all) entitlement Owner
14
15
16 // -----------------------------------------------------------------------
17 /// Handler resource that implements the Scheduled Transaction interface
18 access(all) resource Handler: FlowTransactionScheduler.TransactionHandler {
19
20 // RECEIPTS
21 ///./////.////
22 access(all) let handlerId: UInt64
23 access(all) let receiver: Address
24 access(self) var offerVault: @FlowToken.Vault
25 access(self) var receipts: @[FlowTransactionScheduler.ScheduledTransaction]
26
27 access(FlowTransactionScheduler.Execute) fun executeTransaction(id: UInt64, data: AnyStruct?) {
28 pre {
29 self.offerVault.balance > 0.0: "Offer vault is empty"
30 }
31 let balance <- self.offerVault.withdraw(amount: self.offerVault.balance) as! @FlowToken.Vault
32 let owner = getAccount(self.owner!.address)
33 let vault = owner.capabilities.borrow<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)!.deposit(from: <- balance.withdraw(amount: balance.balance))
34 destroy balance
35
36 }
37
38 // Function to withdraw funds from the offer vault
39 // this is called when the offer is accepted
40 access(Owner) fun withdrawFunds(): @FlowToken.Vault {
41 pre {
42 self.offerVault.balance > 0.0: "Offer vault is empty"
43 }
44 let balance <- self.offerVault.withdraw(amount: self.offerVault.balance) as! @FlowToken.Vault
45 return <- balance
46 }
47 // deposit receipt
48 access(all) fun depositReceipt(receipt: @FlowTransactionScheduler.ScheduledTransaction) {
49 self.receipts.append(<- receipt)
50 }
51
52 init(
53 offerVault: @FlowToken.Vault,
54 receiver: Address,
55 ) {
56 // increment the handler id
57 Escrow.handlerId = Escrow.handlerId + 1
58 // set the handler id
59 self.handlerId = Escrow.handlerId
60 self.offerVault <- offerVault
61 self.receipts <- []
62 self.receiver = receiver
63/* let inboxIdentifier = "\(receiver)_Escrow_Handler_\(self.handlerId)"
64 // Get a cap to this handler resource
65 let handlerCap = Escrow.account.capabilities.storage.issue<auth(Owner) &Handler>(Escrow.getHandlerStoragePath(self.handlerId))
66 // Pubish an authorized capability to the inbox
67 Escrow.account.inbox.publish(handlerCap, name: inboxIdentifier, recipient: receiver) */
68
69 }
70 }
71
72 // Public helper functions
73 // function to get storage path for a handler
74 // based on the handler id
75 access(all) view fun getHandlerStoragePath(_ handlerId: UInt64): StoragePath {
76 return StoragePath(identifier: "\(Escrow.account.address)_Escrow_Handler_\(handlerId)")!
77 }
78
79 // function to create a new handler
80 access(all) fun createHandler(offerVault: @FlowToken.Vault, receiver: Address): @Handler {
81 return <- create Handler(offerVault: <- offerVault, receiver: receiver)
82 }
83 // function to get the address of the escrow
84 access(all) view fun getAddress(): Address {
85 return self.address
86 }
87
88 init() {
89 self.handlerId = 0
90 self.address = self.account.address
91 }
92}