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 463a06d commit cc48480
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 27 deletions.
10 changes: 9 additions & 1 deletion packages/swapper/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { HDWallet } from '@shapeshiftoss/hdwallet-core'
import {
Asset,
ApprovalNeededInput,
ApprovalNeededOutput,
BuildQuoteTxInput,
GetQuoteInput,
Quote,
SwapperType,
ExecQuoteInput,
ExecQuoteOutput
ExecQuoteOutput,
} from '@shapeshiftoss/types'

export class SwapError extends Error {}
Expand Down Expand Up @@ -53,4 +55,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: 5 additions & 1 deletion packages/swapper/src/swappers/thorchain/ThorchainSwapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Asset, SwapperType, Quote, ExecQuoteOutput } from '@shapeshiftoss/types'
import { Asset, SwapperType, Quote, ExecQuoteOutput, ApprovalNeededOutput } from '@shapeshiftoss/types'
import { Swapper } from '../../api'

export class ThorchainSwapper implements Swapper {
Expand Down Expand Up @@ -31,4 +31,8 @@ export class ThorchainSwapper implements Swapper {
async executeQuote(): Promise<ExecQuoteOutput> {
throw new Error('ThorchainSwapper: executeQuote unimplemented')
}

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
Expand Up @@ -4,6 +4,8 @@ import BigNumber from 'bignumber.js'
import { zrxService } from './utils/zrxService'
import {
Asset,
ApprovalNeededInput,
ApprovalNeededOutput,
BuildQuoteTxInput,
ChainTypes,
GetQuoteInput,
Expand All @@ -19,6 +21,7 @@ import { Swapper } from '../../api'
import { buildQuoteTx } from './buildQuoteTx/buildQuoteTx'
import { getZrxQuote } from './getQuote/getQuote'
import { executeQuote } from './executeQuote/executeQuote'
import { approvalNeeded } from './approvalNeeded/approvalNeeded'

export type ZrxSwapperDeps = {
adapterManager: ChainAdapterManager
Expand Down Expand Up @@ -81,4 +84,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 @@ -184,6 +184,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 cc48480

Please sign in to comment.