-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement 0x gasless swap approve method
- Loading branch information
Showing
4 changed files
with
309 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,20 @@ import { | |
EdgeSwapInfo, | ||
EdgeSwapPlugin, | ||
EdgeSwapQuote, | ||
EdgeSwapResult | ||
EdgeSwapResult, | ||
EdgeTransaction | ||
} from 'edge-core-js/types' | ||
|
||
import { snooze } from '../../../util/utils' | ||
import { ZeroXApi } from './api' | ||
import { GaslessSwapStatusResponse, GaslessSwapSubmitRequest } from './apiTypes' | ||
import { EXPIRATION_MS, NATIVE_TOKEN_ADDRESS } from './constants' | ||
import { asInitOptions } from './types' | ||
import { getCurrencyCode, getTokenAddress } from './util' | ||
import { getCurrencyCode, getTokenAddress, makeSignatureStruct } from './util' | ||
|
||
const swapInfo: EdgeSwapInfo = { | ||
displayName: '0x Gasless Swap', | ||
isDex: true, | ||
pluginId: 'zeroxgaslessswap', | ||
supportEmail: '[email protected]' | ||
} | ||
|
@@ -73,6 +77,7 @@ export const make0xGaslessSwap: EdgeCorePluginFactory = ( | |
swapRequest.fromWallet.currencyInfo.pluginId | ||
) | ||
const apiSwapQuote = await api.gaslessSwapQuote(chainId, { | ||
checkApproval: true, | ||
sellToken: fromTokenAddress ?? NATIVE_TOKEN_ADDRESS, | ||
buyToken: toTokenAddress ?? NATIVE_TOKEN_ADDRESS, | ||
takerAddress: fromWalletAddress, | ||
|
@@ -82,6 +87,17 @@ export const make0xGaslessSwap: EdgeCorePluginFactory = ( | |
if (!apiSwapQuote.liquidityAvailable) | ||
throw new Error('No liquidity available') | ||
|
||
// The plugin only supports gasless swaps, so if approval is required | ||
// it must be gasless. | ||
if ( | ||
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
apiSwapQuote.approval != null && | ||
apiSwapQuote.approval.isRequired && | ||
!apiSwapQuote.approval.isGaslessAvailable | ||
) { | ||
throw new Error('Approval is required but gasless is not available') | ||
} | ||
|
||
const { gasFee, zeroExFee } = apiSwapQuote.fees | ||
|
||
if ( | ||
|
@@ -104,7 +120,88 @@ export const make0xGaslessSwap: EdgeCorePluginFactory = ( | |
approve: async ( | ||
opts?: EdgeSwapApproveOptions | ||
): Promise<EdgeSwapResult> => { | ||
throw new Error('Approve not yet implemented') | ||
let approvalData: GaslessSwapSubmitRequest['approval'] | ||
if ( | ||
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
apiSwapQuote.approval != null && | ||
apiSwapQuote.approval.isRequired | ||
) { | ||
// Assert that that approval is gasless, otherwise it would have | ||
// been caught above, so this case should be unreachable. | ||
if (!apiSwapQuote.approval.isGaslessAvailable) { | ||
throw new Error('Unreachable non-gasless approval condition') | ||
} | ||
|
||
const approvalTypeData = JSON.stringify( | ||
apiSwapQuote.approval.eip712 | ||
) | ||
const approvalSignatureHash = await swapRequest.fromWallet.signMessage( | ||
approvalTypeData, | ||
{ otherParams: { typedData: true } } | ||
) | ||
const approvalSignature = makeSignatureStruct(approvalSignatureHash) | ||
approvalData = { | ||
type: apiSwapQuote.approval.type, | ||
eip712: apiSwapQuote.approval.eip712, | ||
signature: approvalSignature | ||
} | ||
} | ||
|
||
const tradeTypeData = JSON.stringify(apiSwapQuote.trade.eip712) | ||
const tradeSignatureHash = await swapRequest.fromWallet.signMessage( | ||
tradeTypeData, | ||
{ otherParams: { typedData: true } } | ||
) | ||
const tradeSignature = makeSignatureStruct(tradeSignatureHash) | ||
const tradeData: GaslessSwapSubmitRequest['trade'] = { | ||
type: apiSwapQuote.trade.type, | ||
eip712: apiSwapQuote.trade.eip712, | ||
signature: tradeSignature | ||
} | ||
|
||
const apiSwapSubmition = await api.gaslessSwapSubmit(chainId, { | ||
...(approvalData !== undefined ? { approval: approvalData } : {}), | ||
trade: tradeData | ||
}) | ||
|
||
let apiSwapStatus: GaslessSwapStatusResponse | ||
do { | ||
// Wait before checking | ||
await snooze(500) | ||
apiSwapStatus = await api.gaslessSwapStatus( | ||
chainId, | ||
apiSwapSubmition.tradeHash | ||
) | ||
} while (apiSwapStatus.status === 'pending') | ||
|
||
if (apiSwapStatus.status === 'failed') { | ||
throw new Error(`Swap failed: ${apiSwapStatus.reason ?? 'unknown'}`) | ||
} | ||
|
||
// 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 = { | ||
txid: apiSwapStatus.transactions[0].hash.slice(2), // Remove '0x' | ||
date: Date.now(), | ||
currencyCode: fromCurrencyCode, | ||
blockHeight: 0, | ||
nativeAmount: swapRequest.nativeAmount, | ||
networkFee: networkFee.nativeAmount, | ||
ourReceiveAddresses: [], | ||
signedTx: '', | ||
tokenId: swapRequest.fromTokenId, | ||
memos: [], | ||
isSend: true, | ||
walletId: swapRequest.fromWallet.id | ||
} | ||
|
||
console.log('!@!', transaction) | ||
|
||
return { | ||
orderId: apiSwapSubmition.tradeHash, | ||
transaction | ||
} | ||
}, | ||
close: async () => {}, | ||
expirationDate: new Date(Date.now() + EXPIRATION_MS), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.