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

Upgrade cosmos and thorchain to use 0.42.14 cosmos-client #436

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
40950ad
interim commit
mikewyszinski Sep 8, 2021
6e93f7f
Merge branch 'master' into issue-398/upgrade-cosmos
mikewyszinski Sep 9, 2021
86bc04b
interim commit
mikewyszinski Sep 10, 2021
107d0f3
interim commit
mikewyszinski Sep 10, 2021
f6290e5
Merge branch 'master' into issue-398/upgrade-cosmos
mikewyszinski Sep 10, 2021
5f8d427
cosmos tests passing
mikewyszinski Sep 10, 2021
81d08e5
fix eth compilation error
mikewyszinski Sep 10, 2021
068eaed
re-added cosmos type guards
mikewyszinski Sep 12, 2021
b230ddb
still one thorchain test not passing
mikewyszinski Sep 13, 2021
554d7ed
all tests passing but cleanup required
mikewyszinski Sep 13, 2021
70de241
added integration tests
mikewyszinski Sep 18, 2021
5d5c052
added rune xfer
mikewyszinski Sep 20, 2021
c1960df
added cosmos integration tests
mikewyszinski Sep 21, 2021
1f64f99
modifed cosmos tests
mikewyszinski Sep 22, 2021
6758c1c
ready for review
mikewyszinski Sep 24, 2021
ad68ce2
Merge branch 'master' into issue-398/upgrade-cosmos
mikewyszinski Sep 24, 2021
8d30046
fixed some code review issues
mikewyszinski Sep 29, 2021
d561301
Merge branch 'master' into issue-398/upgrade-cosmos
mikewyszinski Sep 30, 2021
ba4b286
addressed code review items
mikewyszinski Sep 30, 2021
07fc715
modified readme
mikewyszinski Sep 30, 2021
de3bb65
skipping cosmos failing test
mikewyszinski Sep 30, 2021
992c737
deposit and rune transfer still not working
mikewyszinski Oct 1, 2021
046cdb2
fixed unit tests
mikewyszinski Oct 1, 2021
dbf4f30
commented out deposit
mikewyszinski Oct 1, 2021
563eddd
fixed testnet settings
mikewyszinski Mar 11, 2022
94d94f1
change testnet id
the-eridanus Mar 11, 2022
3fb3d63
ignore lint for now
the-eridanus Mar 12, 2022
1e608fc
fix cosmos e2e tests
the-eridanus Mar 13, 2022
9d84c23
fix most e2e tests
the-eridanus Mar 14, 2022
6d4aa70
fix some more tests
the-eridanus Mar 14, 2022
3fce1ea
fix tests
the-eridanus Mar 14, 2022
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
21 changes: 17 additions & 4 deletions packages/xchain-cosmos/src/cosmos/sdk-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TxHistoryParams } from '@xchainjs/xchain-client'
import { TxHash, TxHistoryParams } from '@xchainjs/xchain-client'
import * as xchainCrypto from '@xchainjs/xchain-crypto'
import { baseAmount } from '@xchainjs/xchain-util/lib'
import axios from 'axios'
import * as BIP32 from 'bip32'
import { cosmosclient, proto, rest } from 'cosmos-client'
Expand All @@ -19,6 +20,7 @@ import {
TxResponse,
} from './types'

