Skip to content

Commit

Permalink
1030 thorchain amm refactor (#1052)
Browse files Browse the repository at this point in the history
* getInboundDetails and getChainInboundDetails

* Wallet from xchain-wallet package

* ThorchainAction

* makeNonProtocolAction with feeRates

* xchain-wallet dependency updated

* Bug fixes

* e2e Tests

* yarn.lock

* Changeset version files

* Typo

* 1030 thorchain amm refactor examples (#1053)

* Swap examples

* Liquidity examples
  • Loading branch information
0xp3gasus authored Feb 27, 2024
1 parent 0e42643 commit 5efae97
Show file tree
Hide file tree
Showing 41 changed files with 2,539 additions and 2,286 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-snails-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-wallet': patch
---

Bug fixes
5 changes: 5 additions & 0 deletions .changeset/few-ways-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-amm': patch
---

ThorchainAMM `estimateSwap` method `walletIndex` parameter removed
5 changes: 5 additions & 0 deletions .changeset/gorgeous-tools-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-query': patch
---

ThorchainQuery new method `getChainInboundDetails` for returning the inbound address details of a chain
5 changes: 5 additions & 0 deletions .changeset/lemon-chairs-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-amm': minor
---

ThorchainAMM new method `validateSwap` for validating swap params
5 changes: 5 additions & 0 deletions .changeset/modern-donkeys-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-amm': patch
---

ThorchainAMM `estimateSwap` method `interfaceID` parameter removed
5 changes: 5 additions & 0 deletions .changeset/popular-maps-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-amm': patch
---

ThorchainAMM `estimateSwap` method new `fromAddress` parameter support
5 changes: 5 additions & 0 deletions .changeset/pretty-beans-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-amm': major
---

Wallet class removed
5 changes: 5 additions & 0 deletions .changeset/sharp-days-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-query': patch
---

ThorchainQuery new method `getInboundDetails` for returning the inbound addresses details
5 changes: 5 additions & 0 deletions .changeset/six-files-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-amm': minor
---

ThorchainQuery new method `isRouterApprovedToSpend` for validating thorchain router are allowed to spend ERC20 assets
5 changes: 5 additions & 0 deletions .changeset/smooth-eyes-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-amm': major
---

ThorchainAMM methods `wallet` parameter removed
5 changes: 5 additions & 0 deletions .changeset/twenty-teachers-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain-amm': major
---

ThorchainAMM constructor uses the wallet class from the `@xchainjs/xchain-wallet` package
47 changes: 0 additions & 47 deletions examples/do-swap/doSend.ts

This file was deleted.

52 changes: 34 additions & 18 deletions examples/do-swap/doStreamingSwap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import cosmosclient from '@cosmos-client/core'
import { Client as AvaxClient, defaultAvaxParams } from '@xchainjs/xchain-avax'
import { Client as BnbClient } from '@xchainjs/xchain-binance'
import { Client as BtcClient, defaultBTCParams as defaultBtcParams } from '@xchainjs/xchain-bitcoin'
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 { Midgard, MidgardCache, MidgardQuery } from '@xchainjs/xchain-midgard-query'
import { THORChain } from '@xchainjs/xchain-thorchain'
import { AmmEstimateSwapParams, ThorchainAMM, Wallet } from '@xchainjs/xchain-thorchain-amm'
import { ThorchainCache, ThorchainQuery, Thornode, TxDetails } from '@xchainjs/xchain-thorchain-query'
import { Client as GaiaClient } from '@xchainjs/xchain-cosmos'
import { Client as DogeClient, defaultDogeParams } from '@xchainjs/xchain-doge'
import { Client as EthClient, defaultEthParams } from '@xchainjs/xchain-ethereum'
import { Client as LtcClient, defaultLtcParams } from '@xchainjs/xchain-litecoin'
import { Client as ThorClient, THORChain, defaultClientConfig as defaultThorParams } from '@xchainjs/xchain-thorchain'
import { ThorchainAMM } from '@xchainjs/xchain-thorchain-amm'
import { QuoteSwapParams, ThorchainQuery, TxDetails } from '@xchainjs/xchain-thorchain-query'
import {
CryptoAmount,
assetAmount,
Expand All @@ -13,6 +21,7 @@ import {
delay,
register9Rheader,
} from '@xchainjs/xchain-util'
import { Wallet } from '@xchainjs/xchain-wallet'
import axios from 'axios'

import { checkTx } from '../check-tx/check-tx'
Expand Down Expand Up @@ -62,7 +71,7 @@ const delayedLog = async (message: string, delayMs: number) => {
console.log(`${message} (Done!)`)
}

const doStreamingSwap = async (tcAmm: ThorchainAMM, wallet: Wallet, network: Network) => {
const doStreamingSwap = async (tcAmm: ThorchainAMM, wallet: Wallet) => {
try {
const amount = process.argv[4]
const decimals = Number(process.argv[5])
Expand All @@ -72,18 +81,16 @@ const doStreamingSwap = async (tcAmm: ThorchainAMM, wallet: Wallet, network: Net
const streamingQuantity = Number(process.argv[9])

const toChain = toAsset.synth ? THORChain : toAsset.chain
const destinationAddress = wallet.clients[toChain].getAddress()

const swapParams: AmmEstimateSwapParams = {
const swapParams: QuoteSwapParams = {
fromAddress: await wallet.getAddress(fromAsset.chain),
fromAsset,
amount: new CryptoAmount(assetToBase(assetAmount(amount, decimals)), fromAsset),
destinationAsset: toAsset,
destinationAddress,
destinationAddress: await wallet.getAddress(toChain),
streamingInterval,
streamingQuantity,
// toleranceBps: 0, // WARNING: No compatible with Streaming Swap
wallet,
walletIndex: 0,
}
const affiliateAddress = process.argv[10]
if (affiliateAddress) {
Expand All @@ -94,7 +101,7 @@ const doStreamingSwap = async (tcAmm: ThorchainAMM, wallet: Wallet, network: Net
const outPutCanSwap = await tcAmm.estimateSwap(swapParams)
printTx(outPutCanSwap, swapParams.amount)
if (outPutCanSwap.txEstimate.canSwap) {
const output = await tcAmm.doSwap(wallet, swapParams)
const output = await tcAmm.doSwap(swapParams)
console.log(
`Tx hash: ${output.hash},\n Tx url: ${output.url}\n WaitTime: ${outPutCanSwap.txEstimate.outboundDelaySeconds}`,
)
Expand All @@ -105,7 +112,7 @@ const doStreamingSwap = async (tcAmm: ThorchainAMM, wallet: Wallet, network: Net
? 20000
: outPutCanSwap.txEstimate.outboundDelaySeconds * 1000,
)
await checkTx(network, output.hash)
await checkTx(wallet.getNetwork(), output.hash)
}
} catch (error) {
console.error(error)
Expand All @@ -115,12 +122,21 @@ const doStreamingSwap = async (tcAmm: ThorchainAMM, wallet: Wallet, network: Net
const main = async () => {
const seed = process.argv[2]
const network = process.argv[3] as Network
const midgardCache = new MidgardCache(new Midgard(network))
const thorchainCache = new ThorchainCache(new Thornode(network), new MidgardQuery(midgardCache))
const thorchainQuery = new ThorchainQuery(thorchainCache)
const thorchainAmm = new ThorchainAMM(thorchainQuery)
const wallet = new Wallet(seed, thorchainQuery)
await doStreamingSwap(thorchainAmm, wallet, network)
const wallet = new Wallet({
BTC: new BtcClient({ ...defaultBtcParams, phrase: seed, network }),
BCH: new BchClient({ ...defaultBchParams, phrase: seed, network }),
LTC: new LtcClient({ ...defaultLtcParams, phrase: seed, network }),
DOGE: new DogeClient({ ...defaultDogeParams, phrase: seed, network }),
ETH: new EthClient({ ...defaultEthParams, phrase: seed, network }),
AVAX: new AvaxClient({ ...defaultAvaxParams, phrase: seed, network }),
BSC: new BscClient({ ...defaultBscParams, phrase: seed, network }),
GAIA: new GaiaClient({ phrase: seed, network }),
BNB: new BnbClient({ phrase: seed, network }),
THOR: new ThorClient({ ...defaultThorParams, phrase: seed, network }),
})

const thorchainAmm = new ThorchainAMM(new ThorchainQuery(), wallet)
await doStreamingSwap(thorchainAmm, wallet)
}

main()
Expand Down
51 changes: 33 additions & 18 deletions examples/do-swap/doSwap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import cosmosclient from '@cosmos-client/core'
import { Client as AvaxClient, defaultAvaxParams } from '@xchainjs/xchain-avax'
import { Client as BnbClient } from '@xchainjs/xchain-binance'
import { Client as BtcClient, defaultBTCParams as defaultBtcParams } from '@xchainjs/xchain-bitcoin'
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 { Midgard, MidgardCache, MidgardQuery } from '@xchainjs/xchain-midgard-query'
import { THORChain } from '@xchainjs/xchain-thorchain'
import { AmmEstimateSwapParams, ThorchainAMM, Wallet } from '@xchainjs/xchain-thorchain-amm'
import { ThorchainCache, ThorchainQuery, Thornode, TxDetails } from '@xchainjs/xchain-thorchain-query'
import { Client as GaiaClient } from '@xchainjs/xchain-cosmos'
import { Client as DogeClient, defaultDogeParams } from '@xchainjs/xchain-doge'
import { Client as EthClient, defaultEthParams } from '@xchainjs/xchain-ethereum'
import { Client as LtcClient, defaultLtcParams } from '@xchainjs/xchain-litecoin'
import { Client as ThorClient, THORChain, defaultClientConfig as defaultThorParams } from '@xchainjs/xchain-thorchain'
import { ThorchainAMM } from '@xchainjs/xchain-thorchain-amm'
import { QuoteSwapParams, ThorchainQuery, TxDetails } from '@xchainjs/xchain-thorchain-query'
import {
CryptoAmount,
assetAmount,
Expand All @@ -13,6 +21,7 @@ import {
delay,
register9Rheader,
} from '@xchainjs/xchain-util'
import { Wallet } from '@xchainjs/xchain-wallet'
import axios from 'axios'

import { checkTx } from '../check-tx/check-tx'
Expand Down Expand Up @@ -65,25 +74,23 @@ const delayedLog = async (message: string, delayMs: number) => {
/**
* From asset to asset with no Affiliate address on testnet
*/
const doSingleSwap = async (tcAmm: ThorchainAMM, wallet: Wallet, network: Network) => {
const doSingleSwap = async (tcAmm: ThorchainAMM, wallet: Wallet) => {
try {
const amount = process.argv[4]
const decimals = Number(process.argv[5])
const fromAsset = assetFromString(`${process.argv[6]}`)
const toAsset = assetFromString(`${process.argv[7]}`)

const toChain = toAsset.synth ? THORChain : toAsset.chain
const destinationAddress = wallet.clients[toChain].getAddress()

const swapParams: AmmEstimateSwapParams = {
const swapParams: QuoteSwapParams = {
fromAsset,
amount: new CryptoAmount(assetToBase(assetAmount(amount, decimals)), fromAsset),
destinationAsset: toAsset,
destinationAddress,
destinationAddress: await wallet.getAddress(toChain),
toleranceBps: 1000, //optional
wallet,
walletIndex: 0,
}

const affiliateAddress = process.argv[8]
if (affiliateAddress) {
const affiliateFeeBasisPoints = Number(process.argv[9])
Expand All @@ -93,7 +100,7 @@ const doSingleSwap = async (tcAmm: ThorchainAMM, wallet: Wallet, network: Networ
const outPutCanSwap = await tcAmm.estimateSwap(swapParams)
printTx(outPutCanSwap, swapParams.amount)
if (outPutCanSwap.txEstimate.canSwap) {
const output = await tcAmm.doSwap(wallet, swapParams)
const output = await tcAmm.doSwap(swapParams)
console.log(
`Tx hash: ${output.hash},\n Tx url: ${output.url}\n WaitTime: ${outPutCanSwap.txEstimate.outboundDelaySeconds}`,
)
Expand All @@ -104,7 +111,7 @@ const doSingleSwap = async (tcAmm: ThorchainAMM, wallet: Wallet, network: Networ
? 12000
: outPutCanSwap.txEstimate.outboundDelaySeconds * 1000,
)
await checkTx(network, output.hash)
await checkTx(wallet.getNetwork(), output.hash)
}
} catch (error) {
console.error(error)
Expand All @@ -114,12 +121,20 @@ const doSingleSwap = async (tcAmm: ThorchainAMM, wallet: Wallet, network: Networ
const main = async () => {
const seed = process.argv[2]
const network = process.argv[3] as Network
const midgardCache = new MidgardCache(new Midgard(network))
const thorchainCache = new ThorchainCache(new Thornode(network), new MidgardQuery(midgardCache))
const thorchainQuery = new ThorchainQuery(thorchainCache)
const thorchainAmm = new ThorchainAMM(thorchainQuery)
const wallet = new Wallet(seed, thorchainQuery)
await doSingleSwap(thorchainAmm, wallet, network)
const wallet = new Wallet({
BTC: new BtcClient({ ...defaultBtcParams, phrase: seed, network }),
BCH: new BchClient({ ...defaultBchParams, phrase: seed, network }),
LTC: new LtcClient({ ...defaultLtcParams, phrase: seed, network }),
DOGE: new DogeClient({ ...defaultDogeParams, phrase: seed, network }),
ETH: new EthClient({ ...defaultEthParams, phrase: seed, network }),
AVAX: new AvaxClient({ ...defaultAvaxParams, phrase: seed, network }),
BSC: new BscClient({ ...defaultBscParams, phrase: seed, network }),
GAIA: new GaiaClient({ phrase: seed, network }),
BNB: new BnbClient({ phrase: seed, network }),
THOR: new ThorClient({ ...defaultThorParams, phrase: seed, network }),
})
const thorchainAmm = new ThorchainAMM(new ThorchainQuery(), wallet)
await doSingleSwap(thorchainAmm, wallet)
}

main()
Expand Down
54 changes: 0 additions & 54 deletions examples/do-swap/example.ts

This file was deleted.

Loading

0 comments on commit 5efae97

Please sign in to comment.