@flowtyio/flow-contracts 0.1.0-beta.27 → 0.1.0-beta.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/*
|
|
2
|
+
FungibleTokenRouter forwards tokens from one account to another using
|
|
3
|
+
FungibleToken metadata views. If a token is not configured to be received,
|
|
4
|
+
any deposits will panic like they would a deposit that it attempt to a
|
|
5
|
+
non-existent receiver
|
|
6
|
+
|
|
7
|
+
https://github.com/Flowtyio/fungible-token-router
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import "FungibleToken"
|
|
11
|
+
import "FungibleTokenMetadataViews"
|
|
12
|
+
|
|
13
|
+
access(all) contract FungibleTokenRouter {
|
|
14
|
+
access(all) let StoragePath: StoragePath
|
|
15
|
+
access(all) let PublicPath: PublicPath
|
|
16
|
+
|
|
17
|
+
access(all) entitlement Owner
|
|
18
|
+
|
|
19
|
+
access(all) event RouterCreated(uuid: UInt64, defaultAddress: Address)
|
|
20
|
+
access(all) event OverrideAdded(uuid: UInt64, owner: Address?, overrideAddress: Address, tokenType: String)
|
|
21
|
+
access(all) event OverrideRemoved(uuid: UInt64, owner: Address?, overrideAddress: Address?, tokenType: String)
|
|
22
|
+
access(all) event TokensRouted(tokenType: String, amount: UFix64, to: Address)
|
|
23
|
+
|
|
24
|
+
access(all) resource Router: FungibleToken.Receiver {
|
|
25
|
+
// a default address that is used for any token type that is not overridden
|
|
26
|
+
access(all) var defaultAddress: Address
|
|
27
|
+
|
|
28
|
+
// token type identifier -> destination address
|
|
29
|
+
access(all) var addressOverrides: {String: Address}
|
|
30
|
+
|
|
31
|
+
access(Owner) fun setDefaultAddress(_ addr: Address) {
|
|
32
|
+
self.defaultAddress = addr
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
access(Owner) fun addOverride(type: Type, addr: Address) {
|
|
36
|
+
emit OverrideAdded(uuid: self.uuid, owner: self.owner?.address, overrideAddress: addr, tokenType: type.identifier)
|
|
37
|
+
self.addressOverrides[type.identifier] = addr
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
access(Owner) fun removeOverride(type: Type): Address? {
|
|
41
|
+
let removedAddr = self.addressOverrides.remove(key: type.identifier)
|
|
42
|
+
emit OverrideRemoved(uuid: self.uuid, owner: self.owner?.address, overrideAddress: removedAddr, tokenType: type.identifier)
|
|
43
|
+
return removedAddr
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
access(all) fun deposit(from: @{FungibleToken.Vault}) {
|
|
47
|
+
let tokenType = from.getType().identifier
|
|
48
|
+
let destination = self.addressOverrides[tokenType] ?? self.defaultAddress
|
|
49
|
+
|
|
50
|
+
if let md = from.resolveView(Type<FungibleTokenMetadataViews.FTVaultData>()) {
|
|
51
|
+
let vaultData = md as! FungibleTokenMetadataViews.FTVaultData
|
|
52
|
+
let receiver = getAccount(destination).capabilities.get<&{FungibleToken.Receiver}>(vaultData.receiverPath)
|
|
53
|
+
|
|
54
|
+
assert(receiver.check(), message: "no receiver found at path: ".concat(vaultData.receiverPath.toString()))
|
|
55
|
+
|
|
56
|
+
emit TokensRouted(tokenType: tokenType, amount: from.balance, to: destination)
|
|
57
|
+
receiver.borrow()!.deposit(from: <-from)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
panic("Could not find FungibleTokenMetadataViews.FTVaultData on depositing tokens")
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
access(all) view fun getSupportedVaultTypes(): {Type: Bool} {
|
|
64
|
+
// theoretically any token is supported, it depends on the defaultAddress
|
|
65
|
+
return {}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
access(all) view fun isSupportedVaultType(type: Type): Bool {
|
|
69
|
+
// theoretically any token is supported, it depends on the defaultAddress
|
|
70
|
+
return true
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
init(defaultAddress: Address) {
|
|
74
|
+
self.defaultAddress = defaultAddress
|
|
75
|
+
self.addressOverrides = {}
|
|
76
|
+
|
|
77
|
+
emit RouterCreated(uuid: self.uuid, defaultAddress: defaultAddress)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
access(all) fun createRouter(defaultAddress: Address): @Router {
|
|
82
|
+
return <- create Router(defaultAddress: defaultAddress)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
init() {
|
|
86
|
+
let identifier = "FungibleTokenRouter_".concat(self.account.address.toString())
|
|
87
|
+
self.StoragePath = StoragePath(identifier: identifier)!
|
|
88
|
+
self.PublicPath = PublicPath(identifier: identifier)!
|
|
89
|
+
}
|
|
90
|
+
}
|
package/flow.json
CHANGED
|
@@ -496,7 +496,17 @@
|
|
|
496
496
|
"aliases": {
|
|
497
497
|
"testing": "0x0000000000000007",
|
|
498
498
|
"emulator": "0xf8d6e0586b0a20c7",
|
|
499
|
-
"testnet": "0x83d75469f66d2ee6"
|
|
499
|
+
"testnet": "0x83d75469f66d2ee6",
|
|
500
|
+
"mainnet": "0xacc5081c003e24cf"
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
"FungibleTokenRouter": {
|
|
504
|
+
"source": "./contracts/fungible-token-router/FungibleTokenRouter.cdc",
|
|
505
|
+
"aliases": {
|
|
506
|
+
"testing": "0x0000000000000007",
|
|
507
|
+
"emulator": "0xf8d6e0586b0a20c7",
|
|
508
|
+
"testnet": "0x83231f90a288bc35",
|
|
509
|
+
"mainnet": "0x707c0b39a8d689cb"
|
|
500
510
|
}
|
|
501
511
|
}
|
|
502
512
|
},
|