-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsetRelatedAccount.cdc
36 lines (27 loc) · 1.38 KB
/
setRelatedAccount.cdc
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
import FindRelatedAccounts from "../contracts/FindRelatedAccounts.cdc"
import FIND from "../contracts/FIND.cdc"
transaction(name: String, target: String) {
var relatedAccounts : &FindRelatedAccounts.Accounts?
let address : Address?
prepare(account: AuthAccount) {
self.address = FIND.resolve(target)
self.relatedAccounts= account.borrow<&FindRelatedAccounts.Accounts>(from:FindRelatedAccounts.storagePath)
if self.relatedAccounts == nil {
let relatedAccounts <- FindRelatedAccounts.createEmptyAccounts()
account.save(<- relatedAccounts, to: FindRelatedAccounts.storagePath)
account.link<&FindRelatedAccounts.Accounts{FindRelatedAccounts.Public}>(FindRelatedAccounts.publicPath, target: FindRelatedAccounts.storagePath)
self.relatedAccounts= account.borrow<&FindRelatedAccounts.Accounts>(from:FindRelatedAccounts.storagePath)
}
let cap = account.getCapability<&FindRelatedAccounts.Accounts{FindRelatedAccounts.Public}>(FindRelatedAccounts.publicPath)
if !cap.check() {
account.unlink(FindRelatedAccounts.publicPath)
account.link<&FindRelatedAccounts.Accounts{FindRelatedAccounts.Public}>(FindRelatedAccounts.publicPath, target: FindRelatedAccounts.storagePath)
}
}
pre{
self.address != nil : "The input pass in is not a valid name or address. Input : ".concat(target)
}
execute{
self.relatedAccounts!.addFlowAccount(name: name, address: self.address!)
}
}