Skip to content
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

10016 StakingAccountQueries #10069

Merged
merged 8 commits into from
Sep 17, 2024
Merged

10016 StakingAccountQueries #10069

merged 8 commits into from
Sep 17, 2024

Conversation

turadg
Copy link
Member

@turadg turadg commented Sep 11, 2024

closes: #10016

Description

Implements the StakingAccountQueries. Except for getRedelegation because there is no query that's guaranteed to return one. It's a strict subset of what's returned by getRedelegations. If there are ever use cases in which that payload is too large we can optimize then.

This also massages the return types in cosmos-api.ts to more closely match what is returned by the Cosmos query endpoints. (No longer dropping data.)

Security Considerations

The queries are read-only. Like any query there is a risk of resource exhaustion. These aren't rate limited but since they run in a vat the malicious code would exhaust itself. The risks of exhausting the RPC server are no greater than any off-chain query.

Scaling Considerations

May benefit from some load testing in production-ish environment to see what the service can handle.

Documentation Considerations

The interface is already documented. This implements it so nothing changes (other than the return types, as mentioned above)

Testing Considerations

New tests

Upgrade Considerations

No vats yet use this code.

Copy link

cloudflare-workers-and-pages bot commented Sep 11, 2024

Deploying agoric-sdk with  Cloudflare Pages  Cloudflare Pages

Latest commit: c006436
Status: ✅  Deploy successful!
Preview URL: https://b64abe8d.agoric-sdk.pages.dev
Branch Preview URL: https://10016-stakingaccountqueries.agoric-sdk.pages.dev

View logs

@turadg turadg marked this pull request as ready for review September 16, 2024 22:08
@turadg turadg marked this pull request as draft September 17, 2024 00:08
Comment on lines 27 to 38
await osmoAccount.undelegate(
delegations.map(d => ({
validator: {
chainId: 'osmosis-1',
value: /** @type {`${string}valoper${string}`} */ (
d.delegation.validatorAddress
),
encoding: 'bech32',
},
amount: { denom: 'uosmo', value: BigInt(d.balance.amount) },
})),
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the mismatch between the return of getDelegations and the param of undelegate is clunky. I'll see about cleaning that up before calling this R4R.

Other feedback welcomed though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 6420537 we moved away from using the Delegation: { validatorAddress, delegatorAddress, shares } type with undelegate to better align with the .delegate() and .redelegate().

I think this was the correct decision to better align the API, so I would support seeing us massage to getDelegations: () => { validator: CosmosValidatorAddress, amount: DenomAmount[] } if that's what you're aiming for.

Copy link
Member

@0xpatrickdev 0xpatrickdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, only critical feedback item is ensure toDenomAmount doesn't throw when we give it a decimal.

I also assume your view is that LocalOrchAccount queries are out of scope for this task. I'm fine with this as we don't have any stories that require it, but think we should make a note or task item on #10048

Comment on lines 27 to 38
await osmoAccount.undelegate(
delegations.map(d => ({
validator: {
chainId: 'osmosis-1',
value: /** @type {`${string}valoper${string}`} */ (
d.delegation.validatorAddress
),
encoding: 'bech32',
},
amount: { denom: 'uosmo', value: BigInt(d.balance.amount) },
})),
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 6420537 we moved away from using the Delegation: { validatorAddress, delegatorAddress, shares } type with undelegate to better align with the .delegate() and .redelegate().

I think this was the correct decision to better align the API, so I would support seeing us massage to getDelegations: () => { validator: CosmosValidatorAddress, amount: DenomAmount[] } if that's what you're aiming for.

return harden({
rewards: rewards.map(reward => ({
validatorAddress: reward.validatorAddress,
reward: reward.reward.map(toDenomAmount),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we need to add logic to toDenomAmount to round down to the nearest whole integer? I see that this query returns DecCoin instead of the typical Coin.

Alternatively, accounting for decimals with ratios is an option, but I'm not sure it's worth the cost here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I've added tests and helpers for this

@@ -97,6 +100,12 @@ export type CosmosChainInfo = Readonly<{
stakingTokens?: Readonly<Array<{ denom: string }>>;
}>;

/** @see {QueryDelegationTotalRewardsResponse} */
export interface OrchestrationQueryDelegationTotalRewardsResponse {
rewards: { validatorAddress: string; reward: DenomAmount[] }[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nb: Consider

Suggested change
rewards: { validatorAddress: string; reward: DenomAmount[] }[];
rewards: { validator: CosmosValidatorAddress; amount: DenomAmount[] }[];

for consistency, although we don't need this response for withdrawReward or withdrawRewards


/**
* Get the pending rewards for the account.
* @returns the amounts of the account's rewards pending from all validators
*/
getRewards: () => Promise<DenomAmount[]>;
getRewards: () => Promise<OrchestrationQueryDelegationTotalRewardsResponse>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like what OrchestrationQueryDelegationTotalRewardsResponse computes to, but consider a shorter name. I recognize something like DelegationTotalRewards or DelegationRewards might look too much like a cosmic-proto type, but I would prefer the brevity at this expense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gonna try CosmosGetRewardsResponse. Pattern being: "Cosmos" to convey that it's part of cosmos-api and it's clearly not part one of the protobuf types because they don't need to say that. Middle is the method name ("getRewards") and "Response" is because it's the return value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, all the queries start with get and Response implies a query so maybe CosmosRewardsResponse.

balance: { denom: 'uatom', amount: '1000000' },
},
],
pagination: { nextKey: new Uint8Array(), total: 1n },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we handle/support pagination? Not sure it's worth the cost atm, but curious what it looks like. Maybe it's as simple as exposing a nextKey as an optional param in getDelegations() and the other interfaces that support it.

@turadg turadg marked this pull request as ready for review September 17, 2024 19:00
@@ -0,0 +1,153 @@
/* eslint-env node */
Copy link
Member

@0xpatrickdev 0xpatrickdev Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! (entire file 🙂)

@turadg turadg added the automerge:rebase Automatically rebase updates, then merge label Sep 17, 2024
@mergify mergify bot merged commit f291362 into master Sep 17, 2024
90 checks passed
@mergify mergify bot deleted the 10016-StakingAccountQueries branch September 17, 2024 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge:rebase Automatically rebase updates, then merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

implement StakingAccountQueries interface
2 participants