-
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
Open
bjartek
wants to merge
22
commits into
main
Choose a base branch
from
FlowUtils
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d12499b
adding functions to get the total flow balance of an account with all…
bjartek 105bbdf
add me with index 1
bjartek 5ae09a8
Update cadence/contracts/AccountUtils.cdc
bjartek 4644e79
Update cadence/contracts/AccountUtils.cdc
bjartek 64ee57d
Update cadence/contracts/AccountUtils.cdc
bjartek 58c66c9
Update cadence/contracts/AccountUtils.cdc
bjartek 16f53f4
Update cadence/contracts/AccountUtils.cdc
bjartek c7eeaee
Update cadence/contracts/AccountUtils.cdc
bjartek d4fd7a9
Update cadence/contracts/AccountUtils.cdc
bjartek f3512f1
Update cadence/contracts/AccountUtils.cdc
bjartek 0aac227
refactor and add more fields
bjartek ca67aa5
trying to get imports to work
bjartek 2955452
remove uneeded code
bjartek 307ef3a
remove uneeeded flow.json configuration
bjartek 02e1001
rename stakedBalance -> nodeStakedBalance
bjartek 79d039e
trying to get this test to work
bjartek dc287e8
adding example scripts and got it to work
bjartek 3f1d318
delete uneeded standard code
bjartek a01e0f2
update flow cli
bjartek 52ae053
fixed tests
bjartek f91fa76
use service account address
bjartek 1d8000b
fixed comments
bjartek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||||||
|
|
||||||
| pub struct AccountInfo { | ||||||
|
|
||||||
| pub(set) var primaryAddress: Address | ||||||
| pub(set) var primaryAcctBalance: UFix64 | ||||||
| pub(set) var secondaryAddress: Address? | ||||||
| pub(set) var secondaryAcctBalance: UFix64 | ||||||
| pub(set) var nodeStakedBalance: UFix64 | ||||||
| pub(set) var delegatedBalance: UFix64 | ||||||
|
|
||||||
| init(_ address:Address) { | ||||||
| self.primaryAddress=address | ||||||
| self.primaryAcctBalance = 0.0 | ||||||
| self.secondaryAddress = nil | ||||||
| self.secondaryAcctBalance = 0.0 | ||||||
| self.nodeStakedBalance = 0.0 | ||||||
| self.delegatedBalance = 0.0 | ||||||
| } | ||||||
|
|
||||||
| pub fun getTotalBalance() :UFix64 { | ||||||
| return self.primaryAcctBalance+self.secondaryAcctBalance+self.nodeStakedBalance+self.delegatedBalance | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// 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.getTotalBalance() | ||||||
| } | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| /// Get the account info for this account | ||||||
| pub fun getAccountInfo(address: Address): AccountInfo?{ | ||||||
|
|
||||||
| var info: AccountInfo = AccountInfo(address) | ||||||
|
|
||||||
| let account = getAccount(address) | ||||||
| //if balance is 0 the account is not valid | ||||||
| if account.balance == 0.0 { | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| if account.getLinkTarget(/public/lockedFlowTokenReceiver) != nil { | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| // Get the main Vault balance of this account | ||||||
| if let vaultRef = account.getCapability(/public/flowTokenBalance).borrow<&FlowToken.Vault{FungibleToken.Balance}>(){ | ||||||
| info.primaryAcctBalance = vaultRef.balance | ||||||
| } | ||||||
|
|
||||||
| var allNodeInfo: [FlowIDTableStaking.NodeInfo] = [] | ||||||
| var allDelegateInfo: [FlowIDTableStaking.DelegatorInfo] = [] | ||||||
|
|
||||||
| // get all node objects using the original basic node account configuration | ||||||
| if let nodeStaker = account.getCapability<&{FlowIDTableStaking.NodeStakerPublic}>(FlowIDTableStaking.NodeStakerPublicPath).borrow() { | ||||||
| allNodeInfo.append(FlowIDTableStaking.NodeInfo(nodeID: nodeStaker.id)) | ||||||
| } | ||||||
|
|
||||||
| // get all delegator objects using the original basic delegator account configuration | ||||||
| if let delegator = account.getCapability<&{FlowIDTableStaking.NodeDelegatorPublic}>(/public/flowStakingDelegator).borrow() { | ||||||
| allDelegateInfo.append(FlowIDTableStaking.DelegatorInfo(nodeID: delegator.nodeID, delegatorID: delegator.id)) | ||||||
| } | ||||||
|
|
||||||
| // get all nodes/delegators from the staking collection | ||||||
| // includes all nodes and delegators that are in the locked account | ||||||
| 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 but don't have a staking collection we need to add nodes/delegators there | ||||||
| // If there is a locked account and a staking collection, the staking collection staking information would have already included the locked account | ||||||
| if let lockedAccountInfo = account.getCapability<&LockedTokens.TokenHolder{LockedTokens.LockedAccountInfo}>(LockedTokens.LockedAccountInfoPublicPath).borrow() { | ||||||
|
|
||||||
| info.secondaryAddress = lockedAccountInfo.getLockedAccountAddress() | ||||||
| info.secondaryAcctBalance = lockedAccountInfo.getLockedAccountBalance() + FlowStorageFees.minimumStorageReservation | ||||||
| if !doesAccountHaveStakingCollection { | ||||||
| if let nodeID = lockedAccountInfo.getNodeID() { | ||||||
| allNodeInfo.append(FlowIDTableStaking.NodeInfo(nodeID: nodeID)) | ||||||
| } | ||||||
|
|
||||||
| if let delegatorID = lockedAccountInfo.getDelegatorID() { | ||||||
| if let nodeID = lockedAccountInfo.getDelegatorNodeID() { | ||||||
| allDelegateInfo.append(FlowIDTableStaking.DelegatorInfo(nodeID: nodeID, delegatorID: delegatorID)) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // ===== 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 nodes : {String:UFix64} = {} | ||||||
| let delegators : {String:UFix64} = {} | ||||||
| for nodeInfo in allNodeInfo { | ||||||
| let balance = nodeInfo.tokensStaked | ||||||
| + nodeInfo.tokensCommitted | ||||||
| + nodeInfo.tokensUnstaking | ||||||
| + nodeInfo.tokensUnstaked | ||||||
| + nodeInfo.tokensRewarded | ||||||
|
|
||||||
| nodes["n:".concat(nodeInfo.id)] = balance | ||||||
| } | ||||||
|
|
||||||
| for delegatorInfo in allDelegateInfo { | ||||||
| let balance = delegatorInfo.tokensStaked | ||||||
| + delegatorInfo.tokensCommitted | ||||||
| + delegatorInfo.tokensUnstaking | ||||||
| + delegatorInfo.tokensUnstaked | ||||||
| + delegatorInfo.tokensRewarded | ||||||
|
|
||||||
| delegators["n:".concat(delegatorInfo.nodeID).concat(" d:").concat(delegatorInfo.id.toString())] = balance | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| for key in nodes.keys { | ||||||
| let value = nodes[key]! | ||||||
| info.nodeStakedBalance = info.nodeStakedBalance + value | ||||||
| } | ||||||
|
|
||||||
| for key in delegators.keys { | ||||||
| let value = delegators[key]! | ||||||
| info.delegatedBalance = info.delegatedBalance + value | ||||||
| } | ||||||
|
|
||||||
| return info | ||||||
| } | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import "AccountUtils" | ||
| pub fun main(address:Address) : AnyStruct { | ||
|
|
||
| return AccountUtils.getTotalFlowBalance(address:address) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import "AccountUtils" | ||
| pub fun main(address:Address) : AnyStruct { | ||
|
|
||
| return AccountUtils.getAccountInfo(address:address) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import Test | ||
| import "AccountUtils" | ||
|
|
||
| access(all) | ||
| fun setup() { | ||
| var err = Test.deployContract( | ||
| name: "AccountUtils", | ||
| path: "../cadence/contracts/AccountUtils.cdc", | ||
| arguments: [] | ||
| ) | ||
| Test.expect(err, Test.beNil()) | ||
| } | ||
|
|
||
| access(all) | ||
| fun testGetFlowBalance() { | ||
|
|
||
| let address=Test.serviceAccount().address | ||
| let balance = AccountUtils.getTotalFlowBalance(address:address) | ||
| let expected : UFix64? = 1000000000.0 | ||
| Test.assertEqual(expected, balance) | ||
|
|
||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding a closing bracket for the addition of the does account have staking collection conditional above