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 0x gasless swap metadata #333

Closed
Closed
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

- 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
43 changes: 20 additions & 23 deletions src/util/swapHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ export async function makeSwapPluginQuote(
tx.assetAction = assetAction
}
}
const action = tx.savedAction

if (action?.actionType !== 'swap') throw new Error(`Invalid swap action type`)
if (tx.savedAction?.actionType !== 'swap')
throw new Error(`Invalid swap action type`)

const toNativeAmount = action?.toAsset.nativeAmount
const destinationAddress = action?.payoutAddress
const isEstimate = action?.isEstimate ?? false
const quoteId = action?.orderId
const toNativeAmount = tx.savedAction?.toAsset.nativeAmount
const destinationAddress = tx.savedAction?.payoutAddress
const isEstimate = tx.savedAction?.isEstimate ?? false
const quoteId = tx.savedAction?.orderId
if (
fromNativeAmount == null ||
toNativeAmount == null ||
Expand Down Expand Up @@ -146,29 +146,26 @@ export async function makeSwapPluginQuote(
tx.metadata.notes = `${metadataNotes}\n\n` + (tx.metadata.notes ?? '')
}

const signedTransaction = await fromWallet.signTx(tx)
const broadcastedTransaction = await fromWallet.broadcastTx(
signedTransaction
)
const savedAction = signedTransaction.savedAction
const signedTx = await fromWallet.signTx(tx)
const broadcastedTransaction = await fromWallet.broadcastTx(signedTx)
if (
addTxidToOrderUri &&
savedAction != null &&
'orderUri' in savedAction
signedTx.savedAction != null &&
'orderUri' in signedTx.savedAction
) {
if (savedAction.orderUri != null)
savedAction.orderUri = `${savedAction.orderUri}${tx.txid}`
if (signedTx.savedAction.orderUri != null)
signedTx.savedAction.orderUri = `${signedTx.savedAction.orderUri}${tx.txid}`
}

await fromWallet.saveTx(signedTransaction)
await fromWallet.saveTx(signedTx)

// For token transactions that spend the parent gas currency, add
// a fee action
if (
signedTransaction.tokenId != null &&
signedTransaction.parentNetworkFee != null &&
signedTransaction.assetAction != null &&
savedAction != null
signedTx.tokenId != null &&
signedTx.parentNetworkFee != null &&
signedTx.assetAction != null &&
signedTx.savedAction != null
) {
// Only tag the network fee if any of the following is true:
// 1. Not a DEX transaction
Expand All @@ -179,16 +176,16 @@ export async function makeSwapPluginQuote(
fromWallet.id !== toWallet.id ||
(request.fromTokenId != null && request.toTokenId != null)
) {
const assetActionType: EdgeAssetActionType = signedTransaction.assetAction.assetActionType.startsWith(
const assetActionType: EdgeAssetActionType = signedTx.assetAction.assetActionType.startsWith(
'swap'
)
? 'swapNetworkFee'
: 'transferNetworkFee'
await fromWallet.saveTxAction({
txid: signedTransaction.txid,
txid: signedTx.txid,
tokenId: null,
assetAction: { assetActionType },
savedAction
savedAction: signedTx.savedAction
})
}
}
Expand Down
Loading