-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add staking queries * add tests * add orderby to staking query * updates other count queries to return a number * address comments * addr comment * update tests * run fmt * fix test * fix build * regen and fmt --------- Co-authored-by: Greg Nazario <greg@gnazar.io>
- Loading branch information
1 parent
1219f4d
commit ad7898b
Showing
13 changed files
with
3,510 additions
and
3,203 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
getDelegatedStakingActivities, | ||
getNumberOfDelegators, | ||
getNumberOfDelegatorsForAllPools, | ||
} from "../internal/staking"; | ||
import { GetDelegatedStakingActivitiesResponse, GetNumberOfDelegatorsResponse, HexInput, OrderBy } from "../types"; | ||
import { AptosConfig } from "./aptos_config"; | ||
|
||
/** | ||
* A class to query all `Staking` related queries on Aptos. | ||
*/ | ||
export class Staking { | ||
readonly config: AptosConfig; | ||
|
||
constructor(config: AptosConfig) { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* Queries current number of delegators in a pool. Throws an error if the pool is not found. | ||
* | ||
* @param poolAddress Pool address | ||
* @returns The number of delegators for the given pool | ||
*/ | ||
async getNumberOfDelegators(args: { poolAddress: HexInput }): Promise<number> { | ||
const numDelegators = await getNumberOfDelegators({ aptosConfig: this.config, ...args }); | ||
return numDelegators; | ||
} | ||
|
||
/** | ||
* Queries current number of delegators in a pool. Throws an error if the pool is not found. | ||
* | ||
* @param poolAddress Pool address | ||
* @returns GetNumberOfDelegatorsForAllPoolsResponse response type | ||
*/ | ||
async getNumberOfDelegatorsForAllPools(args?: { | ||
options?: { | ||
orderBy?: OrderBy<GetNumberOfDelegatorsResponse[0]>; | ||
}; | ||
}): Promise<GetNumberOfDelegatorsResponse> { | ||
const numDelegatorData = await getNumberOfDelegatorsForAllPools({ aptosConfig: this.config, ...args }); | ||
return numDelegatorData; | ||
} | ||
|
||
/** | ||
* Queries delegated staking activities | ||
* | ||
* @param delegatorAddress Delegator address | ||
* @param poolAddress Pool address | ||
* @returns GetDelegatedStakingActivitiesResponse response type | ||
*/ | ||
async getDelegatedStakingActivities(args: { | ||
delegatorAddress: HexInput; | ||
poolAddress: HexInput; | ||
}): Promise<GetDelegatedStakingActivitiesResponse> { | ||
const delegatedStakingActivities = await getDelegatedStakingActivities({ aptosConfig: this.config, ...args }); | ||
return delegatedStakingActivities; | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/internal/queries/getDelegatedStakingActivities.graphql
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,12 @@ | ||
query getDelegatedStakingActivities($delegatorAddress: String, $poolAddress: String) { | ||
delegated_staking_activities( | ||
where: { delegator_address: { _eq: $delegatorAddress }, pool_address: { _eq: $poolAddress } } | ||
) { | ||
amount | ||
delegator_address | ||
event_index | ||
event_type | ||
pool_address | ||
transaction_version | ||
} | ||
} |
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,9 @@ | ||
query getNumberOfDelegators($where_condition: num_active_delegator_per_pool_bool_exp!, $order_by: [num_active_delegator_per_pool_order_by!]) { | ||
num_active_delegator_per_pool( | ||
where: $where_condition, | ||
order_by: $order_by | ||
) { | ||
num_active_delegator | ||
pool_address | ||
} | ||
} |
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,68 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/** | ||
* This file contains the underlying implementations for exposed API surface in | ||
* the {@link api/staking}. By moving the methods out into a separate file, | ||
* other namespaces and processes can access these methods without depending on the entire | ||
* faucet namespace and without having a dependency cycle error. | ||
*/ | ||
|
||
import { AptosConfig } from "../api/aptos_config"; | ||
import { Hex } from "../core"; | ||
import { GetDelegatedStakingActivitiesResponse, GetNumberOfDelegatorsResponse, HexInput, OrderBy } from "../types"; | ||
import { GetDelegatedStakingActivitiesQuery, GetNumberOfDelegatorsQuery } from "../types/generated/operations"; | ||
import { GetDelegatedStakingActivities, GetNumberOfDelegators } from "../types/generated/queries"; | ||
import { queryIndexer } from "./general"; | ||
|
||
export async function getNumberOfDelegators(args: { | ||
aptosConfig: AptosConfig; | ||
poolAddress: HexInput; | ||
}): Promise<number> { | ||
const { aptosConfig, poolAddress } = args; | ||
const address = Hex.fromHexInput({ hexInput: poolAddress }).toString(); | ||
const query = { | ||
query: GetNumberOfDelegators, | ||
variables: { where_condition: { pool_address: { _eq: address } } }, | ||
}; | ||
const data: GetNumberOfDelegatorsQuery = await queryIndexer<GetNumberOfDelegatorsQuery>({ aptosConfig, query }); | ||
if (data.num_active_delegator_per_pool.length === 0) { | ||
throw Error("Delegator pool not found"); | ||
} | ||
return data.num_active_delegator_per_pool[0].num_active_delegator; | ||
} | ||
|
||
export async function getNumberOfDelegatorsForAllPools(args: { | ||
aptosConfig: AptosConfig; | ||
options?: { | ||
orderBy?: OrderBy<GetNumberOfDelegatorsResponse[0]>; | ||
}; | ||
}): Promise<GetNumberOfDelegatorsResponse> { | ||
const { aptosConfig, options } = args; | ||
const query = { | ||
query: GetNumberOfDelegators, | ||
variables: { where_condition: {}, order_by: options?.orderBy }, | ||
}; | ||
const data: GetNumberOfDelegatorsQuery = await queryIndexer<GetNumberOfDelegatorsQuery>({ | ||
aptosConfig, | ||
query, | ||
}); | ||
return data.num_active_delegator_per_pool; | ||
} | ||
|
||
export async function getDelegatedStakingActivities(args: { | ||
aptosConfig: AptosConfig; | ||
delegatorAddress: HexInput; | ||
poolAddress: HexInput; | ||
}): Promise<GetDelegatedStakingActivitiesResponse> { | ||
const { aptosConfig, delegatorAddress, poolAddress } = args; | ||
const query = { | ||
query: GetDelegatedStakingActivities, | ||
variables: { | ||
delegatorAddress: Hex.fromHexInput({ hexInput: delegatorAddress }).toString(), | ||
poolAddress: Hex.fromHexInput({ hexInput: poolAddress }).toString(), | ||
}, | ||
}; | ||
const data = await queryIndexer<GetDelegatedStakingActivitiesQuery>({ aptosConfig, query }); | ||
return data.delegated_staking_activities; | ||
} |
Oops, something went wrong.