-
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 14 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,147 @@ | ||||||||
|
||||||||
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 stakedBalance: UFix64 | ||||||||
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. I would probably prefer this to be called 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. Sure we can do that. Totally forget about this pr. |
||||||||
pub(set) var delegatedBalance: UFix64 | ||||||||
|
||||||||
init(_ address:Address) { | ||||||||
self.primaryAddress=address | ||||||||
self.primaryAcctBalance = 0.0 | ||||||||
self.secondaryAddress = nil | ||||||||
self.secondaryAcctBalance = 0.0 | ||||||||
self.stakedBalance = 0.0 | ||||||||
self.delegatedBalance = 0.0 | ||||||||
} | ||||||||
|
||||||||
pub fun getTotalBalance() :UFix64 { | ||||||||
return self.primaryAcctBalance+self.secondaryAcctBalance+self.stakedBalance+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 | ||||||||
} | ||||||||
|
||||||||
//TODO: should we return something here | ||||||||
austinkline marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
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 | ||||||||
} | ||||||||
|
||||||||
// Get the locked account associated with the primary account if there is one | ||||||||
if let lockedAccount = account.getCapability(LockedTokens.LockedAccountInfoPublicPath).borrow<&LockedTokens.TokenHolder{LockedTokens.LockedAccountInfo}>() { | ||||||||
austinkline marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
} | ||||||||
|
||||||||
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)) | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
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 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.stakedBalance = info.stakedBalance + value | ||||||||
} | ||||||||
|
||||||||
for key in delegators.keys { | ||||||||
let value = delegators[key]! | ||||||||
info.delegatedBalance = info.delegatedBalance + value | ||||||||
} | ||||||||
|
||||||||
return info | ||||||||
} | ||||||||
} |
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 testWithoutPrefix() { | ||
// Act | ||
var balance = AccountUtils.getTotalFlowBalance("0xf8d6e0586b0a20c7") | ||
|
||
|
||
Test.assertEqual(balance, 0.1) | ||
|
||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.