-
Notifications
You must be signed in to change notification settings - Fork 43
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
List staking balances #134
Merged
Merged
Changes from 19 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
1bc6462
[chore] Update package-lock.json (#132)
John-peterson-coinbase b335769
initial commit for list balances
marchsu dabcd61
fix tests
marchsu 1f4507e
rm unnecessary change
marchsu 29d8932
return from model
marchsu 4aadb96
lint
marchsu bb60cc6
fix typo
marchsu 1e19dd2
rename some vars and update tests
marchsu 9395842
rename func
marchsu c13fb15
rm unnecessary var
marchsu 562fcea
lint
marchsu a42f6c8
fix lint and test
marchsu 891a2a0
rm request body
marchsu cfc45aa
rm request totally and update desc
marchsu 8fbb805
clean up
marchsu aed5841
add to export
marchsu e6de8f6
fix
marchsu b54b2ed
fix test
marchsu 955a42f
add test
marchsu 5228a9b
add list historical staking balance to address
marchsu 3886f7c
add tests in address
marchsu e6a158f
clean up a bit
marchsu 97455d0
Merge remote-tracking branch 'origin/v0.0.16'
marchsu 09dd340
leave unrelated unchanged
marchsu ab3337f
force
marchsu a4a2205
rename some test vars
marchsu 74ce333
add changelog
marchsu 0ea73ec
Merge remote-tracking branch 'origin/v0.0.16'
marchsu 10ba61c
change prefix of static method test
marchsu 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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,120 @@ | ||
import { StakingBalance as StakingBalanceModel } from "../client"; | ||
import { Balance } from "./balance"; | ||
import { Coinbase } from "./coinbase"; | ||
|
||
/** | ||
* A representation of a staking balance earned on a network for a given asset. | ||
*/ | ||
export class StakingBalance { | ||
private model: StakingBalanceModel; | ||
|
||
/** | ||
* Creates the StakingBalance object. | ||
* | ||
* @param model - The underlying staking balance object. | ||
*/ | ||
constructor(model: StakingBalanceModel) { | ||
this.model = model; | ||
} | ||
|
||
/** | ||
* Returns a list of StakingBalances for the provided network, asset, and address. | ||
* | ||
* @param networkId - The network ID. | ||
* @param assetId - The asset ID. | ||
* @param address - The address ID. | ||
* @param startTime - The start time. | ||
* @param endTime - The end time. | ||
* @returns The staking balances. | ||
*/ | ||
public static async list( | ||
networkId: string, | ||
assetId: string, | ||
address: string, | ||
startTime: string, | ||
endTime: string, | ||
): Promise<StakingBalance[]> { | ||
const stakingBalances: StakingBalance[] = []; | ||
const queue: string[] = [""]; | ||
|
||
while (queue.length > 0) { | ||
const page = queue.shift(); | ||
|
||
const response = await Coinbase.apiClients.stake!.fetchHistoricalStakingBalances( | ||
address, | ||
networkId, | ||
assetId, | ||
startTime, | ||
endTime, | ||
100, | ||
page?.length ? page : undefined, | ||
); | ||
|
||
response.data.data.forEach(stakingBalance => { | ||
stakingBalances.push(new StakingBalance(stakingBalance)); | ||
}); | ||
|
||
if (response.data.has_more) { | ||
if (response.data.next_page) { | ||
queue.push(response.data.next_page); | ||
} | ||
} | ||
} | ||
|
||
return stakingBalances; | ||
} | ||
|
||
/** | ||
* Returns the bonded stake amount of the StakingBalance. | ||
* | ||
* @returns The Balance. | ||
*/ | ||
public bondedStake(): Balance { | ||
return Balance.fromModelWithAmountInWholeUnits(this.model.bonded_stake); | ||
} | ||
|
||
/** | ||
* Returns the unbonded stake amount of the StakingBalance. | ||
* | ||
* @returns The Balance. | ||
*/ | ||
public unbondedBalance(): Balance { | ||
return Balance.fromModelWithAmountInWholeUnits(this.model.unbonded_balance); | ||
} | ||
|
||
/** | ||
* Returns the participant type of the address. | ||
* | ||
* @returns The participant type. | ||
*/ | ||
public participantType(): string { | ||
return this.model.participant_type; | ||
} | ||
|
||
/** | ||
* Returns the date of the StakingBalance. | ||
* | ||
* @returns The date. | ||
*/ | ||
public date(): Date { | ||
return new Date(this.model.date); | ||
} | ||
|
||
/** | ||
* Returns the onchain address of the StakingBalance. | ||
* | ||
* @returns The onchain address. | ||
*/ | ||
public address(): string { | ||
return this.model.address; | ||
} | ||
|
||
/** | ||
* Print the Staking Balance as a string. | ||
* | ||
* @returns The string representation of the Staking Balance. | ||
*/ | ||
public toString(): string { | ||
return `StakingBalance { date: '${this.date().toISOString()}' address: '${this.address()}' bondedStake: '${this.bondedStake().toString()}' unbondedBalance: '${this.unbondedBalance().toString()}' participantType: '${this.participantType()}' }`; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Should this be a method on the
Address
class (ref) instead of static method here?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.
we have an equivalent for listing staking rewards , I think we need both
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.
I see. That pattern is fine. Can you please add the
Address
class change to this PR.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.
added, wondering if I can test the change to Address, currently only unit tests added to all related files includingWallet
,External Address
&Wallet Address
hardcoded the address in the
list
and verify the call from address functions wellThere 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.
Cool. Thanks! You can also call this method on an
ExternalAddress
subclass instance if you want to test on a specific address.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.
thanks, that works as well
toString()
StakingBalance { date: '2024-07-02T00:00:00.000Z' address: '0x80000001677f23a227dfed6f61b132d114be83b8ad0aa5f3c5d1d77e6ee0bf5f73b0af750cc34e8f2dae73c21dc36f4a' bondedStake: 'Balance { amount: '32' asset: 'Asset{ networkId: ethereum-mainnet, assetId: eth, contractAddress: undefined, decimals: 18 }' }' unbondedBalance: 'Balance { amount: '5.479839548' asset: 'Asset{ networkId: ethereum-mainnet, assetId: eth, contractAddress: undefined, decimals: 18 }' }' participantType: 'VALIDATOR' }