Skip to content

Commit

Permalink
refactor: ChainAmount → TokenCount
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 14, 2024
1 parent f280790 commit 769ef76
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
10 changes: 5 additions & 5 deletions packages/orchestration/src/cosmos-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
LocalIbcAddress,
RemoteIbcAddress,
} from '@agoric/vats/tools/ibc-utils.js';
import type { AmountArg, ChainAddress, ChainAmount } from './types.js';
import type { AmountArg, ChainAddress, TokenCount } from './types.js';

/** A helper type for type extensions. */
export type TypeUrl = string;
Expand Down Expand Up @@ -93,14 +93,14 @@ export interface StakingAccountQueries {
* Get the pending rewards for the account.
* @returns the amounts of the account's rewards pending from all validators
*/
getRewards: () => Promise<ChainAmount[]>;
getRewards: () => Promise<TokenCount[]>;

/**
* Get the rewards pending with a specific validator.
* @param validator - the validator address to query for
* @returns the amount of the account's rewards pending from a specific validator
*/
getReward: (validator: CosmosValidatorAddress) => Promise<ChainAmount[]>;
getReward: (validator: CosmosValidatorAddress) => Promise<TokenCount[]>;
}
export interface StakingAccountActions {
/**
Expand Down Expand Up @@ -140,14 +140,14 @@ export interface StakingAccountActions {
* Withdraw rewards from all validators. The promise settles when the rewards are withdrawn.
* @returns The total amounts of rewards withdrawn
*/
withdrawRewards: () => Promise<ChainAmount[]>;
withdrawRewards: () => Promise<TokenCount[]>;

/**
* Withdraw rewards from a specific validator. The promise settles when the rewards are withdrawn.
* @param validator - the validator to withdraw rewards from
* @returns
*/
withdrawReward: (validator: CosmosValidatorAddress) => Promise<ChainAmount[]>;
withdrawReward: (validator: CosmosValidatorAddress) => Promise<TokenCount[]>;
}

/**
Expand Down
16 changes: 8 additions & 8 deletions packages/orchestration/src/exos/stakingAccountKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
export const maxClockSkew = 10n * 60n;

/**
* @import {AmountArg, IcaAccount, ChainAddress, ChainAmount, CosmosValidatorAddress, ICQConnection, StakingAccountActions} from '../types.js';
* @import {AmountArg, IcaAccount, ChainAddress, ChainAmount, CosmosValidatorAddress, ICQConnection, StakingAccountActions, TokenCount} from '../types.js';
* @import {RecorderKit, MakeRecorderKit} from '@agoric/zoe/src/contractSupport/recorder.js';
* @import {Baggage} from '@agoric/swingset-liveslots';
* @import {AnyJson} from '@agoric/cosmic-proto';
Expand Down Expand Up @@ -128,8 +128,8 @@ export const tryDecodeResponse = (ackStr, fromProtoMsg) => {
}
};

/** @type {(c: { denom: string, amount: string }) => ChainAmount} */
const toChainAmount = c => ({ denom: c.denom, value: BigInt(c.amount) });
/** @type {(c: { denom: string, amount: string }) => TokenCount} */
const toTokenCount = c => ({ denom: c.denom, value: BigInt(c.amount) });

/**
* @param {Baggage} baggage
Expand Down Expand Up @@ -341,7 +341,7 @@ export const prepareStakingAccountKit = (baggage, makeRecorderKit, zcf) => {

/**
* @param {CosmosValidatorAddress} validator
* @returns {Promise<ChainAmount[]>}
* @returns {Promise<TokenCount[]>}
*/
async withdrawReward(validator) {
trace('withdrawReward', validator);
Expand All @@ -359,11 +359,11 @@ export const prepareStakingAccountKit = (baggage, makeRecorderKit, zcf) => {
);
trace('withdrawReward response', response);
const { amount: coins } = response;
return harden(coins.map(toChainAmount));
return harden(coins.map(toTokenCount));
},
/**
* @param {ChainAmount['denom']} [denom] - defaults to bondDenom
* @returns {Promise<ChainAmount>}
* @param {TokenCount['denom']} [denom] - defaults to bondDenom
* @returns {Promise<TokenCount>}
*/
async getBalance(denom) {
const { chainAddress, icqConnection, bondDenom } = this.state;
Expand All @@ -383,7 +383,7 @@ export const prepareStakingAccountKit = (baggage, makeRecorderKit, zcf) => {
decodeBase64(result.key),
);
if (!balance) throw Fail`Result lacked balance key: ${result}`;
return harden(toChainAmount(balance));
return harden(toTokenCount(balance));
},

withdrawRewards() {
Expand Down
13 changes: 6 additions & 7 deletions packages/orchestration/src/orchestration-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ export type DenomArg = Denom | Brand;
/**
* Count of some fungible token on some blockchain.
*
* NB: this is not an instance of the `Amount` type from ERTP but can be
* converted to one surjectively
* @see {@link Orchestrator.asAmount} to convert to an Amount surjectively
*/
export type ChainAmount = {
export type TokenCount = {
denom: Denom;
value: bigint; // Nat
};

/** Amounts can be provided as pure data using denoms or as native Amounts */
export type AmountArg = ChainAmount | Amount;
export type AmountArg = TokenCount | Amount;

/** An address on some blockchain, e.g., cosmos, eth, etc. */
export type ChainAddress = {
Expand Down Expand Up @@ -108,7 +107,7 @@ export interface Orchestrator {
* @param amount - the described amount
* @returns the Amount in local structuerd format
*/
asAmount: (amount: ChainAmount) => NatAmount;
asAmount: (amount: TokenCount) => NatAmount;
}

/**
Expand All @@ -121,10 +120,10 @@ export interface OrchestrationAccountI {
getAddress: () => ChainAddress;

/** @returns an array of amounts for every balance in the account. */
getBalances: () => Promise<ChainAmount[]>;
getBalances: () => Promise<TokenCount[]>;

/** @returns the balance of a specific denom for the account. */
getBalance: (denom: DenomArg) => Promise<ChainAmount>;
getBalance: (denom: DenomArg) => Promise<TokenCount>;

/**
* Transfer amount to another account on the same chain. The promise settles when the transfer is complete.
Expand Down

0 comments on commit 769ef76

Please sign in to comment.