Skip to content

Commit

Permalink
Merge branch 'master' into 1012-mayachain-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
0xp3gasus committed Feb 5, 2024
2 parents 520af3e + 196b040 commit 9f405b3
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 71 deletions.
6 changes: 6 additions & 0 deletions packages/xchain-cosmos-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.2.2

### Patch Changes

- ae35c36: GetTransactionData for non native asset transactions

## 0.2.1

### Patch Changes
Expand Down
17 changes: 14 additions & 3 deletions packages/xchain-cosmos-sdk/__e2e__/cosmos-sdk-client.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { AssetInfo, Network, TxParams } from '@xchainjs/xchain-client'
import { StdFee } from '@cosmjs/stargate'
import { AssetInfo, Network, PreparedTx, TxParams } from '@xchainjs/xchain-client'
import { Asset, assetFromString, assetToString, baseAmount, eqAsset } from '@xchainjs/xchain-util'

import Client from '../src/client'

const AssetKUJI = assetFromString('KUJI.KUJI') as Asset
const AssetTokenKuji = {
chain: 'KUJI',
Expand All @@ -9,11 +12,18 @@ const AssetTokenKuji = {
synth: false,
}

import Client from '../src/client'

let xchainClient: Client

class CustomSdkClient extends Client {
public prepareTx(): Promise<PreparedTx> {
throw new Error('Method not implemented.')
}
protected getMsgTypeUrlByType(): string {
throw new Error('Method not implemented.')
}
protected getStandardFee(): StdFee {
throw new Error('Method not implemented.')
}
getAssetInfo(): AssetInfo {
throw new Error('Method not implemented.')
}
Expand Down Expand Up @@ -61,6 +71,7 @@ describe('Cosmos SDK client Integration Tests', () => {
[Network.Stagenet]: 'wip',
[Network.Mainnet]: 'wip',
},
registryTypes: [],
}
xchainClient = new CustomSdkClient(settings)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/xchain-cosmos-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-cosmos-sdk",
"version": "0.2.1",
"version": "0.2.2",
"description": "Genereic Cosmos SDK client for XChainJS",
"keywords": [
"XChain",
Expand Down
100 changes: 60 additions & 40 deletions packages/xchain-cosmos-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
singleFee,
} from '@xchainjs/xchain-client'
import * as xchainCrypto from '@xchainjs/xchain-crypto'
import { Address, Asset, BaseAmount, CachedValue, Chain, baseAmount } from '@xchainjs/xchain-util'
import { Address, Asset, BaseAmount, CachedValue, Chain, assetToString, baseAmount } from '@xchainjs/xchain-util'
import * as bech32 from 'bech32'
import * as BIP32 from 'bip32'
import * as crypto from 'crypto'
Expand Down Expand Up @@ -104,7 +104,7 @@ export default abstract class Client extends BaseXChainClient implements XChainC
private splitAmountAndDenom(amountsAndDenoms: string[]) {
const amounAndDenomParsed: { amount: string; denom: string }[] = []
amountsAndDenoms.forEach((amountAndDenom) => {
const regex = /^(\d+)(\D+)$/
const regex = /^(\d+)(.*)$/
const match = amountAndDenom.match(regex)

if (match) {
Expand All @@ -123,10 +123,15 @@ export default abstract class Client extends BaseXChainClient implements XChainC
* @returns {Tx} transaction with xchainjs format
*/
private async mapIndexedTxToTx(indexedTx: IndexedTx): Promise<Tx> {
const mapTo: Map<Address, { amount: BaseAmount; asset: Asset | undefined }> = new Map()
const mapFrom: Map<Address, { amount: BaseAmount; asset: Asset | undefined }> = new Map()
const mapTo: Map<string, { amount: BaseAmount; asset: Asset | undefined; address: Address }> = new Map()
const mapFrom: Map<string, { amount: BaseAmount; asset: Asset | undefined; address: Address }> = new Map()

indexedTx.events.forEach((event) => {
/**
* Approach to be compatible with other clients. Due to Cosmos transaction sorted events, the first 7 events
* belongs to the transaction fee, so they can be skipped
*/

indexedTx.events.slice(7).forEach((event) => {
if (event.type === 'transfer') {
const keyAmount = event.attributes.find((atribute) => atribute.key === 'amount') as {
key: string
Expand All @@ -143,60 +148,75 @@ export default abstract class Client extends BaseXChainClient implements XChainC
try {
const allTokensInEvent = keyAmount.value.split(',') // More than one asset per event (kuji faucet example)
const amounts = this.splitAmountAndDenom(allTokensInEvent)
const nativeAssetAmounts = amounts.filter((amount) => amount.denom === this.baseDenom) // TODO: Temporally discard non native assets
const totalNativeAmount = nativeAssetAmounts.reduce(
// TODO: Diferenciate fee from amount
(acum, amount) => acum.plus(amount.amount),
baseAmount(0, this.defaultDecimals),
)
// Fill to
if (mapTo.has(keyRecipient.value)) {
const currentTo = mapTo.get(keyRecipient.value) as { amount: BaseAmount; asset: Asset | undefined }
currentTo.amount = currentTo?.amount.plus(totalNativeAmount)
mapTo.set(keyRecipient.value, currentTo)
} else {
const asset = this.assetFromDenom(this.baseDenom)
if (asset) {
mapTo.set(keyRecipient.value, {
amount: totalNativeAmount,
asset,
})
const denomAmountMap: Record<string, BaseAmount> = {}
amounts.forEach((amount) => {
if (amount.denom in denomAmountMap) {
denomAmountMap[amount.denom] = denomAmountMap[amount.denom].plus(
baseAmount(amount.amount, this.defaultDecimals),
)
} else {
denomAmountMap[amount.denom] = baseAmount(amount.amount, this.defaultDecimals)
}
}
// Fill from
if (mapFrom.has(keySender.value)) {
const currentTo = mapFrom.get(keySender.value) as { amount: BaseAmount; asset: Asset | undefined }
currentTo.amount = currentTo?.amount.plus(totalNativeAmount)
mapFrom.set(keySender.value, currentTo)
} else {
const asset = this.assetFromDenom(this.baseDenom)
})
Object.entries(denomAmountMap).forEach(([denom, amount]) => {
// Fill to
const asset = this.assetFromDenom(denom)
if (asset) {
mapFrom.set(keySender.value, {
amount: totalNativeAmount,
asset,
})
const recipientAssetKey = `${keyRecipient.value}${assetToString(asset)}`
if (mapTo.has(recipientAssetKey)) {
const currentTo = mapTo.get(keyRecipient.value) as {
amount: BaseAmount
asset: Asset
address: Address
}
currentTo.amount = currentTo.amount.plus(amount)
mapTo.set(recipientAssetKey, currentTo)
} else {
mapTo.set(recipientAssetKey, {
amount,
asset,
address: keyRecipient.value,
})
}
// Fill from
const senderAssetKey = `${keySender.value}${assetToString(asset)}`
if (mapFrom.has(senderAssetKey)) {
const currentTo = mapFrom.get(senderAssetKey) as {
amount: BaseAmount
asset: Asset
address: Address
}
currentTo.amount = currentTo.amount.plus(amount)
mapFrom.set(senderAssetKey, currentTo)
} else {
mapFrom.set(senderAssetKey, {
amount,
asset,
address: keySender.value,
})
}
}
}
})
} catch (e) {
console.error('Error:', e)
}
}
})

const txTo: TxTo[] = []
for (const [key, value] of mapTo.entries()) {
for (const value of mapTo.values()) {
const txToObj: TxTo = {
to: key,
to: value.address,
amount: value.amount,
asset: value.asset,
}
txTo.push(txToObj)
}

const txFrom: TxFrom[] = []
for (const [key, value] of mapFrom.entries()) {
for (const value of mapFrom.values()) {
const txFromObj: TxFrom = {
from: key,
from: value.address,
amount: value.amount,
asset: value.asset,
}
Expand Down
7 changes: 7 additions & 0 deletions packages/xchain-kujira/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.1.10

### Patch Changes

- Updated dependencies [ae35c36]
- @xchainjs/xchain-cosmos-sdk@0.2.2

## 0.1.9

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-kujira/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-kujira",
"version": "0.1.9",
"version": "0.1.10",
"description": "Custom Kujira client",
"keywords": [
"XChain",
Expand Down Expand Up @@ -34,7 +34,7 @@
"dependencies": {
"@cosmjs/encoding": "0.31.1",
"@cosmjs/stargate": "0.31.1",
"@xchainjs/xchain-cosmos-sdk": "0.2.1"
"@xchainjs/xchain-cosmos-sdk": "0.2.2"
},
"devDependencies": {
"@cosmjs/amino": "0.31.1",
Expand Down
9 changes: 9 additions & 0 deletions packages/xchain-mayachain-amm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 1.0.8

### Patch Changes

- Updated dependencies [e1ec010]
- @xchainjs/xchain-thorchain@1.0.2
- @xchainjs/xchain-kujira@0.1.10
- @xchainjs/xchain-wallet@0.1.4

## 1.0.7

### Patch Changes
Expand Down
8 changes: 4 additions & 4 deletions packages/xchain-mayachain-amm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-mayachain-amm",
"version": "1.0.7",
"version": "1.0.8",
"description": "module that exposes estimating & swapping cryptocurrency assets on mayachain",
"keywords": [
"MAYAChain",
Expand Down Expand Up @@ -37,11 +37,11 @@
},
"dependencies": {
"@xchainjs/xchain-ethereum": "0.31.5",
"@xchainjs/xchain-kujira": "0.1.9",
"@xchainjs/xchain-kujira": "0.1.10",
"@xchainjs/xchain-mayachain-query": "0.1.7",
"@xchainjs/xchain-mayachain": "0.2.16",
"@xchainjs/xchain-thorchain": "1.0.1",
"@xchainjs/xchain-wallet": "0.1.3"
"@xchainjs/xchain-thorchain": "1.0.2",
"@xchainjs/xchain-wallet": "0.1.4"
},
"devDependencies": {
"@cosmos-client/core": "0.46.1",
Expand Down
7 changes: 7 additions & 0 deletions packages/xchain-thorchain-amm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.8.21

### Patch Changes

- Updated dependencies [e1ec010]
- @xchainjs/xchain-thorchain@1.0.2

## 0.8.20

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-thorchain-amm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-thorchain-amm",
"version": "0.8.20",
"version": "0.8.21",
"description": "module that exposes estimating & swappping cryptocurrency assets on thorchain",
"keywords": [
"THORChain",
Expand Down Expand Up @@ -40,7 +40,7 @@
"@xchainjs/xchain-avax": "0.4.5",
"@xchainjs/xchain-bsc": "0.4.6",
"@xchainjs/xchain-ethereum": "0.31.5",
"@xchainjs/xchain-thorchain": "1.0.1",
"@xchainjs/xchain-thorchain": "1.0.2",
"@xchainjs/xchain-mayachain": "0.2.16"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions packages/xchain-thorchain/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 1.0.2

### Patch Changes

- e1ec010: getDenom, getChainId and getPrefix util functions
- Updated dependencies [ae35c36]
- @xchainjs/xchain-cosmos-sdk@0.2.2

## 1.0.1

### Patch Changes
Expand Down
5 changes: 3 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": "1.0.1",
"version": "1.0.2",
"description": "Custom Thorchain client and utilities used by XChainJS clients",
"keywords": [
"THORChain",
Expand Down Expand Up @@ -37,8 +37,9 @@
"@cosmjs/encoding": "0.31.1",
"@cosmjs/crypto": "0.31.1",
"@xchainjs/xchain-client": "0.16.1",
"@xchainjs/xchain-cosmos-sdk": "0.2.1",
"@xchainjs/xchain-cosmos-sdk": "0.2.2",
"@xchainjs/xchain-util": "0.13.2",
"axios": "1.3.6",
"bignumber.js": "9.0.0",
"protobufjs": "6.11.4"
},
Expand Down
17 changes: 4 additions & 13 deletions packages/xchain-thorchain/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
bech32ToBase64,
makeClientPath,
} from '@xchainjs/xchain-cosmos-sdk'
import { Address, Asset, assetFromString, assetToString, eqAsset, isSynthAsset } from '@xchainjs/xchain-util'
import { Address, Asset, assetFromString, eqAsset } from '@xchainjs/xchain-util'
import { BigNumber } from 'bignumber.js'
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx'

Expand All @@ -32,7 +32,7 @@ import {
defaultClientConfig,
} from './const'
import { DepositParam, DepositTx, TxOfflineParams } from './types'
import { getDefaultExplorers, getExplorerAddressUrl, getExplorerTxUrl, isAssetRuneNative as isAssetRune } from './utils'
import { getDefaultExplorers, getDenom, getExplorerAddressUrl, getExplorerTxUrl, getPrefix } from './utils'

/**
* Interface for custom Thorchain client
Expand Down Expand Up @@ -70,14 +70,7 @@ export class Client extends CosmosSDKClient implements ThorchainClient {
* @returns the address prefix
*/
protected getPrefix(network: Network): string {
switch (network) {
case Network.Mainnet:
return 'thor'
case Network.Stagenet:
return 'sthor'
case Network.Testnet:
return 'tthor'
}
return getPrefix(network)
}

/**
Expand Down Expand Up @@ -150,9 +143,7 @@ export class Client extends CosmosSDKClient implements ThorchainClient {
* @returns {string} The denomination of the given asset.
*/
public getDenom(asset: Asset): string | null {
if (isAssetRune(asset)) return RUNE_DENOM
if (isSynthAsset(asset)) return assetToString(asset).toLowerCase()
return asset.symbol.toLowerCase()
return getDenom(asset)
}

/**
Expand Down
Loading

0 comments on commit 9f405b3

Please sign in to comment.