const DEFAULT_GAS_VALUE = baseAmount('200000', 8)
export class CosmosSDKClient {
sdk: cosmosclient.CosmosSDK

Expand Down Expand Up @@ -194,7 +196,15 @@ export class CosmosSDKClient {
return (await axios.get<TxResponse>(`${this.server}/txs/${hash}`)).data
}

async transfer({ privkey, from, to, amount, asset, memo = '', fee = '200000' }: TransferParams): Promise<string> {
async transfer({
privkey,
from,
to,
amount,
asset,
memo = '',
gasLimit = DEFAULT_GAS_VALUE,
}: TransferParams): Promise<TxHash> {
this.setPrefix()

const msgSend = new proto.cosmos.bank.v1beta1.MsgSend({
Expand Down Expand Up @@ -228,7 +238,7 @@ export class CosmosSDKClient {
},
],
fee: {
gas_limit: cosmosclient.Long.fromString(fee),
gas_limit: cosmosclient.Long.fromString(gasLimit.amount.toString()),
},
})

Expand Down Expand Up @@ -260,6 +270,9 @@ export class CosmosSDKClient {
tx_bytes: txBuilder.txBytes(),
mode: rest.cosmos.tx.BroadcastTxMode.Block,
})
return res.data.tx_response?.txhash || 'unknown'
if (!res?.data?.tx_response?.txhash) {
throw new Error('No tx id returned from broadcast')
}
return res.data.tx_response.txhash
}
}
3 changes: 2 additions & 1 deletion packages/xchain-cosmos/src/cosmos/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BaseAmount } from '@xchainjs/xchain-util/lib'
import { BigSource } from 'big.js'
import { proto } from 'cosmos-client'
import { StdTx } from 'cosmos-client/cjs/openapi/api'
Expand Down Expand Up @@ -26,7 +27,7 @@ export type TransferParams = {
amount: BigSource
asset: string
memo?: string
fee?: string
gasLimit?: BaseAmount
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gasLimit?: BaseAmount` -> `gasLimit?: BigNumber

(Sorry, I was wrong with a previous review comment it needs to be a BaseAmount, but still a BigNumber and not a string)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i ended up going with this, which is what other clients use for amount/gasLimit

export type TransferParams = {
  privkey: proto.cosmos.crypto.secp256k1.PrivKey
  from: string
  to: string
  amount: BaseAmount
  asset: string
  memo?: string
  gasLimit?: BigNumber
}

}

