Moving addCoins() to amino/coins

This commit is contained in:
Milan Steiner 2022-04-13 12:51:12 +02:00
parent 342210aac8
commit daa484d03e
3 changed files with 16 additions and 18 deletions

View File

@ -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,
};
}

View File

@ -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,

View File

@ -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,
};
}
}