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

Add support for native token on liquidity v3 #331

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 20 additions & 13 deletions src/entities/trades/swapr-v3/SwaprV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class SwaprV3Trade extends TradeWithSwapTransaction {

static async getQuote(
{ amount, quoteCurrency, tradeType, maximumSlippage }: SwaprV3GetQuoteParams,
provider?: BaseProvider
provider?: BaseProvider,
): Promise<SwaprV3Trade | null> {
const chainId = tryGetChainId(amount, quoteCurrency)
invariant(chainId, 'SwaprV3Trade.getQuote: chainId is required')
Expand All @@ -87,7 +87,7 @@ export class SwaprV3Trade extends TradeWithSwapTransaction {
amount.currency.address,
amount.currency.decimals,
amount.currency.symbol,
amount.currency.name
amount.currency.name,
)

invariant(quoteCurrency.address, `SwaprV3Trade.getQuote: quoteCurrency.address is required`)
Expand All @@ -98,12 +98,12 @@ export class SwaprV3Trade extends TradeWithSwapTransaction {
quoteCurrency.address,
quoteCurrency.decimals,
quoteCurrency.symbol,
quoteCurrency.name
quoteCurrency.name,
)

invariant(
(await provider.getNetwork()).chainId == chainId,
`SwaprV3Trade.getQuote: currencies chainId does not match provider's chainId`
`SwaprV3Trade.getQuote: currencies chainId does not match provider's chainId`,
)

const routes = await getRoutes(tokenIn, tokenOut, chainId)
Expand All @@ -119,7 +119,7 @@ export class SwaprV3Trade extends TradeWithSwapTransaction {
tokenIn.address,
tokenOut.address,
parseUnits(amount.toSignificant(), amount.currency.decimals),
0
0,
)
.catch((error) => {
console.error(`Error sending quoteExactInputSingle transaction: ${error}`)
Expand All @@ -143,7 +143,7 @@ export class SwaprV3Trade extends TradeWithSwapTransaction {
tokenOut.address,
amount.currency.address,
parseUnits(amount.toSignificant(), amount.currency.decimals),
0
0,
)
.catch((error) => {
console.error(`Error sending quoteExactOutputSingle transaction: ${error}`)
Expand Down Expand Up @@ -194,21 +194,28 @@ export class SwaprV3Trade extends TradeWithSwapTransaction {
}

public async swapTransaction(options: TradeOptions): Promise<UnsignedTransaction> {
const to: string = validateAndParseAddress(options.recipient)
const amountIn: string = `0x${this.maximumAmountIn().raw.toString(16)}`
const amountOut: string = `0x${this.minimumAmountOut().raw.toString(16)}`
const isNativeIn = Currency.isNative(this.inputAmount.currency)
const isNativeOut = Currency.isNative(this.outputAmount.currency)
invariant(!(isNativeIn && isNativeOut), 'the router does not support both native in and out')
Diogomartf marked this conversation as resolved.
Show resolved Hide resolved

const recipient = validateAndParseAddress(options.recipient)
const amountIn = `0x${this.maximumAmountIn().raw.toString(16)}`
const amountOut = `0x${this.minimumAmountOut().raw.toString(16)}`

const isTradeExactInput = this.tradeType === TradeType.EXACT_INPUT
const routerContract = getRouterContract()

const baseParams = {
tokenIn: this.inputAmount.currency.address,
tokenOut: this.outputAmount.currency.address,
recipient: to,
tokenIn: isNativeIn ? WXDAI[ChainId.GNOSIS].address : this.inputAmount.currency.address,
tokenOut: isNativeOut ? WXDAI[ChainId.GNOSIS].address : this.outputAmount.currency.address,
recipient,
deadline: dayjs().add(30, 'm').unix(),
sqrtPriceLimitX96: 0,
fee: this.fee,
}

const value = isNativeIn ? amountIn : undefined

const exactInputSingleParams = {
...baseParams,
amountIn: amountIn,
Expand All @@ -223,7 +230,7 @@ export class SwaprV3Trade extends TradeWithSwapTransaction {

const methodName = isTradeExactInput ? 'exactInputSingle' : 'exactOutputSingle'
const params = isTradeExactInput ? exactInputSingleParams : exactOutputSingleParams
const populatedTransaction = await routerContract.populateTransaction[methodName](params)
const populatedTransaction = await routerContract.populateTransaction[methodName](params, { value })

return populatedTransaction
}
Expand Down
68 changes: 52 additions & 16 deletions src/entities/trades/swapr-v3/swapr-v3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SwaprV3Trade } from './SwaprV3'
import { ChainId, TradeType } from '../../../constants'
import { Percent, TokenAmount } from '../../fractions'
import { Token } from '../../token'
import { Currency } from '../../currency'

const maximumSlippage = new Percent('3', '100')
const tokenWXDAI = new Token(ChainId.GNOSIS, '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d', 18, 'WXDAI', 'WXDAI')
Expand Down Expand Up @@ -80,21 +81,37 @@ describe('SwaprV3', () => {
expect(trade?.inputAmount.currency.address).toBe(tokenUSDC.address)
})

// test('should return a EXACT INPUT quote on Gnosis for GNO - WETH', async () => {
// const currencyAmount = new TokenAmount(tokenGNO, parseUnits('1', 18).toString())
// const trade = await SwaprV3Trade.getQuote({
// amount: currencyAmount,
// quoteCurrency: tokenWETH,
// maximumSlippage,
// recipient,
// tradeType: TradeType.EXACT_INPUT,
// })

// expect(trade).toBeDefined()
// expect(trade?.chainId).toEqual(ChainId.GNOSIS)
// expect(trade?.tradeType).toEqual(TradeType.EXACT_INPUT)
// expect(trade?.outputAmount.currency.address).toBe(tokenWETH.address)
// })
test('should return a exact input quote for a native token XDAI - USDC', async () => {
const currencyAmount = new TokenAmount(tokenUSDC, parseUnits('2', 6).toString())
const trade = await SwaprV3Trade.getQuote({
quoteCurrency: Currency.getNative(ChainId.GNOSIS),
amount: currencyAmount,
maximumSlippage,
recipient,
tradeType: TradeType.EXACT_OUTPUT,
})

expect(trade).toBeDefined()
expect(trade?.chainId).toEqual(ChainId.GNOSIS)
expect(trade?.tradeType).toEqual(TradeType.EXACT_OUTPUT)
expect(trade?.outputAmount.currency.address).toBe(tokenUSDC.address)
})

// test('should return a EXACT INPUT quote on Gnosis for GNO - WETH', async () => {
// const currencyAmount = new TokenAmount(tokenGNO, parseUnits('1', 18).toString())
// const trade = await SwaprV3Trade.getQuote({
// amount: currencyAmount,
// quoteCurrency: tokenWETH,
// maximumSlippage,
// recipient,
// tradeType: TradeType.EXACT_INPUT,
// })

// expect(trade).toBeDefined()
// expect(trade?.chainId).toEqual(ChainId.GNOSIS)
// expect(trade?.tradeType).toEqual(TradeType.EXACT_INPUT)
// expect(trade?.outputAmount.currency.address).toBe(tokenWETH.address)
// })
})

describe('Swap', () => {
Expand All @@ -114,8 +131,27 @@ describe('SwaprV3', () => {
account: recipient,
}

const swap = await trade?.swapTransaction(swapOptions)
expect(swap !== undefined)
})
test('should return a swap on gnosis with native token XDAI - USDC', async () => {
const currencyAmount = new TokenAmount(tokenUSDC, parseUnits('2', 6).toString())

const trade = await SwaprV3Trade.getQuote({
quoteCurrency: Currency.getNative(ChainId.GNOSIS),
amount: currencyAmount,
maximumSlippage,
recipient,
tradeType: TradeType.EXACT_INPUT,
})

const swapOptions = {
recipient: recipient,
account: recipient,
}

const swap = await trade?.swapTransaction(swapOptions)
expect(swap !== undefined)
})
})
})
})
Loading