diff --git a/CHANGELOG.md b/CHANGELOG.md index 9234eb72..2127119a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- added: 0x Gasless Swap plugin + ## 2.4.3 (2024-06-03) - added: (Exolix) Add Piratechain diff --git a/src/swap/defi/0x/0xGasless.ts b/src/swap/defi/0x/0xGasless.ts index c9b5b408..cbd39f02 100644 --- a/src/swap/defi/0x/0xGasless.ts +++ b/src/swap/defi/0x/0xGasless.ts @@ -1,12 +1,14 @@ import { add } from 'biggystring' import { + EdgeAssetAction, EdgeCorePluginFactory, EdgeNetworkFee, EdgeSwapApproveOptions, EdgeSwapInfo, EdgeSwapQuote, EdgeSwapResult, - EdgeTransaction + EdgeTransaction, + EdgeTxAction } from 'edge-core-js/types' import { snooze } from '../../../util/utils' @@ -175,10 +177,42 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => { throw new Error(`Swap failed: ${apiSwapStatus.reason ?? 'unknown'}`) } + const assetAction: EdgeAssetAction = { + assetActionType: 'swap' + } + const orderId = apiSwapSubmition.tradeHash + const { + publicAddress: toWalletAddress + } = await swapRequest.toWallet.getReceiveAddress({ + tokenId: swapRequest.toTokenId + }) + + const savedAction: EdgeTxAction = { + actionType: 'swap', + canBePartial: false, + isEstimate: false, + fromAsset: { + pluginId: swapRequest.fromWallet.currencyInfo.pluginId, + tokenId: swapRequest.fromTokenId, + nativeAmount: swapRequest.nativeAmount + }, + orderId, + payoutAddress: toWalletAddress, + payoutWalletId: swapRequest.toWallet.id, + refundAddress: fromWalletAddress, + swapInfo, + toAsset: { + pluginId: swapRequest.toWallet.currencyInfo.pluginId, + tokenId: swapRequest.toTokenId, + nativeAmount: apiSwapQuote.buyAmount + } + } + // 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 transaction: EdgeTransaction = { + assetAction, blockHeight: 0, currencyCode: fromCurrencyCode, date: Date.now(), @@ -187,7 +221,8 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => { nativeAmount: swapRequest.nativeAmount, networkFee: networkFee.nativeAmount, ourReceiveAddresses: [], - signedTx: '', + savedAction, + signedTx: '', // Signing is done by the tx-relay server tokenId: swapRequest.fromTokenId, txid: apiSwapStatus.transactions[0].hash, walletId: swapRequest.fromWallet.id @@ -196,8 +231,18 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => { // Don't forget to save the transaction to the wallet: await swapRequest.fromWallet.saveTx(transaction) + // Save TX action for native currency if it's not a token swap: + if (transaction.tokenId != null) { + await swapRequest.fromWallet.saveTxAction({ + txid: transaction.txid, + tokenId: null, + assetAction: { assetActionType: 'swapNetworkFee' }, + savedAction + }) + } + return { - orderId: apiSwapSubmition.tradeHash, + orderId, transaction } },