Skip to content

Commit

Permalink
extract ethereum-api types module
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 10, 2024
1 parent 2cbe94c commit 48f62fd
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 138 deletions.
60 changes: 55 additions & 5 deletions packages/orchestration/src/cosmos-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
RemoteIbcAddress,
} from '@agoric/vats/tools/ibc-utils.js';
import type { ChainAddress } from './orchestration-api.js';
import type { AmountArg, ChainAmount } from './types.js';
import type { AmountArg, Chain, KnownChains } from './types.js';

/** A helper type for type extensions. */
export type TypeUrl = string;
Expand All @@ -33,6 +33,25 @@ export type CosmosValidatorAddress = ChainAddress & {
addressEncoding: 'bech32';
};

/**
* A denom that designates a token type on some blockchain.
*
* Multiple denoms may designate the same underlying base denom (e.g., `uist`,
* `uatom`) on different Chains or on the same Chain via different paths. On
* Cosmos chains, all but the base denom are IBC style denoms, but that may vary
* across other chains. All the denoms that designate the same underlying base
* denom form an equivalence class, along with the unique Brand on the local
* Chain. Some operations accept any member of the equivalence class to
* effectively designate the corresponding token type on the target chain.
*/
export type Denom = string; // ibc/... or uist

/** Description for an amount of some fungible currency */
export type CoinAmount = {
denom: Denom;
value: bigint; // Nat
};

/**
* Info for a Cosmos-based chain.
*/
Expand Down Expand Up @@ -71,6 +90,7 @@ export interface IcaAccount {
* @returns the address of the account on the remote chain
*/
getAddress: () => ChainAddress;

/**
* Submit a transaction on behalf of the remote account for execution on the remote chain.
* @param msgs - records for the transaction
Expand Down Expand Up @@ -140,14 +160,14 @@ export interface CosmosStakingFacet {
* Get the pending rewards for the account.
* @returns the amounts of the account's rewards pending from all validators
*/
getRewards: () => Promise<ChainAmount[]>;
getRewards: () => Promise<CoinAmount[]>;

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

/**
* Delegate an amount to a validator. The promise settles when the delegation is complete.
Expand Down Expand Up @@ -185,12 +205,42 @@ export interface CosmosStakingFacet {
* 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<CoinAmount[]>;

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

export type CosmosRegistry = {
/**
* For a denom, return information about a denom including the equivalent
* local Brand, the Chain on which the denom is held, and the Chain that
* issues the corresponding asset.
* @param denom
*/
getBrandInfo: <
HoldingChain extends keyof KnownChains,
IssuingChain extends keyof KnownChains,
>(
denom: Denom,
) => {
/** The well-known Brand on Agoric for the direct asset */
brand?: Brand;
/** The Chain at which the argument `denom` exists (where the asset is currently held) */
chain: Chain<HoldingChain>;
/** The Chain that is the issuer of the underlying asset */
base: Chain<IssuingChain>;
/** the Denom for the underlying asset on its issuer chain */
baseDenom: Denom;
};
/**
* Convert an amount described in native data to a local, structured Amount.
* @param amount - the described amount
* @returns the Amount in local structuerd format
*/
asAmount: (amount: CoinAmount) => Amount;
};
7 changes: 7 additions & 0 deletions packages/orchestration/src/ethereum-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Info for an Ethereum-based chain.
*/
export type EthChainInfo = {
chainId: string;
allegedName: string;
};
10 changes: 5 additions & 5 deletions packages/orchestration/src/exos/stakingAccountKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { toRequestQueryJson } from '@agoric/cosmic-proto';
import { ChainAddressShape, CoinShape } from '../typeGuards.js';