export type BaseAccountResponse = {
Expand Down
22 changes: 10 additions & 12 deletions packages/xchain-cosmos/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ export const DECIMAL = 6
* @param {Msg} msg
* @returns {boolean} `true` or `false`.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const isMsgSend = (msg: any): msg is proto.cosmos.bank.v1beta1.MsgSend =>
export const isMsgSend = (msg: unknown): msg is proto.cosmos.bank.v1beta1.MsgSend =>
(msg as proto.cosmos.bank.v1beta1.MsgSend)?.amount !== undefined &&
(msg as proto.cosmos.bank.v1beta1.MsgSend)?.from_address !== undefined &&
(msg as unknown as proto.cosmos.bank.v1beta1.MsgSend)?.to_address !== undefined
((msg as unknown) as proto.cosmos.bank.v1beta1.MsgSend)?.to_address !== undefined
veado marked this conversation as resolved.
Show resolved Hide resolved

/**
* Type guard for MsgMultiSend
*
* @param {Msg} msg
* @returns {boolean} `true` or `false`.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const isMsgMultiSend = (msg: any): msg is proto.cosmos.bank.v1beta1.MsgMultiSend =>
export const isMsgMultiSend = (msg: unknown): msg is proto.cosmos.bank.v1beta1.MsgMultiSend =>
(msg as proto.cosmos.bank.v1beta1.MsgMultiSend)?.inputs !== undefined &&
(msg as proto.cosmos.bank.v1beta1.MsgMultiSend)?.outputs !== undefined

Expand Down Expand Up @@ -75,7 +73,7 @@ const getCoinAmount = (coins?: proto.cosmos.base.v1beta1.ICoin[]) => {
export const getTxsFromHistory = (txs: TxResponse[], mainAsset: Asset): Tx[] => {
return txs.reduce((acc, tx) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let msgs: any[]
let msgs: Array<proto.cosmos.bank.v1beta1.MsgSend | proto.cosmos.bank.v1beta1.MsgMultiSend>
if ((tx.tx as RawTxResponse).body === undefined) {
msgs = codec.packCosmosAny(tx.tx).msg
} else {
Expand All @@ -86,18 +84,18 @@ export const getTxsFromHistory = (txs: TxResponse[], mainAsset: Asset): Tx[] =>
const to: TxTo[] = []
msgs.map((msg) => {
if (isMsgSend(msg)) {
const msgSend = msg as proto.cosmos.bank.v1beta1.MsgSend
const msgSend = msg
const amount = getCoinAmount(msgSend.amount)

let from_index = -1

from.forEach((value, index) => {
if (value.from === msgSend.from_address.toString()) from_index = index
if (value.from === msgSend.from_address) from_index = index
})

if (from_index === -1) {
from.push({
from: msgSend.from_address.toString(),
from: msgSend.from_address,
amount,
})
} else {
Expand All @@ -107,19 +105,19 @@ export const getTxsFromHistory = (txs: TxResponse[], mainAsset: Asset): Tx[] =>
let to_index = -1

to.forEach((value, index) => {
if (value.to === msgSend.to_address.toString()) to_index = index
if (value.to === msgSend.to_address) to_index = index
})

if (to_index === -1) {
to.push({
to: msgSend.to_address.toString(),
to: msgSend.to_address,
amount,
})
} else {
to[to_index].amount = baseAmount(to[to_index].amount.amount().plus(amount.amount()), 6)
}
} else if (isMsgMultiSend(msg)) {
const msgMultiSend = msg as proto.cosmos.bank.v1beta1.MsgMultiSend
const msgMultiSend = msg

msgMultiSend.inputs.map((input) => {
const amount = getCoinAmount(input.coins || [])
Expand Down
6 changes: 1 addition & 5 deletions packages/xchain-ethereum/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,12 +715,8 @@ export default class Client extends BaseXChainClient implements XChainClient, Et
[FeeOption.Fastest]: baseAmount(ratesInGwei[FeeOption.Fastest] * 10 ** 9, ETH_DECIMAL),
}
} catch (error) {}
//should only get here if thor fails
// try {

return await this.estimateGasPricesFromEtherscan()
// } catch (error) {
// throw new Error(`Failed to estimate gas price: ${error.msg ?? error.toString()}`)
// }
}

/**
Expand Down
6 changes: 1 addition & 5 deletions packages/xchain-thorchain/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ class Client extends BaseXChainClient implements ThorchainClient, XChainClient {
const txMinHeight = undefined
const txMaxHeight = undefined

// registerCodecs(getPrefix(this.network))

const txIncomingHistory = (
await this.cosmosClient.searchTxFromRPC({
rpcEndpoint: this.getClientUrl().rpc,
Expand Down Expand Up @@ -421,8 +419,6 @@ class Client extends BaseXChainClient implements ThorchainClient, XChainClient {
* @returns {TxHash} The transaction hash.
*/
async transfer({ walletIndex = 0, asset = AssetRuneNative, amount, recipient, memo }: TxParams): Promise<TxHash> {
// registerCodecs(getPrefix(this.network))

const assetBalance = await this.getBalance(this.getAddress(walletIndex), [asset])
const fee = await this.getFees()
if (
Expand All @@ -439,7 +435,7 @@ class Client extends BaseXChainClient implements ThorchainClient, XChainClient {
amount: amount.amount().toString(),
asset: getDenom(asset),
memo,
fee: DEFAULT_GAS_VALUE,
gasLimit: fee.fast,
veado marked this conversation as resolved.
Show resolved Hide resolved
})

if (!hash) {
Expand Down
5 changes: 1 addition & 4 deletions packages/xchain-thorchain/src/types/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export type MsgCoin = {
asset: string
amount: string
}
export class Msg {}

export type StdSignature = {
pub_key: cosmosclient.PubKey
Expand All @@ -16,14 +15,12 @@ export interface StdTxFee {
amount?: Array<Coin>
}

export class MsgNativeTx extends Msg {
export class MsgNativeTx {
coins: MsgCoin[]
memo: string
signer: cosmosclient.AccAddress

constructor(coins: MsgCoin[], memo: string, signer: cosmosclient.AccAddress) {
super()

this.coins = coins
this.memo = memo
this.signer = signer
Expand Down
10 changes: 2 additions & 8 deletions packages/xchain-thorchain/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,7 @@ export const getChainId = () => 'thorchain'
*/

export const registerCodecs = async (): Promise<void> => {
// const root = await protobuf.load('bundle.json')
// const MsgDeposit = root.lookupType('thorchain/MsgDeposit')
// console.log(MsgDeposit)
// proto.cosmos.bank.v1beta1.
codec.register('thorchain/MsgDeposit', types.types.MsgDeposit)
// )
// codec.register('thorchain/MsgMultiSend', MsgMultiSend, MsgMultiSend.fromJSON)
}

/**
Expand Down Expand Up @@ -182,13 +176,13 @@ export const buildDepositTx = async (msgNativeTx: MsgNativeTx, nodeUrl: string):

const fee: StdTxFee = response.value?.fee ?? { amount: [] }

const unsignedStdTx = {
const unsignedStdTx = ({
msg: response.value.msg,
// override fee
fee: { ...fee, gas: DEPOSIT_GAS_VALUE },
signature: [],
memo: '',
} as unknown as StdTx
} as unknown) as StdTx

return unsignedStdTx
}
Expand Down