Skip to content

Commit

Permalink
Implement 0x gasless swap metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
samholmes committed Jun 25, 2024
1 parent 9bfe6bf commit 286f3da
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
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

- added: 0x Gasless Swap plugin

## 2.4.3 (2024-06-03)

- added: (Exolix) Add Piratechain
Expand Down
51 changes: 48 additions & 3 deletions src/swap/defi/0x/0xGasless.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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(),
Expand All @@ -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
Expand All @@ -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
}
},
Expand Down

0 comments on commit 286f3da

Please sign in to comment.