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

fix: rename zrx swapper functions #252

Merged
merged 7 commits into from
Nov 29, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { APPROVAL_GAS_LIMIT } from '../utils/constants'
import { setupQuote } from '../utils/test-data/setupSwapQuote'
import { setupZrxDeps } from '../utils/test-data/setupZrxDeps'
import { zrxService } from '../utils/zrxService'
import { approvalNeeded } from './approvalNeeded'
import { ZrxApprovalNeeded } from './ZrxApprovalNeeded'

jest.mock('web3')
jest.mock('axios', () => ({
Expand All @@ -28,7 +28,7 @@ Web3.mockImplementation(() => ({
}
}))

describe('approvalNeeded', () => {
describe('ZrxApprovalNeeded', () => {
const { web3Instance: web3, adapterManager } = setupZrxDeps()
const args = { web3, adapterManager }
const walletAddress = '0xc770eefad204b5180df6a14ee197d99d808ee52d'
Expand All @@ -44,7 +44,7 @@ describe('approvalNeeded', () => {
wallet
}

expect(await approvalNeeded(args, input)).toEqual({ approvalNeeded: false })
expect(await ZrxApprovalNeeded(args, input)).toEqual({ approvalNeeded: false })
})

it('throws an error if sellAsset chain is not ETH', async () => {
Expand All @@ -53,8 +53,8 @@ describe('approvalNeeded', () => {
wallet
}

await expect(approvalNeeded(args, input)).rejects.toThrow(
'ZrxSwapper:approvalNeeded only Ethereum chain type is supported'
await expect(ZrxApprovalNeeded(args, input)).rejects.toThrow(
'ZrxSwapper:ZrxApprovalNeeded only Ethereum chain type is supported'
)
})

Expand All @@ -74,7 +74,7 @@ describe('approvalNeeded', () => {
}))
;(zrxService.get as jest.Mock<unknown>).mockReturnValue(Promise.resolve({ data }))

expect(await approvalNeeded(args, input)).toEqual({
expect(await ZrxApprovalNeeded(args, input)).toEqual({
approvalNeeded: false,
gas: APPROVAL_GAS_LIMIT,
gasPrice: data.gasPrice
Expand All @@ -97,7 +97,7 @@ describe('approvalNeeded', () => {
}))
;(zrxService.get as jest.Mock<unknown>).mockReturnValue(Promise.resolve({ data }))

expect(await approvalNeeded(args, input)).toEqual({
expect(await ZrxApprovalNeeded(args, input)).toEqual({
approvalNeeded: true,
gas: APPROVAL_GAS_LIMIT,
gasPrice: data.gasPrice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { getERC20Allowance } from '../utils/helpers/helpers'
import { zrxService } from '../utils/zrxService'
import { ZrxSwapperDeps } from '../ZrxSwapper'

export async function approvalNeeded(
export async function ZrxApprovalNeeded(
{ adapterManager, web3 }: ZrxSwapperDeps,
{ quote, wallet }: ApprovalNeededInput<ChainTypes, SwapperType>
): Promise<ApprovalNeededOutput> {
Expand All @@ -31,7 +31,7 @@ export async function approvalNeeded(
}

if (sellAsset.chain !== ChainTypes.Ethereum) {
throw new SwapError('ZrxSwapper:approvalNeeded only Ethereum chain type is supported')
throw new SwapError('ZrxSwapper:ZrxApprovalNeeded only Ethereum chain type is supported')
}

const accountNumber = quote.sellAssetAccountId ? Number(quote.sellAssetAccountId) : 0
Expand Down Expand Up @@ -66,7 +66,7 @@ export async function approvalNeeded(
const { data } = quoteResponse

if (!quote.sellAsset.tokenId || !data.allowanceTarget) {
throw new SwapError('approvalNeeded - tokenId and allowanceTarget are required')
throw new SwapError('ZrxApprovalNeeded - tokenId and allowanceTarget are required')
}
const allowanceResult = await getERC20Allowance({
web3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Web3 from 'web3'
import { setupQuote } from '../utils/test-data/setupSwapQuote'
import { setupZrxDeps } from '../utils/test-data/setupZrxDeps'
import { zrxService } from '../utils/zrxService'
import { approveInfinite } from './approveInfinite'
import { ZrxApproveInfinite } from '../ZrxApproveInfinite/ZrxApproveInfinite'

jest.mock('web3')
jest.mock('../utils/helpers/helpers', () => ({
Expand All @@ -31,7 +31,7 @@ Web3.mockImplementation(() => ({
}
}))

describe('approveInfinite', () => {
describe('ZrxApproveInfinite', () => {
const { web3Instance, adapterManager } = setupZrxDeps()
const { quoteInput } = setupQuote()
const wallet = ({
Expand All @@ -45,6 +45,6 @@ describe('approveInfinite', () => {
const quote = { ...quoteInput }
;(zrxService.get as jest.Mock<unknown>).mockReturnValue(Promise.resolve({ data }))

expect(await approveInfinite(deps, { quote, wallet })).toEqual('grantAllowanceTxId')
expect(await ZrxApproveInfinite(deps, { quote, wallet })).toEqual('grantAllowanceTxId')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { grantAllowance } from '../utils/helpers/helpers'
import { zrxService } from '../utils/zrxService'
import { ZrxSwapperDeps } from '../ZrxSwapper'

export async function approveInfinite(
export async function ZrxApproveInfinite(
{ adapterManager, web3 }: ZrxSwapperDeps,
{ quote, wallet }: ApproveInfiniteInput<ChainTypes, SwapperType>
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Web3 from 'web3'
import { APPROVAL_GAS_LIMIT, DEFAULT_SLIPPAGE, MAX_SLIPPAGE } from '../utils/constants'
import { setupQuote } from '../utils/test-data/setupSwapQuote'
import { zrxService } from '../utils/zrxService'
import { buildQuoteTx } from './buildQuoteTx'
import { ZrxBuildQuoteTx } from './ZrxBuildQuoteTx'

jest.mock('web3')

Expand Down Expand Up @@ -120,7 +120,7 @@ const setup = () => {
return { web3Instance, adapterManager }
}

describe('buildQuoteTx', () => {
describe('ZrxBuildQuoteTx', () => {
const { quoteInput, sellAsset, buyAsset } = setupQuote()
const { web3Instance, adapterManager } = setup()
const walletAddress = '0xc770eefad204b5180df6a14ee197d99d808ee52d'
Expand All @@ -135,41 +135,41 @@ describe('buildQuoteTx', () => {
it('should throw error if sellAmount AND buyAmount is provided', async () => {
const input = { ...quoteInput, buyAmount: '1234.12', sellAmount: '1234.12' }

await expect(buildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:buildQuoteTx Exactly one of buyAmount or sellAmount is required'
await expect(ZrxBuildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:ZrxBuildQuoteTx Exactly one of buyAmount or sellAmount is required'
)
})

it('should throw error if sellAmount AND buyAmount are NOT provided', async () => {
const input = { ...quoteInput, sellAmount: '', buyAmount: '' }

await expect(buildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:buildQuoteTx Exactly one of buyAmount or sellAmount is required'
await expect(ZrxBuildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:ZrxBuildQuoteTx Exactly one of buyAmount or sellAmount is required'
)
})

it('should throw error if sellAssetAccountId is NOT provided', async () => {
const input = { ...quoteInput, sellAssetAccountId: '' }

await expect(buildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:buildQuoteTx Both sellAssetAccountId and buyAssetAccountId are required'
await expect(ZrxBuildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:ZrxBuildQuoteTx Both sellAssetAccountId and buyAssetAccountId are required'
)
})

it('should throw error if buyAssetAccountId is NOT provided', async () => {
const input = { ...quoteInput, buyAssetAccountId: '' }

await expect(buildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:buildQuoteTx Both sellAssetAccountId and buyAssetAccountId are required'
await expect(ZrxBuildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:ZrxBuildQuoteTx Both sellAssetAccountId and buyAssetAccountId are required'
)
})

it('should throw error if slippage is higher than MAX_SLIPPAGE', async () => {
const slippage = '31.0'
const input = { ...quoteInput, slippage }

await expect(buildQuoteTx(deps, { input, wallet })).rejects.toThrow(
`ZrxSwapper:buildQuoteTx slippage value of ${slippage} is greater than max slippage value of ${MAX_SLIPPAGE}`
await expect(ZrxBuildQuoteTx(deps, { input, wallet })).rejects.toThrow(
`ZrxSwapper:ZrxBuildQuoteTx slippage value of ${slippage} is greater than max slippage value of ${MAX_SLIPPAGE}`
)
})

Expand All @@ -184,8 +184,8 @@ describe('buildQuoteTx', () => {
}
} as unknown) as GetQuoteInput

await expect(buildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:buildQuoteTx One of buyAssetContract or buyAssetSymbol or buyAssetNetwork are required'
await expect(ZrxBuildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:ZrxBuildQuoteTx One of buyAssetContract or buyAssetSymbol or buyAssetNetwork are required'
)
})

Expand All @@ -200,8 +200,8 @@ describe('buildQuoteTx', () => {
}
} as unknown) as GetQuoteInput

await expect(buildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:buildQuoteTx One of sellAssetContract or sellAssetSymbol or sellAssetNetwork are required'
await expect(ZrxBuildQuoteTx(deps, { input, wallet })).rejects.toThrow(
'ZrxSwapper:ZrxBuildQuoteTx One of sellAssetContract or sellAssetSymbol or sellAssetNetwork are required'
)
})

Expand All @@ -219,7 +219,7 @@ describe('buildQuoteTx', () => {
}))
;(zrxService.get as jest.Mock<unknown>).mockReturnValue(Promise.resolve({ data }))

expect(await buildQuoteTx(deps, { input: quoteInput, wallet })).toEqual(mockQuoteResponse)
expect(await ZrxBuildQuoteTx(deps, { input: quoteInput, wallet })).toEqual(mockQuoteResponse)
})

it('should return a quote response with rate when price is given', async () => {
Expand All @@ -238,7 +238,7 @@ describe('buildQuoteTx', () => {
}))
;(zrxService.get as jest.Mock<unknown>).mockReturnValue(Promise.resolve({ data }))

expect(await buildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
expect(await ZrxBuildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
...mockQuoteResponse,
rate: price
})
Expand All @@ -254,7 +254,7 @@ describe('buildQuoteTx', () => {
}
;(zrxService.get as jest.Mock<unknown>).mockReturnValue(Promise.resolve({ data }))

expect(await buildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
expect(await ZrxBuildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
...mockQuoteResponse,
feeData: {
...mockQuoteResponse.feeData,
Expand All @@ -276,7 +276,7 @@ describe('buildQuoteTx', () => {
}
;(zrxService.get as jest.Mock<unknown>).mockReturnValue(Promise.resolve({ data }))

expect(await buildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
expect(await ZrxBuildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
...mockQuoteResponse,
allowanceContract: allowanceTarget
})
Expand All @@ -287,7 +287,7 @@ describe('buildQuoteTx', () => {
Promise.reject({ response: { data: { code: 400 } } })
)

expect(await buildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
expect(await ZrxBuildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
success: false,
statusCode: 400,
statusReason: 'Unknown Error',
Expand All @@ -301,7 +301,7 @@ describe('buildQuoteTx', () => {
Promise.reject({ response: { data: { code: 500 } } })
)

expect(await buildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
expect(await ZrxBuildQuoteTx(deps, { input: quoteInput, wallet })).toEqual({
success: false,
statusCode: 500,
statusReason: 'Unknown Error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { getAllowanceRequired, normalizeAmount } from '../utils/helpers/helpers'
import { zrxService } from '../utils/zrxService'
import { ZrxSwapperDeps } from '../ZrxSwapper'

export async function buildQuoteTx(
export async function ZrxBuildQuoteTx(
{ adapterManager, web3 }: ZrxSwapperDeps,
{ input, wallet }: BuildQuoteTxInput
): Promise<Quote<ChainTypes, SwapperType>> {
Expand All @@ -40,32 +40,32 @@ export async function buildQuoteTx(

if ((buyAmount && sellAmount) || (!buyAmount && !sellAmount)) {
throw new SwapError(
'ZrxSwapper:buildQuoteTx Exactly one of buyAmount or sellAmount is required'
'ZrxSwapper:ZrxBuildQuoteTx Exactly one of buyAmount or sellAmount is required'
)
}

if (!sellAssetAccountId || !buyAssetAccountId) {
throw new SwapError(
'ZrxSwapper:buildQuoteTx Both sellAssetAccountId and buyAssetAccountId are required'
'ZrxSwapper:ZrxBuildQuoteTx Both sellAssetAccountId and buyAssetAccountId are required'
)
}

const buyToken = buyAsset.tokenId || buyAsset.symbol || buyAsset.network
const sellToken = sellAsset.tokenId || sellAsset.symbol || sellAsset.network
if (!buyToken) {
throw new SwapError(
'ZrxSwapper:buildQuoteTx One of buyAssetContract or buyAssetSymbol or buyAssetNetwork are required'
'ZrxSwapper:ZrxBuildQuoteTx One of buyAssetContract or buyAssetSymbol or buyAssetNetwork are required'
)
}
if (!sellToken) {
throw new SwapError(
'ZrxSwapper:buildQuoteTx One of sellAssetContract or sellAssetSymbol or sellAssetNetwork are required'
'ZrxSwapper:ZrxBuildQuoteTx One of sellAssetContract or sellAssetSymbol or sellAssetNetwork are required'
)
}

if (buyAsset.chain !== ChainTypes.Ethereum) {
throw new SwapError(
`ZrxSwapper:buildQuoteTx buyAsset must be on chain [${ChainTypes.Ethereum}]`
`ZrxSwapper:ZrxBuildQuoteTx buyAsset must be on chain [${ChainTypes.Ethereum}]`
)
}

Expand All @@ -75,7 +75,7 @@ export async function buildQuoteTx(

if (new BigNumber(slippage || 0).gt(MAX_SLIPPAGE)) {
throw new SwapError(
`ZrxSwapper:buildQuoteTx slippage value of ${slippage} is greater than max slippage value of ${MAX_SLIPPAGE}`
`ZrxSwapper:ZrxBuildQuoteTx slippage value of ${slippage} is greater than max slippage value of ${MAX_SLIPPAGE}`
)
}

Expand Down
Loading