From dc81f3ea559d47216163a6de2e8ab59f2880dd3a Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Wed, 26 Jun 2024 15:45:11 -0700 Subject: [PATCH] Remove networkFee for a 0xgasless swap Fee is paid out of the fromToken amount, so in other words, the exchange rate is adjusted to account for the fee. This means the user will always expect to pay the `nativeAmount` with no fee, but this does impact the exchange rate used to calculate the buy-token (to-token) amount. --- src/swap/defi/0x/0xGasless.ts | 54 ++++++++++++----------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/src/swap/defi/0x/0xGasless.ts b/src/swap/defi/0x/0xGasless.ts index f49f1dac..c51e8be3 100644 --- a/src/swap/defi/0x/0xGasless.ts +++ b/src/swap/defi/0x/0xGasless.ts @@ -1,8 +1,6 @@ -import { add } from 'biggystring' import { EdgeAssetAction, EdgeCorePluginFactory, - EdgeNetworkFee, EdgeSwapApproveOptions, EdgeSwapInfo, EdgeSwapQuote, @@ -44,14 +42,12 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => { throw new Error('Swap between different wallets is not supported') } - const fromTokenAddress = getTokenAddress( - swapRequest.fromWallet, - swapRequest.fromTokenId - ) - const toTokenAddress = getTokenAddress( - swapRequest.toWallet, - swapRequest.toTokenId - ) + const fromTokenAddress = + getTokenAddress(swapRequest.fromWallet, swapRequest.fromTokenId) ?? + NATIVE_TOKEN_ADDRESS + const toTokenAddress = + getTokenAddress(swapRequest.toWallet, swapRequest.toTokenId) ?? + NATIVE_TOKEN_ADDRESS if (swapRequest.quoteFor === 'max') { throw new Error('Max quotes not supported') @@ -74,8 +70,8 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => { ) const apiSwapQuote = await api.gaslessSwapQuote(chainId, { checkApproval: true, - sellToken: fromTokenAddress ?? NATIVE_TOKEN_ADDRESS, - buyToken: toTokenAddress ?? NATIVE_TOKEN_ADDRESS, + sellToken: fromTokenAddress, + buyToken: toTokenAddress, takerAddress: fromWalletAddress, [amountField]: swapRequest.nativeAmount }) @@ -94,28 +90,6 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => { throw new Error('Approval is required but gasless is not available') } - const { gasFee, zeroExFee } = apiSwapQuote.fees - - if ( - gasFee.feeToken.toLocaleLowerCase() !== - fromTokenAddress?.toLocaleLowerCase() || - zeroExFee.feeToken.toLocaleLowerCase() !== - fromTokenAddress?.toLocaleLowerCase() - ) { - throw new Error( - 'Quoted fees must be in the same token as the from token in the swap request' - ) - } - - const fromCurrencyCode = getCurrencyCode( - swapRequest.fromWallet, - swapRequest.fromTokenId - ) - const networkFee: EdgeNetworkFee = { - currencyCode: fromCurrencyCode, - nativeAmount: add(gasFee.feeAmount, zeroExFee.feeAmount) - } - return { approve: async ( opts?: EdgeSwapApproveOptions @@ -210,6 +184,10 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => { // Create the minimal transaction object for the swap. // Some values may be updated later when the transaction is // updated from queries to the network. + const fromCurrencyCode = getCurrencyCode( + swapRequest.fromWallet, + swapRequest.fromTokenId + ) const transaction: EdgeTransaction = { assetAction, blockHeight: 0, @@ -218,7 +196,8 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => { isSend: true, memos: [], nativeAmount: swapRequest.nativeAmount, - networkFee: networkFee.nativeAmount, + // There is no fee for a gasless swap + networkFee: '0', ourReceiveAddresses: [], savedAction, signedTx: '', // Signing is done by the tx-relay server @@ -239,7 +218,10 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => { expirationDate: new Date(Date.now() + EXPIRATION_MS), fromNativeAmount: apiSwapQuote.sellAmount, isEstimate: false, - networkFee, + networkFee: { + currencyCode: swapRequest.fromWallet.currencyInfo.currencyCode, + nativeAmount: '0' // There is no fee for a gasless swap + }, pluginId: swapInfo.pluginId, request: swapRequest, swapInfo: swapInfo,