diff --git a/packages/amino/src/coins.ts b/packages/amino/src/coins.ts index 21554904..702fb2af 100644 --- a/packages/amino/src/coins.ts +++ b/packages/amino/src/coins.ts @@ -1,5 +1,4 @@ -import { Uint53, Uint64 } from "@cosmjs/math"; - +import { Decimal, Uint53, Uint64 } from "@cosmjs/math"; export interface Coin { readonly denom: string; readonly amount: string; @@ -68,3 +67,14 @@ export function parseCoins(input: string): Coin[] { }; }); } + +/** + * Function to sum up coins with type Coin + */ +export function addCoins(lhs: Coin, rhs: Coin): Coin { + if (lhs.denom !== rhs.denom) throw new Error("Trying to add two coins with different demoms"); + return { + amount: Decimal.fromAtomics(lhs.amount, 0).plus(Decimal.fromAtomics(rhs.amount, 0)).atomics, + denom: lhs.denom, + }; +} diff --git a/packages/amino/src/index.ts b/packages/amino/src/index.ts index aa235722..d273be77 100644 --- a/packages/amino/src/index.ts +++ b/packages/amino/src/index.ts @@ -4,7 +4,7 @@ export { rawEd25519PubkeyToRawAddress, rawSecp256k1PubkeyToRawAddress, } from "./addresses"; -export { Coin, coin, coins, parseCoins } from "./coins"; +export { addCoins, Coin, coin, coins, parseCoins } from "./coins"; export { decodeAminoPubkey, decodeBech32Pubkey, diff --git a/packages/stargate/src/stargateclient.ts b/packages/stargate/src/stargateclient.ts index 9a1ccf12..f97fe3c3 100644 --- a/packages/stargate/src/stargateclient.ts +++ b/packages/stargate/src/stargateclient.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/naming-convention */ +import { addCoins } from "@cosmjs/amino"; import { toHex } from "@cosmjs/encoding"; -import { Decimal, Uint53 } from "@cosmjs/math"; +import { Uint53 } from "@cosmjs/math"; import { HttpEndpoint, Tendermint34Client, toRfc3339WithNanoseconds } from "@cosmjs/tendermint-rpc"; import { assert, sleep } from "@cosmjs/utils"; import { MsgData } from "cosmjs-types/cosmos/base/abci/v1beta1/abci"; @@ -291,9 +292,7 @@ export class StargateClient { (previousValue: Coin | null, currentValue: DelegationResponse): Coin => { // Safe because field is set to non-nullable (https://github.com/cosmos/cosmos-sdk/blob/v0.45.3/proto/cosmos/staking/v1beta1/staking.proto#L295) assert(currentValue.balance); - return previousValue !== null - ? this.addCoins(previousValue, currentValue.balance) - : currentValue.balance; + return previousValue !== null ? addCoins(previousValue, currentValue.balance) : currentValue.balance; }, null, ); @@ -445,15 +444,4 @@ export class StargateClient { }; }); } - - /** - * Function to sum up coins with type Coin - */ - private addCoins(lhs: Coin, rhs: Coin): Coin { - if (lhs.denom !== rhs.denom) throw new Error("Trying to add two coins with different demoms"); - return { - amount: Decimal.fromAtomics(lhs.amount, 0).plus(Decimal.fromAtomics(rhs.amount, 0)).atomics, - denom: lhs.denom, - }; - } }