Skip to content

Chore/update xchain util #1328

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

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
CachedValue,
Chain,
CryptoAmount,
SecuredAsset,
SynthAsset,
TokenAsset,
TradeAsset,
baseAmount,
isSecuredAsset,
isSynthAsset,
isTradeAsset,
} from '@xchainjs/xchain-util'
Expand Down Expand Up @@ -204,13 +206,16 @@ export class ChainflipProtocol implements IProtocol {
* @throws {Error} - If asset is not supported in Chainflip
* @returns the asset data
*/
private async getAssetData(asset: Asset | TokenAsset | SynthAsset | TradeAsset): Promise<AssetData> {
private async getAssetData(asset: Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset): Promise<AssetData> {
if (isSynthAsset(asset)) {
throw Error('Synth asset not supported in Chainflip protocol')
}
if (isTradeAsset(asset)) {
throw Error('Trade asset not supported in Chainflip protocol')
}
if (isSecuredAsset(asset)) {
throw Error('Secured asset not supported in Chainflip protocol')
}
const chainAssets = await this.assetsData.getValue()
const assetData = chainAssets.find((chainAsset) => {
return chainAsset.asset === xAssetToCAsset(asset) && asset.chain === cChainToXChain(chainAsset.chain)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { AssetRuneNative, THORChain } from '@xchainjs/xchain-thorchain'
import { ApproveParams, IsApprovedParams, ThorchainAMM } from '@xchainjs/xchain-thorchain-amm'
import { ThorchainQuery } from '@xchainjs/xchain-thorchain-query'
import { AnyAsset, Chain, assetToString, eqAsset, isSynthAsset, isTradeAsset } from '@xchainjs/xchain-util'
import {
AnyAsset,
Chain,
assetToString,
eqAsset,
isSecuredAsset,
isSynthAsset,
isTradeAsset,
} from '@xchainjs/xchain-util'
import { Wallet } from '@xchainjs/xchain-wallet'

import {
Expand Down Expand Up @@ -57,7 +65,8 @@ export class ThorchainProtocol implements IProtocol {
* @returns {boolean} True if the asset is supported, otherwise false
*/
public async isAssetSupported(asset: AnyAsset): Promise<boolean> {
if (eqAsset(asset, AssetRuneNative) || isTradeAsset(asset) || isSynthAsset(asset)) return true
if (eqAsset(asset, AssetRuneNative) || isTradeAsset(asset) || isSynthAsset(asset) || isSecuredAsset(asset))
return true
const pools = await this.thorchainQuery.thorchainCache.getPools()
return (
Object.values(pools).findIndex((pool) => pool.isAvailable() && assetToString(asset) === pool.assetString) !== -1
Expand Down
17 changes: 9 additions & 8 deletions packages/xchain-aggregator/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Asset,
Chain,
CryptoAmount,
SecuredAsset,
SynthAsset,
TokenAsset,
TradeAsset,
Expand All @@ -23,9 +24,9 @@ type TxSubmitted = {
* Fees
*/
type Fees = {
asset: Asset | TokenAsset | SynthAsset | TradeAsset // The asset for which fees are calculated
affiliateFee: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The affiliate fee amount
outboundFee: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The outbound fee amount
asset: Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset // The asset for which fees are calculated
affiliateFee: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset> // The affiliate fee amount
outboundFee: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset> // The outbound fee amount
}

/**
Expand Down Expand Up @@ -81,8 +82,8 @@ type QuoteSwap = {
protocol: Protocol
toAddress: Address // The destination address for the swap
memo: string // The memo associated with the swap
expectedAmount: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The expected amount to be received after the swap
dustThreshold: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The dust threshold for the swap
expectedAmount: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset> // The expected amount to be received after the swap
dustThreshold: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset> // The dust threshold for the swap
// TODO: Update type to return an array of the fees
fees: Fees // The fees associated with the swap
totalSwapSeconds: number // The total time for the swap operation
Expand All @@ -96,9 +97,9 @@ type QuoteSwap = {
* Represents parameters for quoting a swap operation.
*/
type QuoteSwapParams = {
fromAsset: Asset | TokenAsset | SynthAsset | TradeAsset // The asset to swap from
destinationAsset: Asset | TokenAsset | SynthAsset | TradeAsset // The asset to swap to
amount: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The amount to swap
fromAsset: Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset // The asset to swap from
destinationAsset: Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset // The asset to swap to
amount: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset> // The amount to swap
fromAddress?: string // The source address for the swap
destinationAddress?: string // The destination address for the swap
height?: number // The block height for the swap
Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-cosmos-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
TxTo as BaseTxTo,
TxsPage as BaseTxsPage,
} from '@xchainjs/xchain-client'
import { Asset, SynthAsset, TokenAsset, TradeAsset } from '@xchainjs/xchain-util'
import { Asset, SecuredAsset, SynthAsset, TokenAsset, TradeAsset } from '@xchainjs/xchain-util'
/**
* Compatible tokens with Cosmos providers
*/
export type CompatibleAsset = Asset | TokenAsset | SynthAsset | TradeAsset
export type CompatibleAsset = Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset

/**
* Type definition for Cosmos balance.
Expand Down
4 changes: 3 additions & 1 deletion packages/xchain-midgard-query/src/midgard-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
assetFromString,
assetToString,
baseAmount,
isSecuredAsset,
isSynthAsset,
isTradeAsset,
} from '@xchainjs/xchain-util'
Expand Down Expand Up @@ -107,7 +108,8 @@ export class MidgardQuery {
* @returns {number} - Number of decimals from Midgard. Reference: https://gitlab.com/thorchain/midgard#refresh-native-decimals
*/
public async getDecimalForAsset(asset: CompatibleAsset): Promise<number> {
if (isAssetRuneNative(asset) || isSynthAsset(asset) || isTradeAsset(asset)) return DEFAULT_THORCHAIN_DECIMALS
if (isAssetRuneNative(asset) || isSynthAsset(asset) || isTradeAsset(asset) || isSecuredAsset(asset))
return DEFAULT_THORCHAIN_DECIMALS

const pool = await this.getPool(assetToString(asset))
return Number(pool.nativeDecimal)
Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-midgard-query/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GetActions200Response } from '@xchainjs/xchain-midgard'
import { Address, Asset, CryptoAmount, SynthAsset, TokenAsset, TradeAsset } from '@xchainjs/xchain-util'
import { Address, Asset, CryptoAmount, SecuredAsset, SynthAsset, TokenAsset, TradeAsset } from '@xchainjs/xchain-util'

export type CompatibleAsset = Asset | TokenAsset | SynthAsset | TradeAsset
export type CompatibleAsset = Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset

/**
* Search parameters to search for positions within the THORChain SAVER investment product trunks.
Expand Down
24 changes: 15 additions & 9 deletions packages/xchain-thorchain-query/src/thorchain-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {
CachedValue,
Chain,
CryptoAmount,
SecuredAsset,
SynthAsset,
TokenAsset,
TradeAsset,
assetToString,
baseAmount,
eqAsset,
isSecuredAsset,
isSynthAsset,
isTradeAsset,
} from '@xchainjs/xchain-util'
Expand Down Expand Up @@ -76,26 +78,30 @@ export class ThorchainCache {
* @returns Promise<BigNumber> - The exchange rate.
*/
async getExchangeRate(
from: Asset | TokenAsset | SynthAsset | TradeAsset,
to: Asset | TokenAsset | SynthAsset | TradeAsset,
from: Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset,
to: Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset,
): Promise<BigNumber> {
let exchangeRate: BigNumber
if (eqAsset(from, to)) {
exchangeRate = SAME_ASSET_EXCHANGE_RATE
} else if (isAssetRuneNative(from)) {
// Runes per Asset
const poolToAsset = isSynthAsset(to) || isTradeAsset(to) ? await this.getAnalogAsset(to) : to
const poolToAsset =
isSynthAsset(to) || isTradeAsset(to) || isSecuredAsset(to) ? await this.getAnalogAsset(to) : to
const lpTo = await this.getPoolForAsset(poolToAsset)
exchangeRate = lpTo.assetToRuneRatio
} else if (isAssetRuneNative(to)) {
// Asset per rune
const poolFromAsset = isSynthAsset(from) || isTradeAsset(from) ? await this.getAnalogAsset(from) : from
const poolFromAsset =
isSynthAsset(from) || isTradeAsset(from) || isSecuredAsset(from) ? await this.getAnalogAsset(from) : from
const lpFrom = await this.getPoolForAsset(poolFromAsset)
exchangeRate = lpFrom.runeToAssetRatio
} else {
// AssetA per AssetB
const poolFromAsset = isSynthAsset(from) || isTradeAsset(from) ? await this.getAnalogAsset(from) : from
const poolToAsset = isSynthAsset(to) || isTradeAsset(to) ? await this.getAnalogAsset(to) : to
const poolFromAsset =
isSynthAsset(from) || isTradeAsset(from) || isSecuredAsset(from) ? await this.getAnalogAsset(from) : from
const poolToAsset =
isSynthAsset(to) || isTradeAsset(to) || isSecuredAsset(to) ? await this.getAnalogAsset(to) : to
const lpFrom = await this.getPoolForAsset(poolFromAsset)
const lpTo = await this.getPoolForAsset(poolToAsset)
// from/R * R/to = from/to
Expand Down Expand Up @@ -227,8 +233,8 @@ export class ThorchainCache {
* @param outAsset - The asset you want to convert to.
* @returns Promise<CryptoAmount> - The converted amount.
*/
async convert<T extends Asset | TokenAsset | SynthAsset | TradeAsset>(
input: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset>,
async convert<T extends Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset>(
input: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset>,
outAsset: T,
): Promise<CryptoAmount<T>> {
const exchangeRate = await this.getExchangeRate(input.asset, outAsset)
Expand Down Expand Up @@ -281,7 +287,7 @@ export class ThorchainCache {
}
}

private async getAnalogAsset(asset: SynthAsset | TradeAsset): Promise<Asset | TokenAsset> {
private async getAnalogAsset(asset: SynthAsset | TradeAsset | SecuredAsset): Promise<Asset | TokenAsset> {
const pools = await this.getPools()
const analogAssetPool = Object.values(pools).find((pool) => {
return (
Expand Down
5 changes: 3 additions & 2 deletions packages/xchain-thorchain-query/src/thorchain-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Chain,
CryptoAmount,
SYNTH_ASSET_DELIMITER,
SecuredAsset,
SynthAsset,
TOKEN_ASSET_DELIMITER,
TRADE_ASSET_DELIMITER,
Expand Down Expand Up @@ -337,8 +338,8 @@ export class ThorchainQuery {
* @param ouAsset - the Asset you want to convert to
* @returns CryptoAmount of input
*/
async convert<T extends Asset | TokenAsset | SynthAsset | TradeAsset>(
input: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset>,
async convert<T extends Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset>(
input: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset>,
outAsset: T,
): Promise<CryptoAmount<T>> {
// Convert the input amount to the specified asset
Expand Down
25 changes: 13 additions & 12 deletions packages/xchain-thorchain-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@ import {
BaseAmount,
Chain,
CryptoAmount,
SecuredAsset,
SynthAsset,
TokenAsset,
TradeAsset,
TradeCryptoAmount,
} from '@xchainjs/xchain-util'
import { BigNumber } from 'bignumber.js'

export type CompatibleAsset = Asset | TokenAsset | SynthAsset | TradeAsset
export type CompatibleAsset = Asset | TokenAsset | SynthAsset | TradeAsset | SecuredAsset

/**
* Represents the total fees associated with a swap.
*/
export type TotalFees = {
asset: Asset | TokenAsset | SynthAsset | TradeAsset // The asset for which fees are calculated
affiliateFee: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The affiliate fee
outboundFee: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The outbound fee
asset: CompatibleAsset // The asset for which fees are calculated
affiliateFee: CryptoAmount<CompatibleAsset> // The affiliate fee
outboundFee: CryptoAmount<CompatibleAsset> // The outbound fee
}
/**
* Represents an estimate for a swap transaction.
*/
export type SwapEstimate = {
netOutput: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The net output amount after fees
netOutput: CryptoAmount<CompatibleAsset> // The net output amount after fees
totalFees: TotalFees // The total fees associated with the swap
netOutputStreaming: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The net output amount for streaming
netOutputStreaming: CryptoAmount<CompatibleAsset> // The net output amount for streaming
maxStreamingQuantity: number // The maximum streaming quantity
inboundConfirmationSeconds?: number // The inbound confirmation time in seconds
outboundDelaySeconds: number // The outbound delay time in seconds
Expand All @@ -50,9 +51,9 @@ export type SwapEstimate = {
*/
export type QuoteSwapParams = {
fromAddress?: Address // The address to swap from
fromAsset: Asset | TokenAsset | SynthAsset | TradeAsset // The asset to swap from
destinationAsset: Asset | TokenAsset | SynthAsset | TradeAsset // The asset to swap to
amount: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The amount to swap
fromAsset: CompatibleAsset // The asset to swap from
destinationAsset: CompatibleAsset // The asset to swap to
amount: CryptoAmount<CompatibleAsset> // The amount to swap
destinationAddress?: string // The destination address (optional)
streamingInterval?: number // The streaming interval (optional)
streamingQuantity?: number // The streaming quantity (optional)
Expand All @@ -67,8 +68,8 @@ export type QuoteSwapParams = {
* Represents the output of a swap transaction.
*/
export type SwapOutput = {
output: CryptoAmount<Asset | TokenAsset | SynthAsset> // The output amount
swapFee: CryptoAmount<Asset | TokenAsset | SynthAsset> // The swap fee
output: CryptoAmount<CompatibleAsset> // The output amount
swapFee: CryptoAmount<CompatibleAsset> // The swap fee
slip: BigNumber // The slip
}
/**
Expand Down Expand Up @@ -145,7 +146,7 @@ export type ConstructMemo = {
*/
export type TxDetails = {
memo: string // The memo for the transaction
dustThreshold: CryptoAmount<Asset | TokenAsset | SynthAsset | TradeAsset> // The dust threshold for the transaction
dustThreshold: CryptoAmount<CompatibleAsset> // The dust threshold for the transaction
toAddress: Address // The recipient address for the transaction
expiry: Date // The expiry date for the transaction
txEstimate: SwapEstimate // The swap estimate for the transaction
Expand Down
2 changes: 2 additions & 0 deletions packages/xchain-thorchain/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,14 @@ export const parseAssetToTHORNodeAsset = (
ticker: string
synth: boolean
trade: boolean
secured: boolean
} => {
return {
chain: asset.chain,
symbol: asset.symbol,
ticker: asset.ticker,
synth: asset.type === AssetType.SYNTH,
trade: asset.type === AssetType.TRADE,
secured: asset.type === AssetType.SECURED,
}
}
9 changes: 9 additions & 0 deletions packages/xchain-util/__tests__/asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ describe('asset', () => {
expect(result).toEqual({ chain: 'ETH', symbol: 'ETH', ticker: 'ETH', type: AssetType.TRADE })
})

it('trade BTC-BTC', () => {
const result = assetFromString('BTC-BTC')
expect(result).toEqual({ chain: 'BTC', symbol: 'BTC', ticker: 'BTC', type: AssetType.SECURED })
})
it('trade ETH-ETH', () => {
const result = assetFromString('ETH-ETH')
expect(result).toEqual({ chain: 'ETH', symbol: 'ETH', ticker: 'ETH', type: AssetType.SECURED })
})

it('KUJI.USK', () => {
const result = assetFromString('KUJI.USK')
expect(result).toEqual({ chain: 'KUJI', symbol: 'USK', ticker: 'USK', type: AssetType.TOKEN })
Expand Down
Loading
Loading