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,