From d7c1a80027d2af8ce5a8e397971e57f6ce76981e Mon Sep 17 00:00:00 2001 From: Troy Kessler <43882936+troykessler@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:23:19 +0200 Subject: [PATCH] Coins Improvements (#140) * chore: improved README and export parseCoins and Coin type * fix: README * chore: increase version --- common/coins/README.md | 48 ++++++++++++++++++++++++++++++++------- common/coins/package.json | 2 +- common/coins/src/index.ts | 2 ++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/common/coins/README.md b/common/coins/README.md index 2eee6627..9153223e 100644 --- a/common/coins/README.md +++ b/common/coins/README.md @@ -16,21 +16,53 @@ npm i @kyvejs/coins Here are some examples on how to use the coins sdk: +Arithmetic methods: + ```ts -const coinsA = new Coins({"denom": "acoin", amount: "10"}, {"denom": "bcoin", amount: "20"}) -const coinsB = new Coins("30ccoin") +new Coins("1acoin,2bcoin").add({denom: "ccoin", amount: "3"}).mul(2).toString() +-> "2acoin,4bcoin,6ccoin" + +new Coins("1acoin,2bcoin,4ccoin").sub("1acoin").div(2).toArray() +-> [{denom: "bcoin", amount: "1"}, {denom: "ccoin", amount: "2"}] +``` + +Checks and info methods: -coinsA.add(coinsB).quo(10).toString() --> "1acoin,2bcoin,3ccoin" +```ts +const myCoins = new Coins("1acoin,2bcoin") -coinsA.equal(coinsB) +myCoins.isZero() -> false -coinsA.max(coinsB).toArray() --> [{"denom": "acoin", amount: "10"}, {"denom": "bcoin", amount: "20"}, {"denom": "ccoin", amount: "30"}] +myCoins.len() +-> 2 + +myCoins.equal("1acoin,2bcoin") +-> true + +myCoins.amountOf("bcoin") +-> "2" + +myCoins.min("1bcoin,1ccoin").toString() +-> "1bcoin" + +myCoins.max("1bcoin,1ccoin").toString() +-> "1acoin,2bcoin,1ccoin" +``` + +Comparison methods: + +```ts +const myCoins = new Coins({denom: "bcoin", amount: "1"}, {denom: "ccoin", amount: "2"}) + +new Coins("1acoin,2bcoin,3ccoin").isAllGT(myCoins) +-> true + +new Coins("2bcoin,1ccoin").isAnyLT(myCoins) +-> true ``` -## Available methods +## All available methods The following methods are available on the Coins class: diff --git a/common/coins/package.json b/common/coins/package.json index 91cdaf97..893dbdf5 100644 --- a/common/coins/package.json +++ b/common/coins/package.json @@ -1,6 +1,6 @@ { "name": "@kyvejs/coins", - "version": "1.0.0", + "version": "1.0.1", "main": "./dist/index.js", "types": "./dist/index.d.ts", "files": [ diff --git a/common/coins/src/index.ts b/common/coins/src/index.ts index 2e021e5a..32afa28a 100644 --- a/common/coins/src/index.ts +++ b/common/coins/src/index.ts @@ -585,3 +585,5 @@ export class Coins { .join(","); } } + +export { parseCoins, Coin } from "@cosmjs/amino";