Smart Contract
aiSportsSwapperTransactionHandler
A.46df6b5eeec6103a.aiSportsSwapperTransactionHandler
1import FlowTransactionScheduler from 0xe467b9dd11fa00df
2import FlowTransactionSchedulerUtils from 0xe467b9dd11fa00df
3import aiSportsSwapper from 0x46df6b5eeec6103a
4import FlowToken from 0x1654653399040a61
5import FungibleToken from 0xf233dcee88fe0abe
6
7access(all)
8contract aiSportsSwapperTransactionHandler {
9 /// Handler resource that implements the Scheduled Transaction interface
10 access(all) resource Handler: FlowTransactionScheduler.TransactionHandler {
11 access(FlowTransactionScheduler.Execute) fun executeTransaction(id: UInt64, data: AnyStruct?) {
12
13 let tokenHolder = aiSportsSwapperTransactionHandler.account.address
14 aiSportsSwapper.swapToJuice()
15
16 var delay: UFix64 = 600.0
17 let future = getCurrentBlock().timestamp + delay
18 let priority = FlowTransactionScheduler.Priority.Medium
19 let executionEffort: UInt64 = 1000
20
21 let estimate = FlowTransactionScheduler.estimate(
22 data: data,
23 timestamp: future,
24 priority: priority,
25 executionEffort: executionEffort
26 )
27
28 assert(
29 estimate.timestamp != nil || priority == FlowTransactionScheduler.Priority.Low,
30 message: estimate.error ?? "estimation failed"
31 )
32
33 // Ensure a handler resource exists in the contract account storage
34 if aiSportsSwapperTransactionHandler.account.storage.borrow<&AnyResource>(from: /storage/aiSportsSwapperTransactionHandler) == nil {
35 let handler <- aiSportsSwapperTransactionHandler.createHandler()
36 aiSportsSwapperTransactionHandler.account.storage.save(<-handler, to: /storage/aiSportsSwapperTransactionHandler)
37
38 // Issue a non-entitled public capability for the handler that is publicly accessible
39 let publicCap = aiSportsSwapperTransactionHandler.account.capabilities.storage
40 .issue<&{FlowTransactionScheduler.TransactionHandler}>(/storage/aiSportsSwapperTransactionHandler)
41
42 // publish the capability
43 aiSportsSwapperTransactionHandler.account.capabilities.publish(publicCap, at: /public/aiSportsSwapperTransactionHandler)
44 }
45
46 let vaultRef = aiSportsSwapperTransactionHandler.account.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
47 ?? panic("missing FlowToken vault on contract account")
48 let feesVault <- vaultRef.withdraw(amount: estimate.flowFee ?? 0.0) as! @FlowToken.Vault
49
50 // borrow a reference to the scheduled transaction manager
51 let manager = aiSportsSwapperTransactionHandler.account.storage.borrow<auth(FlowTransactionSchedulerUtils.Owner) &{FlowTransactionSchedulerUtils.Manager}>(from: FlowTransactionSchedulerUtils.managerStoragePath)
52 ?? panic("Could not borrow a Manager reference from \(FlowTransactionSchedulerUtils.managerStoragePath)")
53
54 let handlerTypeIdentifier = manager.getHandlerTypeIdentifiers().keys[0]
55
56 manager.scheduleByHandler(
57 handlerTypeIdentifier: handlerTypeIdentifier,
58 handlerUUID: nil,
59 data: data,
60 timestamp: future,
61 priority: priority,
62 executionEffort: executionEffort,
63 fees: <-feesVault
64 )
65
66 }
67
68 access(all) view fun getViews(): [Type] {
69 return [Type<StoragePath>(), Type<PublicPath>()]
70 }
71
72 access(all) fun resolveView(_ view: Type): AnyStruct? {
73 switch view {
74 case Type<StoragePath>():
75 return /storage/aiSportsSwapperTransactionHandler
76 case Type<PublicPath>():
77 return /public/aiSportsSwapperTransactionHandler
78 default:
79 return nil
80 }
81 }
82 }
83
84
85 /// Factory for the handler resource
86 access(all) fun createHandler(): @Handler {
87 return <- create Handler()
88 }
89}
90