Smart Contract
PrivateReceiverForwarder
A.18eb4ee6b3c026d2.PrivateReceiverForwarder
1/*
2
3# Fungible Token Private Receiver Contract
4
5This contract implements a special resource and receiver interface
6whose deposit function is only callable by an admin through a public capability.
7
8*/
9
10import FungibleToken from 0xf233dcee88fe0abe
11
12
13access(all) contract PrivateReceiverForwarder {
14
15 // Event that is emitted when tokens are deposited to the target receiver
16 access(all) event PrivateDeposit(amount: UFix64, depositedUUID: UInt64, from: Address?, to: Address?, toUUID: UInt64)
17
18 access(all) let SenderStoragePath: StoragePath
19
20 access(all) let PrivateReceiverStoragePath: StoragePath
21 access(all) let PrivateReceiverPublicPath: PublicPath
22
23 access(all) resource Forwarder {
24
25 // This is where the deposited tokens will be sent.
26 // The type indicates that it is a reference to a receiver
27 //
28 access(self) var recipient: Capability<&{FungibleToken.Receiver}>
29
30 // deposit
31 //
32 // Function that takes a Vault object as an argument and forwards
33 // it to the recipient's Vault using the stored reference
34 //
35 access(contract) fun deposit(from: @{FungibleToken.Vault}) {
36 let receiverRef = self.recipient.borrow()!
37
38 let balance = from.balance
39
40 let uuid = from.uuid
41
42 receiverRef.deposit(from: <-from)
43
44 emit PrivateDeposit(amount: balance, depositedUUID: uuid, from: self.owner?.address, to: receiverRef.owner?.address, toUUID: receiverRef.uuid)
45 }
46
47 init(recipient: Capability<&{FungibleToken.Receiver}>) {
48 pre {
49 recipient.borrow() != nil: "Could not borrow Receiver reference from the Capability"
50 }
51 self.recipient = recipient
52 }
53 }
54
55 // createNewForwarder creates a new Forwarder reference with the provided recipient
56 //
57 access(all) fun createNewForwarder(recipient: Capability<&{FungibleToken.Receiver}>): @Forwarder {
58 return <-create Forwarder(recipient: recipient)
59 }
60
61
62 access(all) resource Sender {
63 access(all) fun sendPrivateTokens(_ address: Address, tokens: @{FungibleToken.Vault}) {
64
65 let account = getAccount(address)
66
67 let privateReceiver = account.capabilities.borrow<&PrivateReceiverForwarder.Forwarder>(
68 PrivateReceiverForwarder.PrivateReceiverPublicPath
69 ) ?? panic("Could not borrow reference to private forwarder")
70
71 privateReceiver.deposit(from: <-tokens)
72
73 }
74 }
75
76 init(senderPath: StoragePath, storagePath: StoragePath, publicPath: PublicPath) {
77
78 self.SenderStoragePath = senderPath
79
80 self.PrivateReceiverStoragePath = storagePath
81 self.PrivateReceiverPublicPath = publicPath
82
83 self.account.storage.save(<-create Sender(), to: self.SenderStoragePath)
84
85 }
86}