mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Make TokenInfo free from BCP types
This commit is contained in:
parent
fb9160f258
commit
eb72f0e175
@ -9,7 +9,6 @@ import {
|
||||
SignableBytes,
|
||||
SignedTransaction,
|
||||
SigningJob,
|
||||
TokenTicker,
|
||||
TransactionId,
|
||||
TxCodec,
|
||||
UnsignedTransaction,
|
||||
@ -107,8 +106,7 @@ const defaultPrefix = "cosmos" as CosmosBech32Prefix;
|
||||
const defaultTokens: TokenInfos = [
|
||||
{
|
||||
fractionalDigits: 6,
|
||||
tokenName: "Atom (Cosmos Hub)",
|
||||
tokenTicker: "ATOM" as TokenTicker,
|
||||
ticker: "ATOM",
|
||||
denom: "uatom",
|
||||
},
|
||||
];
|
||||
|
@ -16,8 +16,8 @@ import { HdPaths, Secp256k1HdWallet, UserProfile } from "@iov/keycontrol";
|
||||
|
||||
import { CosmosBech32Prefix } from "./address";
|
||||
import { CosmWasmCodec, cosmWasmCodec } from "./cosmwasmcodec";
|
||||
import { CosmWasmConnection } from "./cosmwasmconnection";
|
||||
import { nonceToSequence, TokenInfos } from "./types";
|
||||
import { CosmWasmConnection, TokenConfiguration } from "./cosmwasmconnection";
|
||||
import { nonceToSequence } from "./types";
|
||||
|
||||
const { fromBase64, toHex } = Encoding;
|
||||
|
||||
@ -45,17 +45,17 @@ describe("CosmWasmConnection", () => {
|
||||
const defaultPrefix = "cosmos" as CosmosBech32Prefix;
|
||||
|
||||
// this is for wasmd blockchain
|
||||
const defaultTokens: TokenInfos = [
|
||||
const defaultTokens: TokenConfiguration = [
|
||||
{
|
||||
fractionalDigits: 6,
|
||||
tokenName: "Fee Token",
|
||||
tokenTicker: "COSM" as TokenTicker,
|
||||
name: "Fee Token",
|
||||
ticker: "COSM",
|
||||
denom: "ucosm",
|
||||
},
|
||||
{
|
||||
fractionalDigits: 6,
|
||||
tokenName: "Staking Token",
|
||||
tokenTicker: "STAKE" as TokenTicker,
|
||||
name: "Staking Token",
|
||||
ticker: "STAKE",
|
||||
denom: "ustake",
|
||||
},
|
||||
];
|
||||
|
@ -38,7 +38,7 @@ import { Stream } from "xstream";
|
||||
import { CosmosBech32Prefix, decodeCosmosPubkey, pubkeyToAddress } from "./address";
|
||||
import { Caip5 } from "./caip5";
|
||||
import { decodeAmount, parseTxsResponse } from "./decode";
|
||||
import { accountToNonce, TokenInfos } from "./types";
|
||||
import { accountToNonce, TokenInfo, TokenInfos } from "./types";
|
||||
|
||||
interface ChainData {
|
||||
readonly chainId: ChainId;
|
||||
@ -68,16 +68,18 @@ function buildQueryString({
|
||||
return components.filter(Boolean).join("&");
|
||||
}
|
||||
|
||||
export type TokenConfiguration = readonly (TokenInfo & { readonly name: string })[];
|
||||
|
||||
export class CosmWasmConnection implements BlockchainConnection {
|
||||
// we must know prefix and tokens a priori to understand the chain
|
||||
public static async establish(
|
||||
url: string,
|
||||
prefix: CosmosBech32Prefix,
|
||||
tokenInfo: TokenInfos,
|
||||
tokens: TokenConfiguration,
|
||||
): Promise<CosmWasmConnection> {
|
||||
const restClient = new RestClient(url);
|
||||
const chainData = await this.initialize(restClient);
|
||||
return new CosmWasmConnection(restClient, chainData, prefix, tokenInfo);
|
||||
return new CosmWasmConnection(restClient, chainData, prefix, tokens);
|
||||
}
|
||||
|
||||
private static async initialize(restClient: RestClient): Promise<ChainData> {
|
||||
@ -102,16 +104,16 @@ export class CosmWasmConnection implements BlockchainConnection {
|
||||
restClient: RestClient,
|
||||
chainData: ChainData,
|
||||
prefix: CosmosBech32Prefix,
|
||||
tokenInfo: TokenInfos,
|
||||
tokens: TokenConfiguration,
|
||||
) {
|
||||
this.restClient = restClient;
|
||||
this.chainData = chainData;
|
||||
this._prefix = prefix;
|
||||
this.tokenInfo = tokenInfo;
|
||||
this.tokenInfo = tokens;
|
||||
|
||||
this.supportedTokens = this.tokenInfo.map(info => ({
|
||||
tokenTicker: info.tokenTicker,
|
||||
tokenName: info.tokenName,
|
||||
this.supportedTokens = tokens.map(info => ({
|
||||
tokenTicker: info.ticker as TokenTicker,
|
||||
tokenName: info.name,
|
||||
fractionalDigits: info.fractionalDigits,
|
||||
}));
|
||||
this.primaryToken = this.supportedTokens[0];
|
||||
|
@ -2,8 +2,7 @@ import { ChainConnector, ChainId } from "@iov/bcp";
|
||||
|
||||
import { CosmosBech32Prefix } from "./address";
|
||||
import { CosmWasmCodec } from "./cosmwasmcodec";
|
||||
import { CosmWasmConnection } from "./cosmwasmconnection";
|
||||
import { TokenInfo } from "./types";
|
||||
import { CosmWasmConnection, TokenConfiguration } from "./cosmwasmconnection";
|
||||
|
||||
/**
|
||||
* A helper to connect to a cosmos-based chain at a given url
|
||||
@ -11,12 +10,12 @@ import { TokenInfo } from "./types";
|
||||
export function createCosmWasmConnector(
|
||||
url: string,
|
||||
prefix: CosmosBech32Prefix,
|
||||
tokenInfo: readonly TokenInfo[],
|
||||
tokens: TokenConfiguration,
|
||||
expectedChainId?: ChainId,
|
||||
): ChainConnector<CosmWasmConnection> {
|
||||
const codec = new CosmWasmCodec(prefix, tokenInfo);
|
||||
const codec = new CosmWasmCodec(prefix, tokens);
|
||||
return {
|
||||
establishConnection: async () => CosmWasmConnection.establish(url, prefix, tokenInfo),
|
||||
establishConnection: async () => CosmWasmConnection.establish(url, prefix, tokens),
|
||||
codec: codec,
|
||||
expectedChainId: expectedChainId,
|
||||
};
|
||||
|
@ -55,8 +55,7 @@ describe("decode", () => {
|
||||
const defaultTokens: TokenInfos = [
|
||||
{
|
||||
fractionalDigits: 6,
|
||||
tokenName: "Atom (Cosmos Hub)",
|
||||
tokenTicker: "ATOM" as TokenTicker,
|
||||
ticker: "ATOM",
|
||||
denom: "uatom",
|
||||
},
|
||||
];
|
||||
|
@ -44,8 +44,7 @@ describe("encode", () => {
|
||||
const defaultTokens: TokenInfos = [
|
||||
{
|
||||
fractionalDigits: 6,
|
||||
tokenName: "Atom (Cosmos Hub)",
|
||||
tokenTicker: "ATOM" as TokenTicker,
|
||||
ticker: "ATOM",
|
||||
denom: "uatom",
|
||||
},
|
||||
];
|
||||
|
@ -1,4 +1,4 @@
|
||||
export { CosmWasmCodec } from "./cosmwasmcodec";
|
||||
export { CosmWasmConnection } from "./cosmwasmconnection";
|
||||
export { CosmWasmConnection, TokenConfiguration } from "./cosmwasmconnection";
|
||||
export { createCosmWasmConnector } from "./cosmwasmconnector";
|
||||
export { TokenInfo } from "./types";
|
||||
|
@ -1,15 +1,26 @@
|
||||
import { Amount, Nonce, Token } from "@iov/bcp";
|
||||
import { Amount, Nonce, TokenTicker } from "@iov/bcp";
|
||||
import amino from "@tendermint/amino-js";
|
||||
|
||||
export interface TokenInfo extends Token {
|
||||
export interface TokenInfo {
|
||||
readonly denom: string;
|
||||
readonly ticker: string;
|
||||
/**
|
||||
* The number of fractional digits the token supports.
|
||||
*
|
||||
* A quantity is expressed as atomic units. 10^fractionalDigits of those
|
||||
* atomic units make up 1 token.
|
||||
*
|
||||
* E.g. in Ethereum 10^18 wei are 1 ETH and from the quantity 123000000000000000000
|
||||
* the last 18 digits are the fractional part and the rest the wole part.
|
||||
*/
|
||||
readonly fractionalDigits: number;
|
||||
}
|
||||
|
||||
export type TokenInfos = ReadonlyArray<TokenInfo>;
|
||||
|
||||
// TODO: return null vs throw exception for undefined???
|
||||
export function amountToCoin(lookup: ReadonlyArray<TokenInfo>, amount: Amount): amino.Coin {
|
||||
const match = lookup.find(({ tokenTicker }) => tokenTicker === amount.tokenTicker);
|
||||
const match = lookup.find(({ ticker }) => ticker === amount.tokenTicker);
|
||||
if (!match) {
|
||||
throw Error(`unknown ticker: ${amount.tokenTicker}`);
|
||||
}
|
||||
@ -26,7 +37,7 @@ export function coinToAmount(tokens: TokenInfos, coin: amino.Coin): Amount {
|
||||
throw Error(`unknown denom: ${coin.denom}`);
|
||||
}
|
||||
return {
|
||||
tokenTicker: match.tokenTicker,
|
||||
tokenTicker: match.ticker as TokenTicker,
|
||||
fractionalDigits: match.fractionalDigits,
|
||||
quantity: coin.amount,
|
||||
};
|
||||
|
7
packages/bcp/types/cosmwasmconnection.d.ts
vendored
7
packages/bcp/types/cosmwasmconnection.d.ts
vendored
@ -21,12 +21,15 @@ import {
|
||||
} from "@iov/bcp";
|
||||
import { Stream } from "xstream";
|
||||
import { CosmosBech32Prefix } from "./address";
|
||||
import { TokenInfos } from "./types";
|
||||
import { TokenInfo } from "./types";
|
||||
export declare type TokenConfiguration = readonly (TokenInfo & {
|
||||
readonly name: string;
|
||||
})[];
|
||||
export declare class CosmWasmConnection implements BlockchainConnection {
|
||||
static establish(
|
||||
url: string,
|
||||
prefix: CosmosBech32Prefix,
|
||||
tokenInfo: TokenInfos,
|
||||
tokens: TokenConfiguration,
|
||||
): Promise<CosmWasmConnection>;
|
||||
private static initialize;
|
||||
private readonly restClient;
|
||||
|
5
packages/bcp/types/cosmwasmconnector.d.ts
vendored
5
packages/bcp/types/cosmwasmconnector.d.ts
vendored
@ -1,13 +1,12 @@
|
||||
import { ChainConnector, ChainId } from "@iov/bcp";
|
||||
import { CosmosBech32Prefix } from "./address";
|
||||
import { CosmWasmConnection } from "./cosmwasmconnection";
|
||||
import { TokenInfo } from "./types";
|
||||
import { CosmWasmConnection, TokenConfiguration } from "./cosmwasmconnection";
|
||||
/**
|
||||
* A helper to connect to a cosmos-based chain at a given url
|
||||
*/
|
||||
export declare function createCosmWasmConnector(
|
||||
url: string,
|
||||
prefix: CosmosBech32Prefix,
|
||||
tokenInfo: readonly TokenInfo[],
|
||||
tokens: TokenConfiguration,
|
||||
expectedChainId?: ChainId,
|
||||
): ChainConnector<CosmWasmConnection>;
|
||||
|
2
packages/bcp/types/index.d.ts
vendored
2
packages/bcp/types/index.d.ts
vendored
@ -1,4 +1,4 @@
|
||||
export { CosmWasmCodec } from "./cosmwasmcodec";
|
||||
export { CosmWasmConnection } from "./cosmwasmconnection";
|
||||
export { CosmWasmConnection, TokenConfiguration } from "./cosmwasmconnection";
|
||||
export { createCosmWasmConnector } from "./cosmwasmconnector";
|
||||
export { TokenInfo } from "./types";
|
||||
|
16
packages/bcp/types/types.d.ts
vendored
16
packages/bcp/types/types.d.ts
vendored
@ -1,8 +1,18 @@
|
||||
import { Amount, Nonce, Token } from "@iov/bcp";
|
||||
import { Amount, Nonce } from "@iov/bcp";
|
||||
import amino from "@tendermint/amino-js";
|
||||
export declare function isAminoStdTx(txValue: amino.TxValue): txValue is amino.StdTx;
|
||||
export interface TokenInfo extends Token {
|
||||
export interface TokenInfo {
|
||||
readonly denom: string;
|
||||
readonly ticker: string;
|
||||
/**
|
||||
* The number of fractional digits the token supports.
|
||||
*
|
||||
* A quantity is expressed as atomic units. 10^fractionalDigits of those
|
||||
* atomic units make up 1 token.
|
||||
*
|
||||
* E.g. in Ethereum 10^18 wei are 1 ETH and from the quantity 123000000000000000000
|
||||
* the last 18 digits are the fractional part and the rest the wole part.
|
||||
*/
|
||||
readonly fractionalDigits: number;
|
||||
}
|
||||
export declare type TokenInfos = ReadonlyArray<TokenInfo>;
|
||||
export declare function amountToCoin(lookup: ReadonlyArray<TokenInfo>, amount: Amount): amino.Coin;
|
||||
|
@ -1,18 +1,18 @@
|
||||
import { CosmWasmCodec, CosmWasmConnection, TokenInfo } from "@cosmwasm/bcp";
|
||||
import { TokenTicker, TxCodec } from "@iov/bcp";
|
||||
import { CosmWasmCodec, CosmWasmConnection, TokenConfiguration } from "@cosmwasm/bcp";
|
||||
import { TxCodec } from "@iov/bcp";
|
||||
|
||||
const prefix = "cosmos";
|
||||
const tokens: readonly TokenInfo[] = [
|
||||
const tokens: TokenConfiguration = [
|
||||
{
|
||||
fractionalDigits: 6,
|
||||
tokenName: "Fee Token",
|
||||
tokenTicker: "COSM" as TokenTicker,
|
||||
name: "Fee Token",
|
||||
ticker: "COSM",
|
||||
denom: "cosm",
|
||||
},
|
||||
{
|
||||
fractionalDigits: 6,
|
||||
tokenName: "Staking Token",
|
||||
tokenTicker: "STAKE" as TokenTicker,
|
||||
name: "Staking Token",
|
||||
ticker: "STAKE",
|
||||
denom: "stake",
|
||||
},
|
||||
];
|
||||
|
Loading…
x
Reference in New Issue
Block a user