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

Implement max quotes for 0xGasless #339

Merged
merged 1 commit into from
Aug 12, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- fixed: Implemented max quotes for 0xGasless swap plugin

## 2.7.4 (2024-08-01)

- fixed: Thorchain swap error caused by failing cleaner
Expand Down
23 changes: 16 additions & 7 deletions src/swap/defi/0x/0xGasless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
EdgeTxAction
} from 'edge-core-js/types'

import { due } from '../../../util/due'
import { snooze } from '../../../util/utils'
import { EXPIRATION_MS, NATIVE_TOKEN_ADDRESS } from './constants'
import { asInitOptions } from './types'
Expand Down Expand Up @@ -51,9 +52,17 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => {
swapRequest.toTokenId
)

if (swapRequest.quoteFor === 'max') {
throw new Error('Max quotes not supported')
}
const swapNativeAmount: string = due(() => {
if (swapRequest.quoteFor === 'max') {
const balance = swapRequest.fromWallet.balanceMap.get(
swapRequest.fromTokenId
)
if (balance == null) throw new Error('No balance for the from token')
return balance
} else {
return swapRequest.nativeAmount
}
})

// From wallet address
const {
Expand All @@ -64,7 +73,7 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => {

// Amount request parameter/field name to use in the quote request
const amountField =
swapRequest.quoteFor === 'from' ? 'sellAmount' : 'buyAmount'
swapRequest.quoteFor === 'to' ? 'buyAmount' : 'sellAmount'

// Get quote from ZeroXApi
const chainId = api.getChainIdFromPluginId(
Expand All @@ -75,7 +84,7 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => {
sellToken: fromTokenAddress ?? NATIVE_TOKEN_ADDRESS,
buyToken: toTokenAddress ?? NATIVE_TOKEN_ADDRESS,
takerAddress: fromWalletAddress,
[amountField]: swapRequest.nativeAmount
[amountField]: swapNativeAmount
})

if (!apiSwapQuote.liquidityAvailable)
Expand Down Expand Up @@ -166,7 +175,7 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => {
fromAsset: {
pluginId: swapRequest.fromWallet.currencyInfo.pluginId,
tokenId: swapRequest.fromTokenId,
nativeAmount: swapRequest.nativeAmount
nativeAmount: swapNativeAmount
},
orderId,
// The payout address is the same as the fromWalletAddress because
Expand Down Expand Up @@ -197,7 +206,7 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => {
date: Date.now(),
isSend: true,
memos: [],
nativeAmount: swapRequest.nativeAmount,
nativeAmount: swapNativeAmount,
// There is no fee for a gasless swap
networkFee: '0',
ourReceiveAddresses: [],
Expand Down
11 changes: 11 additions & 0 deletions src/util/due.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Simple utility function to immediately invoke a function. It is clearer to
* read then an IIFE and is named after the `do` keyword in a future ECMAScript
* proposal.
*
* @param f function to immediately invoke
* @returns the return value of the function
*/
export function due<T>(f: () => T): T {
return f()
}
Loading