/**
* @import {IcaAccount, ChainAddress, ChainAmount, CosmosValidatorAddress, ICQConnection} from '../types.js';
* @import {IcaAccount, ChainAddress, CoinAmount, CosmosValidatorAddress, ICQConnection} 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 @@ -85,7 +85,7 @@ export const tryDecodeResponse = (ackStr, fromProtoMsg) => {
}
};

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

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

/**
* @param {CosmosValidatorAddress} validator
* @returns {Promise<ChainAmount[]>}
* @returns {Promise<CoinAmount[]>}
*/
async withdrawReward(validator) {
const { chainAddress } = this.state;
Expand All @@ -239,8 +239,8 @@ export const prepareStakingAccountKit = (baggage, makeRecorderKit, zcf) => {
return harden(coins.map(toChainAmount));
},
/**
* @param {ChainAmount['denom']} [denom] - defaults to bondDenom
* @returns {Promise<ChainAmount>}
* @param {CoinAmount['denom']} [denom] - defaults to bondDenom
* @returns {Promise<CoinAmount>}
*/
async getBalance(denom) {
const { chainAddress, icqConnection, bondDenom } = this.state;
Expand Down
91 changes: 67 additions & 24 deletions packages/orchestration/src/orchestration-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
*/
import type { Amount, Brand } from '@agoric/ertp/exported.js';
import type { LocalChainAccount } from '@agoric/vats/src/localchain.js';
import type { Chain, ChainAmount, Denom, KnownChains } from './types.js';
import type {
AmountArg,
Chain,
CoinAmount,
DenomArg,
KnownChains,
TransferMsg,
} from './types.js';

/** An address on some blockchain, e.g., cosmos, eth, etc. */
export type ChainAddress = {
Expand All @@ -25,32 +32,68 @@ export interface Orchestrator {
getChain: <C extends keyof KnownChains>(chainName: C) => Promise<Chain<C>>;

makeLocalAccount: () => Promise<LocalChainAccount>;
}

/**
* An object that supports high-level operations for an account on a remote chain.
*/
export interface OrchestrationAccountI {
/**
* @returns the address of the account on the remote chain
*/
getAddress: () => ChainAddress;

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

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

/**
* For a denom, return information about a denom including the equivalent
* local Brand, the Chain on which the denom is held, and the Chain that
* issues the corresponding asset.
* @param denom
* Transfer amount to another account on the same chain. The promise settles when the transfer is complete.
* @param toAccount - the account to send the amount to. MUST be on the same chain
* @param amount - the amount to send
* @returns void
*/
getBrandInfo: <
HoldingChain extends keyof KnownChains,
IssuingChain extends keyof KnownChains,
>(
denom: Denom,
) => {
/** The well-known Brand on Agoric for the direct asset */
brand?: Brand;
/** The Chain at which the argument `denom` exists (where the asset is currently held) */
chain: Chain<HoldingChain>;
/** The Chain that is the issuer of the underlying asset */
base: Chain<IssuingChain>;
/** the Denom for the underlying asset on its issuer chain */
baseDenom: Denom;
};
send: (toAccount: ChainAddress, amount: AmountArg) => Promise<void>;

/**
* Transfer an amount to another account, typically on another chain.
* The promise settles when the transfer is complete.
* @param amount - the amount to transfer.
* @param destination - the account to transfer the amount to.
* @param memo - an optional memo to include with the transfer, which could drive custom PFM behavior
* @returns void
*
* TODO document the mapping from the address to the destination chain.
*/
transfer: (
amount: AmountArg,
destination: ChainAddress,
memo?: string,
) => Promise<void>;

// ??? Does this make sense at the chain agnostic level? Or does it assume IBC?
// More broadly, do we need to support Orchestration that isn't over IBC?
/**
* Convert an amount described in native data to a local, structured Amount.
* @param amount - the described amount
* @returns the Amount in local structuerd format
* Transfer an amount to another account in multiple steps. The promise settles when
* the entire path of the transfer is complete.
* @param amount - the amount to transfer
* @param msg - the transfer message, including follow-up steps
* @returns void
*/
asAmount: (amount: ChainAmount) => Amount;
transferSteps: (amount: AmountArg, msg: TransferMsg) => Promise<void>;
/**
* deposit payment from zoe to the account. For remote accounts,
* an IBC Transfer will be executed to transfer funds there.
*/
deposit: (payment: Payment) => Promise<void>;
}

export type AfterAction = { destChain: string; destAddress: ChainAddress };
export type SwapExact = { amountIn: Amount; amountOut: Amount };
export type SwapMaxSlippage = {
amountIn: Amount;
brandOut: Brand;
slippage: number;
};
Loading

0 comments on commit 48f62fd

Please sign in to comment.