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

Synth and ERC20 bug fix #1057

Merged
merged 3 commits into from
Mar 4, 2024
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
6 changes: 6 additions & 0 deletions .changeset/large-onions-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@xchainjs/xchain-mayachain-amm': patch
'@xchainjs/xchain-thorchain-amm': patch
---

Bug fix on swap validation with synth and erc-20 assets
11 changes: 8 additions & 3 deletions packages/xchain-mayachain-amm/src/mayachain-amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Client as KujiraClient, defaultKujiParams } from '@xchainjs/xchain-kuji
import { Client as MayaClient, MAYAChain } from '@xchainjs/xchain-mayachain'
import { MayachainQuery, QuoteSwap, QuoteSwapParams } from '@xchainjs/xchain-mayachain-query'
import { Client as ThorClient } from '@xchainjs/xchain-thorchain'
import { Asset, CryptoAmount, baseAmount, getContractAddressFromAsset } from '@xchainjs/xchain-util'
import { Asset, CryptoAmount, baseAmount, eqAsset, getContractAddressFromAsset } from '@xchainjs/xchain-util'
import { Wallet } from '@xchainjs/xchain-wallet'
import { ethers } from 'ethers'

Expand Down Expand Up @@ -127,7 +127,10 @@ export class MayachainAMM {
const errors: string[] = []

// Validate destination address if provided
if (destinationAddress && !this.wallet.validateAddress(destinationAsset.chain, destinationAddress)) {
if (
destinationAddress &&
!this.wallet.validateAddress(destinationAsset.synth ? MAYAChain : destinationAsset.chain, destinationAddress)
) {
errors.push(`destinationAddress ${destinationAddress} is not a valid address`)
}
// Validate affiliate address if provided
Expand Down Expand Up @@ -315,7 +318,9 @@ export class MayachainAMM {
*/
private isERC20Asset(asset: Asset): boolean {
// Check if the asset's chain is an EVM chain and if the symbol matches AssetETH.symbol
return this.isEVMChain(asset.chain) ? [AssetETH.symbol].includes(asset.symbol) : false
return this.isEVMChain(asset.chain)
? [AssetETH].findIndex((nativeEVMAsset) => eqAsset(nativeEVMAsset, asset)) === -1
: false
}

/**
Expand Down
38 changes: 37 additions & 1 deletion packages/xchain-thorchain-amm/__e2e__/thorchain-swap-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { Client as BchClient, defaultBchParams } from '@xchainjs/xchain-bitcoincash'
import { Client as BscClient, defaultBscParams } from '@xchainjs/xchain-bsc'
import { Network } from '@xchainjs/xchain-client'
import { AssetATOM, Client as GaiaClient } from '@xchainjs/xchain-cosmos'
import { AssetATOM, Client as GaiaClient, GAIAChain } from '@xchainjs/xchain-cosmos'
import { Client as DogeClient, defaultDogeParams } from '@xchainjs/xchain-doge'
import { AssetETH, Client as EthClient, defaultEthParams } from '@xchainjs/xchain-ethereum'
import { Client as LtcClient, defaultLtcParams } from '@xchainjs/xchain-litecoin'
Expand Down Expand Up @@ -132,6 +132,42 @@ describe('ThorchainAmm e2e tests', () => {
console.log(errors)
})

it(`Should validate swap from ATOM to synth ATOM without errors`, async () => {
const errors = await thorchainAmm.validateSwap({
fromAsset: AssetATOM,
amount: new CryptoAmount(assetToBase(assetAmount('10')), AssetATOM),
destinationAddress: await wallet.getAddress(THORChain),
destinationAsset: assetFromStringEx('GAIA/ATOM'),
})

console.log(errors)
})

it(`Should validate swap from ATOM to synth ATOM with destination address error`, async () => {
const errors = await thorchainAmm.validateSwap({
fromAsset: AssetATOM,
amount: new CryptoAmount(assetToBase(assetAmount('10')), AssetATOM),
destinationAddress: await wallet.getAddress(GAIAChain),
destinationAsset: assetFromStringEx('GAIA/ATOM'),
})

console.log(errors)
})

it(`Should validate swap from AVAX.USDC to synth ATOM`, async () => {
const asset = assetFromStringEx('AVAX.USDC-0XB97EF9EF8734C71904D8002F8B6BC66DD9C48A6E')

const errors = await thorchainAmm.validateSwap({
fromAsset: asset,
fromAddress: await wallet.getAddress(asset.chain),
amount: new CryptoAmount(assetToBase(assetAmount('10')), asset),
destinationAddress: await wallet.getAddress(AssetATOM.chain),
destinationAsset: AssetATOM,
})

console.log(errors)
})

it(`Should estimate swap from BTC to ETH`, async () => {
const quoteSwap = await thorchainAmm.estimateSwap({
fromAsset: AssetBTC,
Expand Down
5 changes: 4 additions & 1 deletion packages/xchain-thorchain-amm/src/thorchain-amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ export class ThorchainAMM {
}: QuoteSwapParams): Promise<string[]> {
const errors: string[] = []

if (destinationAddress && !this.wallet.validateAddress(destinationAsset.chain, destinationAddress)) {
if (
destinationAddress &&
!this.wallet.validateAddress(destinationAsset.synth ? THORChain : destinationAsset.chain, destinationAddress)
) {
errors.push(`destinationAddress ${destinationAddress} is not a valid address`)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-thorchain-amm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AssetBNB } from '@xchainjs/xchain-binance'
import { AssetBSC } from '@xchainjs/xchain-bsc'
import { AssetATOM } from '@xchainjs/xchain-cosmos'
import { AssetETH } from '@xchainjs/xchain-ethereum'
import { Asset, Chain } from '@xchainjs/xchain-util'
import { Asset, Chain, eqAsset } from '@xchainjs/xchain-util'

/**
* Check if a chain is EVM and supported by the protocol
Expand All @@ -21,7 +21,7 @@ export const isProtocolEVMChain = (chain: Chain): boolean => {
*/
export const isProtocolERC20Asset = (asset: Asset): boolean => {
return isProtocolEVMChain(asset.chain)
? ![AssetETH.symbol, AssetAVAX.symbol, AssetBSC.symbol].includes(asset.symbol)
? [AssetETH, AssetAVAX, AssetBSC].findIndex((nativeEVMAsset) => eqAsset(nativeEVMAsset, asset)) === -1
: false
}

Expand Down
Loading