-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPrivateReceiverForwarder.cdc
More file actions
77 lines (53 loc) · 2.38 KB
/
Copy pathPrivateReceiverForwarder.cdc
File metadata and controls
77 lines (53 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import FungibleToken from "./FungibleToken.cdc"
pub contract PrivateReceiverForwarder {
// Event that is emitted when tokens are deposited to the target receiver
pub event PrivateDeposit(amount: UFix64, to: Address?)
pub let SenderStoragePath: StoragePath
pub let PrivateReceiverStoragePath: StoragePath
pub let PrivateReceiverPublicPath: PublicPath
pub resource Forwarder {
// This is where the deposited tokens will be sent.
// The type indicates that it is a reference to a receiver
//
access(self) var recipient: Capability<&{FungibleToken.Receiver}>
// deposit
//
// Function that takes a Vault object as an argument and forwards
// it to the recipient's Vault using the stored reference
//
access(contract) fun deposit(from: @FungibleToken.Vault) {
let receiverRef = self.recipient.borrow()!
let balance = from.balance
receiverRef.deposit(from: <-from)
emit PrivateDeposit(amount: balance, to: self.owner?.address)
}
init(recipient: Capability<&{FungibleToken.Receiver}>) {
pre {
recipient.borrow() != nil: "Could not borrow Receiver reference from the Capability"
}
self.recipient = recipient
}
}
// createNewForwarder creates a new Forwarder reference with the provided recipient
//
pub fun createNewForwarder(recipient: Capability<&{FungibleToken.Receiver}>): @Forwarder {
return <-create Forwarder(recipient: recipient)
}
pub resource Sender {
pub fun sendPrivateTokens(_ address: Address, tokens: @FungibleToken.Vault) {
let account = getAccount(address)
let privateReceiver = account.getCapability<&PrivateReceiverForwarder.Forwarder>(PrivateReceiverForwarder.PrivateReceiverPublicPath)
.borrow() ?? panic("Could not borrow reference to private forwarder")
privateReceiver.deposit(from: <-tokens)
}
pub fun replicate(): @Sender {
return <-create Sender()
}
}
init() {
self.SenderStoragePath = /storage/PrivateSender
self.PrivateReceiverStoragePath = /storage/PrivateReceiver
self.PrivateReceiverPublicPath = /public/PrivateReceiver
self.account.save(<-create Sender(), to: self.SenderStoragePath)
}
}