Skip to content

Commit

Permalink
refactor: ChainAmount → DenomAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 15, 2024
1 parent dfb656b commit ccccd0e
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, DenomAmount } 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<DenomAmount[]>;

/**
* 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<DenomAmount[]>;
}
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<DenomAmount[]>;

/**
* 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<DenomAmount[]>;
}

/**
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, DenomAmount} 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 }) => DenomAmount} */
const toDenomAmount = 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<DenomAmount[]>}
*/
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(toDenomAmount));
},
/**
* @param {ChainAmount['denom']} [denom] - defaults to bondDenom
* @returns {Promise<ChainAmount>}
* @param {DenomAmount['denom']} [denom] - defaults to bondDenom
* @returns {Promise<DenomAmount>}
*/
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(toDenomAmount(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 @@ -32,16 +32,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 DenomAmount = {
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 = DenomAmount | Amount;

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

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

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

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

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

0 comments on commit ccccd0e

Please sign in to comment.