Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xchain-cosmos|thorchain] Finalize ATOM support - bump pgks #602

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/xchain-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# v.0.18.0 (2022-xx-xx)
# v.0.18.0 (2022-06-22)

## Add

Expand Down Expand Up @@ -31,6 +31,7 @@
- Remove deprecated `AssetMuon`
- Remove deprecated `Client.getMainAsset`
- Remove deprecated `BaseAccountResponse`
- Rename `DECIMAL` -> `COSMOS_DECIMAL`

# v.0.17.0 (2022-03-23)

Expand Down
10 changes: 5 additions & 5 deletions packages/xchain-cosmos/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseAmount, baseAmount } from '@xchainjs/xchain-util'
import nock from 'nock'

import { Client } from '../src/client'
import { AssetAtom } from '../src/const'
import { AssetAtom, COSMOS_DECIMAL } from '../src/const'
import { GetTxByHashResponse, TxHistoryResponse } from '../src/cosmos/types'

const getClientUrl = (client: Client): string => {
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('Client Test', () => {
})
const balances = await cosmosClient.getBalance('cosmos1gehrq0pr5d79q8nxnaenvqh09g56jafm82thjv')

const expected = balances[0].amount.amount().isEqualTo(baseAmount(75000000, 6).amount())
const expected = balances[0].amount.amount().isEqualTo(baseAmount(75000000, COSMOS_DECIMAL).amount())
expect(expected).toBeTruthy()
expect(balances[0].asset).toEqual(AssetAtom)
})
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('Client Test', () => {

it('transfer', async () => {
const to_address = 'cosmos1gehrq0pr5d79q8nxnaenvqh09g56jafm82thjv'
const send_amount: BaseAmount = baseAmount(10000, 6)
const send_amount: BaseAmount = baseAmount(10000, COSMOS_DECIMAL)
const memo = 'transfer'

const expected_txsPost_result = {
Expand Down Expand Up @@ -332,9 +332,9 @@ describe('Client Test', () => {
expect(tx.type).toEqual('transfer')
expect(tx.hash).toEqual('19BFC1E8EBB10AA1EC6B82E380C6F5FD349D367737EA8D55ADB4A24F0F7D1066')
expect(tx.from[0].from).toEqual('cosmos1pjkpqxmvz47a5aw40l98fyktlg7k6hd9heq95z')
expect(tx.from[0].amount.amount().isEqualTo(baseAmount(4318994970, 6).amount())).toBeTruthy()
expect(tx.from[0].amount.amount().isEqualTo(baseAmount(4318994970, COSMOS_DECIMAL).amount())).toBeTruthy()
expect(tx.to[0].to).toEqual('cosmos155svs6sgxe55rnvs6ghprtqu0mh69kehrn0dqr')
expect(tx.to[0].amount.amount().isEqualTo(baseAmount(4318994970, 6).amount())).toBeTruthy()
expect(tx.to[0].amount.amount().isEqualTo(baseAmount(4318994970, COSMOS_DECIMAL).amount())).toBeTruthy()
})

it('should return valid explorer url', () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/xchain-cosmos/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { proto } from '@cosmos-client/core'
import { baseAmount, eqAsset } from '@xchainjs/xchain-util'

import { AssetAtom } from '../src/const'
import { AssetAtom, COSMOS_DECIMAL } from '../src/const'
import { APIQueryParam, RawTxResponse, TxResponse } from '../src/cosmos/types'
import { getAsset, getDenom, getQueryString, getTxsFromHistory, isMsgMultiSend, isMsgSend } from '../src/util'

Expand Down Expand Up @@ -147,18 +147,18 @@ describe('cosmos/util', () => {
expect(parsed_txs[0].asset).toEqual(AssetAtom)
expect(parsed_txs[0].from.length).toEqual(1)
expect(parsed_txs[0].from[0].from).toEqual(from_address)
expect(parsed_txs[0].from[0].amount.amount().isEqualTo(baseAmount(2000, 6).amount())).toBeTruthy()
expect(parsed_txs[0].from[0].amount.amount().isEqualTo(baseAmount(2000, COSMOS_DECIMAL).amount())).toBeTruthy()
expect(parsed_txs[0].to.length).toEqual(1)
expect(parsed_txs[0].to[0].to).toEqual(to_address)
expect(parsed_txs[0].to[0].amount.amount().isEqualTo(baseAmount(2000, 6).amount())).toBeTruthy()
expect(parsed_txs[0].to[0].amount.amount().isEqualTo(baseAmount(2000, COSMOS_DECIMAL).amount())).toBeTruthy()

expect(parsed_txs[1].asset).toEqual(AssetAtom)
expect(parsed_txs[1].from.length).toEqual(1)
expect(parsed_txs[1].from[0].from).toEqual(from_address)
expect(parsed_txs[1].from[0].amount.amount().isEqualTo(baseAmount(2000, 6).amount())).toBeTruthy()
expect(parsed_txs[1].from[0].amount.amount().isEqualTo(baseAmount(2000, COSMOS_DECIMAL).amount())).toBeTruthy()
expect(parsed_txs[1].to.length).toEqual(1)
expect(parsed_txs[1].to[0].to).toEqual(to_address)
expect(parsed_txs[1].to[0].amount.amount().isEqualTo(baseAmount(2000, 6).amount())).toBeTruthy()
expect(parsed_txs[1].to[0].amount.amount().isEqualTo(baseAmount(2000, COSMOS_DECIMAL).amount())).toBeTruthy()
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/xchain-cosmos/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-cosmos",
"version": "0.18.0-alpha.2",
"version": "0.18.0",
"description": "Custom Cosmos client and utilities used by XChainJS clients",
"keywords": [
"XChain",
Expand Down
11 changes: 8 additions & 3 deletions packages/xchain-cosmos/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Asset, BaseAmount, Chain, baseAmount, eqAsset } from '@xchainjs/xchain-
import BigNumber from 'bignumber.js'
import Long from 'long'

import { AssetAtom, DECIMAL, DEFAULT_FEE, DEFAULT_GAS_LIMIT } from './const'
import { AssetAtom, COSMOS_DECIMAL, DEFAULT_FEE, DEFAULT_GAS_LIMIT } from './const'
import { CosmosSDKClient } from './cosmos/sdk-client'
import { TxOfflineParams } from './cosmos/types'
import { ChainIds, ClientUrls, CosmosClientParams } from './types'
Expand Down Expand Up @@ -180,7 +180,7 @@ class Client extends BaseXChainClient implements CosmosClient, XChainClient {
const balances = coins
.reduce((acc: Balance[], { denom, amount }) => {
const asset = getAsset(denom)
return asset ? [...acc, { asset, amount: baseAmount(amount || '0', DECIMAL) }] : acc
return asset ? [...acc, { asset, amount: baseAmount(amount || '0', COSMOS_DECIMAL) }] : acc
}, [])
.filter(({ asset: balanceAsset }) => !assets || assets.filter((asset) => eqAsset(balanceAsset, asset)).length)

Expand Down Expand Up @@ -328,7 +328,12 @@ class Client extends BaseXChainClient implements CosmosClient, XChainClient {
async getFees(): Promise<Fees> {
try {
const feeRate = await this.getFeeRateFromThorchain()
const fee = baseAmount(feeRate, DECIMAL)
// convert decimal: 1e8 (THORChain) to 1e6 (COSMOS)
// Similar to `fromCosmosToThorchain` in THORNode
// @see https://gitlab.com/thorchain/thornode/-/blob/e787022028f662b3a7c594e4a65aca618caa359c/bifrost/pkg/chainclients/gaia/util.go#L86
const decimalDiff = COSMOS_DECIMAL - 8 /* THORCHAIN_DECIMAL */
const feeRate1e6 = feeRate * 10 ** decimalDiff
const fee = baseAmount(feeRate1e6, COSMOS_DECIMAL)
return singleFee(FeeType.FlatFee, fee)
} catch (error) {
return singleFee(FeeType.FlatFee, DEFAULT_FEE)
Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-cosmos/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Asset, Chain, baseAmount } from '@xchainjs/xchain-util'
/**
* The decimal for cosmos chain.
*/
export const DECIMAL = 6
export const COSMOS_DECIMAL = 6

export const AssetAtom: Asset = { chain: Chain.Cosmos, symbol: 'ATOM', ticker: 'ATOM', synth: false }

Expand All @@ -19,4 +19,4 @@ export const DEFAULT_GAS_LIMIT = '200000'
* As same as definition in Cosmosstation's web wallet
* @see https://github.com/cosmostation/web-wallet-ts-react/blob/4d78718b613defbd6c92079b33aa8ce9f86d597c/src/constants/chain.ts#L66
*/
export const DEFAULT_FEE = baseAmount(5000, DECIMAL)
export const DEFAULT_FEE = baseAmount(5000, COSMOS_DECIMAL)
19 changes: 11 additions & 8 deletions packages/xchain-cosmos/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { proto } from '@cosmos-client/core'
import { FeeType, Fees, Network, Tx, TxFrom, TxTo, TxType } from '@xchainjs/xchain-client'
import { Asset, BaseAmount, CosmosChain, baseAmount, eqAsset } from '@xchainjs/xchain-util'

import { AssetAtom, DECIMAL } from './const'
import { AssetAtom, COSMOS_DECIMAL } from './const'
import { APIQueryParam, TxResponse } from './cosmos/types'
import { ChainIds, ClientUrls as ClientUrls } from './types'

Expand Down Expand Up @@ -70,8 +70,8 @@ export const getAsset = (denom: string): Asset | null => {
*/
const getCoinAmount = (coins: proto.cosmos.base.v1beta1.ICoin[]): BaseAmount =>
coins
.map((coin) => baseAmount(coin.amount || 0, DECIMAL))
.reduce((acc, cur) => baseAmount(acc.amount().plus(cur.amount()), DECIMAL), baseAmount(0, DECIMAL))
.map((coin) => baseAmount(coin.amount || 0, COSMOS_DECIMAL))
.reduce((acc, cur) => baseAmount(acc.amount().plus(cur.amount()), COSMOS_DECIMAL), baseAmount(0, COSMOS_DECIMAL))

/**
* Filters `ICoin[]` by given `Asset`
Expand Down Expand Up @@ -126,7 +126,10 @@ export const getTxsFromHistory = (txs: TxResponse[], asset: Asset): Tx[] => {
amount,
})
} else {
from[from_index].amount = baseAmount(from[from_index].amount.amount().plus(amount.amount()), DECIMAL)
from[from_index].amount = baseAmount(
from[from_index].amount.amount().plus(amount.amount()),
COSMOS_DECIMAL,
)
}

let to_index = -1
Expand All @@ -141,7 +144,7 @@ export const getTxsFromHistory = (txs: TxResponse[], asset: Asset): Tx[] => {
amount,
})
} else {
to[to_index].amount = baseAmount(to[to_index].amount.amount().plus(amount.amount()), DECIMAL)
to[to_index].amount = baseAmount(to[to_index].amount.amount().plus(amount.amount()), COSMOS_DECIMAL)
}
}
})
Expand Down Expand Up @@ -182,9 +185,9 @@ export const getQueryString = (params: APIQueryParam): string => {
export const getDefaultFees = (): Fees => {
return {
type: FeeType.FlatFee,
fast: baseAmount(750, DECIMAL),
fastest: baseAmount(2500, DECIMAL),
average: baseAmount(0, DECIMAL),
fast: baseAmount(750, COSMOS_DECIMAL),
fastest: baseAmount(2500, COSMOS_DECIMAL),
average: baseAmount(0, COSMOS_DECIMAL),
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-thorchain/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# v0.25.2 (2022-xx-xx)
# v0.25.2 (2022-06-22)

## Update

- Latest `@cosmos-client/core@0.45.10`
- Latest "xchain-cosmos@0.18.0-alpha.2"
- Latest "xchain-cosmos@0.18.0"

## Fix

Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-thorchain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-thorchain",
"version": "0.25.2-alpha.1",
"version": "0.25.2",
"description": "Custom Thorchain client and utilities used by XChainJS clients",
"keywords": [
"THORChain",
Expand Down Expand Up @@ -50,7 +50,7 @@
"peerDependencies": {
"@cosmos-client/core": "^0.45.10",
"@xchainjs/xchain-client": "^0.11.1",
"@xchainjs/xchain-cosmos": "^0.18.0-alpha.2",
"@xchainjs/xchain-cosmos": "^0.18.0",
"@xchainjs/xchain-crypto": "^0.2.4",
"@xchainjs/xchain-util": "^0.5.1",
"axios": "^0.25.0",
Expand Down