Skip to content
This repository was archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
add approvalNeeded to ZrxSwapper
Browse files Browse the repository at this point in the history
  • Loading branch information
DaoDev44 committed Oct 5, 2021
1 parent d1cf1be commit 675f421
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 25 deletions.
8 changes: 8 additions & 0 deletions packages/swapper/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { HDWallet } from '@shapeshiftoss/hdwallet-core'
import {
Asset,
ApprovalNeededInput,
ApprovalNeededOutput,
BuildQuoteTxInput,
GetQuoteInput,
Quote,
Expand Down Expand Up @@ -65,4 +67,10 @@ export interface Swapper {
* @param wallet
*/
executeQuote(args: ExecQuoteInput): Promise<ExecQuoteOutput>

/**
* Get a boolean if a quote needs approval
*/

approvalNeeded(args: ApprovalNeededInput): Promise<ApprovalNeededOutput>
}
6 changes: 6 additions & 0 deletions packages/swapper/src/swappers/thorchain/ThorchainSwapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Asset,
ApprovalNeededOutput,
SwapperType,
Quote,
ExecQuoteOutput,
Expand Down Expand Up @@ -35,6 +36,7 @@ export class ThorchainSwapper implements Swapper {
console.info(assets)
throw new Error('ThorchainSwapper: getAvailableAssets unimplemented')
}

canTradePair(sellAsset: Asset, buyAsset: Asset): boolean {
console.info(sellAsset, buyAsset)
throw new Error('ThorchainSwapper: canTradePair unimplemented')
Expand All @@ -47,4 +49,8 @@ export class ThorchainSwapper implements Swapper {
getDefaultPair(): Partial<Asset>[] {
throw new Error('Method not implemented.')
}

async approvalNeeded(): Promise<ApprovalNeededOutput> {
throw new Error('ThorchainSwapper: executeQuote unimplemented')
}
}
7 changes: 7 additions & 0 deletions packages/swapper/src/swappers/zrx/ZrxSwapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Web3 from 'web3'
import {
Asset,
ApprovalNeededInput,
ApprovalNeededOutput,
BuildQuoteTxInput,
ChainTypes,
GetQuoteInput,
Expand All @@ -17,6 +19,7 @@ import { getZrxQuote } from './getQuote/getQuote'
import { getUsdRate } from './utils/helpers/helpers'
import { getMinMax } from './getMinMax/getMinMax'
import { executeQuote } from './executeQuote/executeQuote'
import { approvalNeeded } from './approvalNeeded/approvalNeeded'

export type ZrxSwapperDeps = {
adapterManager: ChainAdapterManager
Expand Down Expand Up @@ -76,4 +79,8 @@ export class ZrxSwapper implements Swapper {
async executeQuote(args: ExecQuoteInput): Promise<ExecQuoteOutput> {
return executeQuote(this.deps, args)
}

async approvalNeeded(args: ApprovalNeededInput): Promise<ApprovalNeededOutput> {
return approvalNeeded(this.deps, args)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Web3 from 'web3'
import { HDWallet } from '@shapeshiftoss/hdwallet-core'
import { ChainTypes } from '@shapeshiftoss/types'
import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters'
import { approvalNeeded } from './approvalNeeded'
import { setupQuote } from '../utils/test-data/setupSwapQuote'

const setup = () => {
const unchainedUrls = {
[ChainTypes.Ethereum]: 'http://localhost:31300/api/v1'
}
const ethNodeUrl = 'http://localhost:1000'
const adapterManager = new ChainAdapterManager(unchainedUrls)
const web3Provider = new Web3.providers.HttpProvider(ethNodeUrl)
const web3 = new Web3(web3Provider)

return { web3, adapterManager }
}

describe('approvalNeeded', () => {
const args = setup()
const wallet = <HDWallet>{}
const { quoteInput, sellAsset } = setupQuote()

it('returns false if sellAsset symbol is ETH', async () => {
const input = { quote: { ...quoteInput, sellAsset: { ...sellAsset, symbol: 'ETH' } }, wallet }

expect(await approvalNeeded(args, input)).toEqual({ approvalNeeded: false })
})
})
22 changes: 22 additions & 0 deletions packages/swapper/src/swappers/zrx/approvalNeeded/approvalNeeded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AxiosResponse } from 'axios'
import { ChainAdapter } from '@shapeshiftoss/chain-adapters'
import { ApprovalNeededInput, ApprovalNeededOutput, ChainTypes, QuoteResponse } from '@shapeshiftoss/types'
import { DEFAULT_SLIPPAGE, AFFILIATE_ADDRESS, DEFAULT_ETH_PATH } from '../utils/constants'

import { ZrxSwapperDeps } from '../ZrxSwapper'
import { zrxService } from '../utils/zrxService'
const APPROVAL_BUY_AMOUNT = '100000000000000000' // A valid buy amount - 0.1 ETH

export async function approvalNeeded(
{ adapterManager }: ZrxSwapperDeps,
{ quote, wallet }: ApprovalNeededInput
): Promise<ApprovalNeededOutput> {
const { sellAsset } = quote

if (sellAsset.symbol === 'ETH' || sellAsset.chain !== ChainTypes.Ethereum) {
return { approvalNeeded: false }
}


return { approvalNeeded: true }
}
26 changes: 1 addition & 25 deletions packages/swapper/src/swappers/zrx/buildQuoteTx/buildQuoteTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AxiosResponse } from 'axios'
import * as rax from 'retry-axios'
import { ChainAdapter } from '@shapeshiftoss/chain-adapters'
import { SwapError } from '../../..'
import { Quote, BuildQuoteTxInput } from '@shapeshiftoss/types'
import { Quote, QuoteResponse, BuildQuoteTxInput } from '@shapeshiftoss/types'
import { ZrxSwapperDeps } from '../ZrxSwapper'
import { applyAxiosRetry } from '../utils/applyAxiosRetry'
import { erc20AllowanceAbi } from '../utils/abi/erc20-abi'
Expand All @@ -18,30 +18,6 @@ import {
MAX_SLIPPAGE
} from '../utils/constants'

type LiquiditySource = {
name: string
proportion: string
}

type QuoteResponse = {
price: string
guaranteedPrice: string
to: string
data?: string
value?: string
gas?: string
estimatedGas?: string
gasPrice?: string
protocolFee?: string
minimumProtocolFee?: string
buyTokenAddress?: string
sellTokenAddress?: string
buyAmount?: string
sellAmount?: string
allowanceTarget?: string
sources?: Array<LiquiditySource>
}

export async function buildQuoteTx(
{ adapterManager, web3 }: ZrxSwapperDeps,
{ input, wallet }: BuildQuoteTxInput
Expand Down
11 changes: 11 additions & 0 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ export type ExecQuoteOutput = {
txid: string
}

export type ApprovalNeededInput = {
quote: Quote
wallet: HDWallet
}

export type ApprovalNeededOutput = {
approvalNeeded: boolean
gas?: string
gasPrice?: string
}

// chain-adapters

export type Transaction = {
Expand Down

0 comments on commit 675f421

Please sign in to comment.