Smart Contract
ExampleConnectors
A.6daee039a7b9c2f0.ExampleConnectors
1// Contract: ExampleConnectors
2// Purpose: Minimal example of a DeFiActions Sink implementation that accepts any
3// fungible token vault type and deposits it into a provided vault capability.
4//
5// Concepts demonstrated:
6// - Implementing DeFiActions.Sink with type-safe deposits
7// - Using a capability to a fungible vault for deposits
8// - Exposing component metadata and optional UniqueIdentifier wiring
9//
10// Safety:
11// - depositCapacity enforces type equality with a precondition
12// - Withdrawals are sized by callers via minimumCapacity() or DeFiActions patterns
13import FungibleToken from 0xf233dcee88fe0abe
14import DeFiActions from 0x92195d814edf9cb0
15
16access(all) contract ExampleConnectors {
17 // TokenSink: A simple Sink that deposits everything it receives
18 // NOTE: In practice, this should not be used as it is essentially a duplicate
19 // of the standard FungibleTokenConnectors.VaultSink connector.
20 access(all) struct TokenSink: DeFiActions.Sink {
21 // Capability to a receiver that can accept withdrawals of the matching vault type
22 access(contract) let vault: Capability<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>
23 // Optional tracing ID used by DeFiActions to correlate flows
24 access(contract) var uniqueID: DeFiActions.UniqueIdentifier?
25
26 init(
27 vault: Capability<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>,
28 uniqueID: DeFiActions.UniqueIdentifier?
29 ) {
30 self.vault = vault
31 self.uniqueID = uniqueID
32 }
33
34 // Required by Sink: advertise the exact deposit type supported
35 access(all) view fun getSinkType(): Type {
36 return self.vault.borrow()!.getType()
37 }
38
39 // This sink places no limit on deposits; callers may size with their own rules
40 access(all) fun minimumCapacity(): UFix64 {
41 return UFix64.max
42 }
43
44 // Deposit the full balance from the provided vault into the target vault
45 access(all) fun depositCapacity(from: auth(FungibleToken.Withdraw) &{FungibleToken.Vault}) {
46 pre {
47 // Enforce exact type match between provided vault and sink type
48 from.getType() == self.getSinkType():
49 "Invalid vault provided for deposit - \(from.getType().identifier) is not \(self.getSinkType().identifier)"
50 }
51 // No-op for empty transfers
52 let amount: UFix64 = from.balance
53 if amount == 0.0 { return }
54 // Move all funds and deposit
55 let payment <- from.withdraw(amount: amount)
56 self.vault.borrow()!.deposit(from: <-payment)
57 }
58
59 // Report metadata about this component for DeFiActions graph inspection
60 access(all) fun getComponentInfo(): DeFiActions.ComponentInfo {
61 return DeFiActions.ComponentInfo(
62 type: self.getType(),
63 id: self.id(),
64 innerComponents: []
65 )
66 }
67
68 // Implementation detail for UniqueIdentifier passthrough
69 access(contract) view fun copyID(): DeFiActions.UniqueIdentifier? {
70 return self.uniqueID
71 }
72
73 // Allow the framework to set/propagate a UniqueIdentifier for tracing
74 access(contract) fun setID(_ id: DeFiActions.UniqueIdentifier?) {
75 self.uniqueID = id
76 }
77 }
78}