-
Notifications
You must be signed in to change notification settings - Fork 13
adding functions to get the total flow balance of an account with all… #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
d12499b
105bbdf
5ae09a8
4644e79
64ee57d
58c66c9
16f53f4
c7eeaee
d4fd7a9
f3512f1
0aac227
ca67aa5
2955452
307ef3a
02e1001
79d039e
dc287e8
3f1d318
a01e0f2
52ae053
f91fa76
1d8000b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,139 @@ | ||||||||
|
|
||||||||
| import "FungibleToken" | ||||||||
| import "FlowToken" | ||||||||
| import "FlowIDTableStaking" | ||||||||
| import "LockedTokens" | ||||||||
| import "FlowStakingCollection" | ||||||||
| import "FlowStorageFees" | ||||||||
|
|
||||||||
|
|
||||||||
| pub contract AccountUtils { | ||||||||
|
|
||||||||
|
|
||||||||
| //Not really sure what fields to keep here? what is useful? | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| pub struct AccountInfo { | ||||||||
| pub(set) var primaryAcctBalance: UFix64 | ||||||||
| pub(set) var secondaryAddress: Address? | ||||||||
| pub(set) var secondaryAcctBalance: UFix64 | ||||||||
| pub(set) var stakedBalance: UFix64 | ||||||||
|
||||||||
|
|
||||||||
| init() { | ||||||||
| self.primaryAcctBalance = 0.0 | ||||||||
| self.secondaryAddress = nil | ||||||||
| self.secondaryAcctBalance = 0.0 | ||||||||
| self.stakedBalance = 0.0 | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| pub fun getStakesAndDelegations(_ account: PublicAccount) : {String:UFix64} { | ||||||||
|
|
||||||||
| var allNodeInfo: [FlowIDTableStaking.NodeInfo] = [] | ||||||||
| var allDelegateInfo: [FlowIDTableStaking.DelegatorInfo] = [] | ||||||||
|
|
||||||||
|
|
||||||||
| //get all stakers in new way | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| if let nodeStaker = account.getCapability<&{FlowIDTableStaking.NodeStakerPublic}>(FlowIDTableStaking.NodeStakerPublicPath).borrow() { | ||||||||
| allNodeInfo.append(FlowIDTableStaking.NodeInfo(nodeID: nodeStaker.id)) | ||||||||
| } | ||||||||
|
|
||||||||
| //get all delegators in new way | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| if let delegator = account.getCapability<&{FlowIDTableStaking.NodeDelegatorPublic}>(/public/flowStakingDelegator).borrow() { | ||||||||
| allDelegateInfo.append(FlowIDTableStaking.DelegatorInfo(nodeID: delegator.nodeID, delegatorID: delegator.id)) | ||||||||
| } | ||||||||
|
|
||||||||
| //get all stakers/delegators from the staking collection | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| var doesAccountHaveStakingCollection = FlowStakingCollection.doesAccountHaveStakingCollection(address: account.address) | ||||||||
| if doesAccountHaveStakingCollection { | ||||||||
| allNodeInfo.appendAll(FlowStakingCollection.getAllNodeInfo(address: account.address)) | ||||||||
| allDelegateInfo.appendAll(FlowStakingCollection.getAllDelegatorInfo(address: account.address)) | ||||||||
| } | ||||||||
|
|
||||||||
| //if we have a lockedAccount linked we need to add stakings/delegators there | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| if let lockedAccountInfo = account.getCapability<&LockedTokens.TokenHolder{LockedTokens.LockedAccountInfo}>(LockedTokens.LockedAccountInfoPublicPath).borrow() { | ||||||||
| if let nodeID = lockedAccountInfo.getNodeID() { | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| allNodeInfo.append(FlowIDTableStaking.NodeInfo(nodeID: nodeID)) | ||||||||
| } | ||||||||
|
|
||||||||
| if let delegatorID = lockedAccountInfo.getDelegatorID() { | ||||||||
| if let nodeID = lockedAccountInfo.getDelegatorNodeID() { | ||||||||
| allDelegateInfo.append(FlowIDTableStaking.DelegatorInfo(nodeID: nodeID, delegatorID: delegatorID)) | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
adding a closing bracket for the addition of the does account have staking collection conditional above |
||||||||
|
|
||||||||
| // ===== Aggregate all stakes and delegations in a digestible set ===== | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| // deduplication between the old way and the new way will happen automatically because the result is stored in a map | ||||||||
| let stakes : {String:UFix64} = {} | ||||||||
| for nodeInfo in allNodeInfo { | ||||||||
| let balance = nodeInfo.tokensStaked | ||||||||
| + nodeInfo.tokensCommitted | ||||||||
| + nodeInfo.tokensUnstaking | ||||||||
| + nodeInfo.tokensUnstaked | ||||||||
| + nodeInfo.tokensRewarded | ||||||||
|
|
||||||||
| stakes["n:".concat(nodeInfo.id)] = balance | ||||||||
| } | ||||||||
|
|
||||||||
| for delegatorInfo in allDelegateInfo { | ||||||||
| let balance = delegatorInfo.tokensStaked | ||||||||
| + delegatorInfo.tokensCommitted | ||||||||
| + delegatorInfo.tokensUnstaking | ||||||||
| + delegatorInfo.tokensUnstaked | ||||||||
| + delegatorInfo.tokensRewarded | ||||||||
|
|
||||||||
| stakes["n:".concat(delegatorInfo.nodeID).concat(" d:").concat(delegatorInfo.id.toString())] = balance | ||||||||
| } | ||||||||
|
|
||||||||
| return stakes | ||||||||
| } | ||||||||
|
|
||||||||
| /// Get the total flow balance for this account and its linked account | ||||||||
| pub fun getTotalFlowBalance(address: Address): UFix64? { | ||||||||
|
|
||||||||
| if let info = self.getAccountInfo(address: address) { | ||||||||
| return info.stakedBalance+info.primaryAcctBalance+info.secondaryAcctBalance | ||||||||
| } | ||||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| /// Get the account info for this account | ||||||||
| pub fun getAccountInfo(address: Address): AccountInfo?{ | ||||||||
|
|
||||||||
| var info: AccountInfo = AccountInfo() | ||||||||
|
|
||||||||
| let account = getAccount(address) | ||||||||
| //if balance is 0 the account is not valid | ||||||||
| if account.balance == 0.0 { | ||||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| //TODO: should we return something here | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| if account.getLinkTarget(/public/lockedFlowTokenReceiver) != nil { | ||||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| //we get the balance of this account | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| if let vaultRef = account.getCapability(/public/flowTokenBalance).borrow<&FlowToken.Vault{FungibleToken.Balance}>(){ | ||||||||
| info.primaryAcctBalance = vaultRef.balance | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
| //we get the locked account for this if any | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| if let lockedAccount = account.getCapability(LockedTokens.LockedAccountInfoPublicPath).borrow<&LockedTokens.TokenHolder{LockedTokens.LockedAccountInfo}>() { | ||||||||
| info.secondaryAddress = lockedAccount.getLockedAccountAddress() | ||||||||
| // \`+ FlowStorageFees.minimumStorageReservation\` is due to https://github.com/onflow/flow-core-contracts/blob/6fcd492d16186e5615d2e6589bc5b7ebce41f548/contracts/LockedTokens.cdc#L308 | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| info.secondaryAcctBalance = lockedAccount.getLockedAccountBalance() + FlowStorageFees.minimumStorageReservation | ||||||||
| } | ||||||||
| // Get stakes and delegations of the account and secondary/locked account | ||||||||
| let stakes = self.getStakesAndDelegations(account) | ||||||||
|
|
||||||||
| var stakeKey = "" | ||||||||
| for key in stakes.keys { | ||||||||
| let value = stakes[key]! | ||||||||
| stakeKey = stakeKey.concat(key).concat(", ") | ||||||||
bjartek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| info.stakedBalance = info.stakedBalance + value | ||||||||
| } | ||||||||
| //what is worth to keep in the struct? | ||||||||
| return info | ||||||||
| } | ||||||||
| } | ||||||||
Uh oh!
There was an error while loading. Please reload this page.