-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support List Historical Staking Balances
List staking balances
- Loading branch information
Showing
14 changed files
with
783 additions
and
0 deletions.
There are no files selected for viewing
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
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 the staking balance for a given asset on a specific date. | ||
*/ | ||
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.