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

refactor(exchange): extract params #3109

Merged
merged 9 commits into from
Mar 7, 2024
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
2 changes: 1 addition & 1 deletion apps/wallet-mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"@types/bip39": "^3.0.0",
"@types/pbkdf2": "^3.1.2",
"@yoroi/api": "1.5.1",
"@yoroi/exchange": "1.0.0",
"@yoroi/exchange": "2.0.0",
"@yoroi/common": "1.5.1",
"@yoroi/links": "1.5.1",
"@yoroi/resolver": "2.0.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export const RampOnOffScreen = () => {
headerShown: false,
}}
name="result-ramp-on-off"
component={() => <ShowExchangeResult variant="noInfo" />}
/>
>
{() => <ShowExchangeResult variant="noInfo" />}
</Stack.Screen>
banklesss marked this conversation as resolved.
Show resolved Hide resolved
</Stack.Navigator>
</RampOnOffProvider>
</SafeAreaView>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Banxa} from '@yoroi/exchange'
import {Exchange} from '@yoroi/types'
import BigNumber from 'bignumber.js'
import {produce} from 'immer'
import React from 'react'
Expand Down Expand Up @@ -140,7 +140,7 @@ const rampOnOffReducer = (state: RampOnOffState, action: RampOnOffAction) => {
})
}

export type OrderType = Banxa.ReferralUrlQueryStringParams['orderType']
export type OrderType = Exchange.ReferralUrlQueryStringParams['orderType']

type RampOnOffAction =
| {type: RampOnOffActionType.OrderTypeChanged; orderType: OrderType}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Banxa, banxaModuleMaker} from '@yoroi/exchange'
import {exchangeManagerMaker} from '@yoroi/exchange'
import {useTheme} from '@yoroi/theme'
import {Exchange} from '@yoroi/types'
import * as React from 'react'
import {Linking, StyleSheet, useWindowDimensions, View} from 'react-native'
import {ScrollView} from 'react-native-gesture-handler'
Expand Down Expand Up @@ -53,19 +54,19 @@ export const CreateExchange = () => {
const denomination = amountTokenInfo.decimals ?? 0
const orderAmount = +Quantities.denominated(quantity, denomination)

const urlOptions = {
const urlOptions: Exchange.ReferralUrlQueryStringParams = {
orderType: orderType,
fiatType: 'USD',
coinType: 'ADA',
coinAmount: orderAmount ?? 0,
blockchain: 'ADA',
walletAddress,
returnUrl,
} as Banxa.ReferralUrlQueryStringParams
}

const banxa = banxaModuleMaker(moduleOptions)
const url = banxa.createReferralUrl(urlOptions)
Linking.openURL(url.toString())
const exchange = exchangeManagerMaker(moduleOptions)
const banxaUrl = exchange.createReferralUrl(Exchange.Provider.Banxa, urlOptions)
Linking.openURL(banxaUrl.toString())
track.exchangeSubmitted({ramp_type: orderType === 'sell' ? 'Sell' : 'Buy', ada_amount: orderAmount})
navigateTo.rampOnOffOpenOrder()
}
Expand Down
47 changes: 47 additions & 0 deletions packages/common/src/helpers/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isUrl,
isKeyOf,
getKeys,
isStringLiteral,
} from './parsers'

describe('parsers', () => {
Expand Down Expand Up @@ -283,4 +284,50 @@ describe('parsers', () => {
expect(getKeys({})).toEqual([])
})
})

describe('isStringLiteral function', () => {
const literalsArray = ['apple', 'banana', 'cherry']

it('should return true for a value that is part of the literals array', () => {
const value = 'banana'
const result = isStringLiteral(literalsArray, value)
expect(result).toBe(true)
})

it('should return false for a value that is not part of the literals array', () => {
const value = 'orange'
const result = isStringLiteral(literalsArray, value)
expect(result).toBe(false)
})

it('should return false for a number value when literals array contains only strings', () => {
const value = 123
const result = isStringLiteral(literalsArray, value)
expect(result).toBe(false)
})

it('should return false for an object value when literals array contains only strings', () => {
const value = {fruit: 'banana'}
const result = isStringLiteral(literalsArray, value)
expect(result).toBe(false)
})

it('should return false for a boolean value when literals array contains only strings', () => {
const value = true
const result = isStringLiteral(literalsArray, value)
expect(result).toBe(false)
})

it('should return false for null when literals array contains only strings', () => {
const value = null
const result = isStringLiteral(literalsArray, value)
expect(result).toBe(false)
})

it('should return false for undefined when literals array contains only strings', () => {
const value = undefined
const result = isStringLiteral(literalsArray, value)
expect(result).toBe(false)
})
})
})
7 changes: 7 additions & 0 deletions packages/common/src/helpers/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,10 @@ export function isArrayOfType<T>(
): data is Array<T> {
return isArray(data) && data.every(predicate)
}

export const isStringLiteral = <T extends string>(
literals: Readonly<T[]>,
value: unknown,
) => {
return literals.includes(value as T)
}
21 changes: 13 additions & 8 deletions packages/exchange/README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
# Yoroi Exchange Module

The Yoroi Exchange package is a utility for interacting with exchanges resources/APIs.
The Yoroi Exchange package is a utility for interacting with exchanges the folowwing resources/APIs:

- [Banxa](https://banxa.com/)

## Installation

Install the package using npm or yarn :

```bash
npm install @yoroi/exchange --save
npm install @yoroi/types --save-dev
```
```bash
yarn add @yoroi/exchange --save
yarn add @yoroi/types --save-dev
```

## Usage

### Generating a Banxa referral URL to redirect/open
```typescript
import { Banxa, banxaModuleMaker } from '@yoroi/exchange';
import { exchangeManagerMaker } from '@yoroi/exchange';
import { Exchange } from '@yoroi/types';

const options: Banxa.ReferralUrlBuilderOptions = {
const options: Exchange.ManagerOptions = {
isProduction: true,
partner: 'emurgo',
};

const params: Banxa.ReferralUrlQueryStringParams = {
const params: Exchange.ReferralUrlQueryStringParams = {
fiatType: 'USD',
coinType: 'ADA',
walletAddress:
'addr1q9v8dvht2mv847gwarl7r4p49yzys8r7zlep7c8t2hqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqquvupf',
};

const banxa = banxaModuleMaker(options);
const module = exchangeManagerMaker(Exchange.Provider.Banxa, options);

const url = banxa.createReferralUrl(params);
const url = module.createReferralUrl(params);

console.log(url.toString())
```
Expand All @@ -43,9 +48,9 @@ console.log(url.toString())
try {
// some Banxa code
} catch (error) {
if (error instanceof Banxa.ValidationError) {
if (error instanceof Exchange.Error.Validation) {
console.error("Validation error:", error.message);
} else if (error instanceof Banxa.UnknownError) {
} else if (error instanceof Exchange.Error.Unknown) {
console.error("Unknown error:", error.message);
}
}
Expand Down
7 changes: 3 additions & 4 deletions packages/exchange/jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// jest.setup.js

// Currently, no global setup is required for our tests.
// This file is reserved for potential future needs.
jest.mock('@react-native-async-storage/async-storage', () =>
require('@react-native-async-storage/async-storage/jest/async-storage-mock')
)
10 changes: 6 additions & 4 deletions packages/exchange/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@yoroi/exchange",
"version": "1.0.0",
"description": "The Banxa integration package of Yoroi SDK",
"version": "2.0.0",
"description": "The Exchange package of Yoroi SDK",
"keywords": [
"yoroi",
"cardano",
Expand Down Expand Up @@ -127,11 +127,12 @@
]
},
"dependencies": {
"@yoroi/common": "1.5.1",
"zod": "^3.22.2"
},
"devDependencies": {
"@commitlint/config-conventional": "^17.0.2",
"@react-native-async-storage/async-storage": "^1.18.1",
"@react-native-async-storage/async-storage": "^1.19.3",
"@react-native-community/eslint-config": "^3.0.2",
"@release-it/conventional-changelog": "^5.0.0",
"@testing-library/react-hooks": "^8.0.1",
Expand Down Expand Up @@ -159,11 +160,12 @@
"typescript": "^5.3.3"
},
"peerDependencies": {
"@react-native-async-storage/async-storage": ">= 1.19.3 <= 1.20.0",
"react": ">= 16.8.0 <= 19.0.0",
"react-query": "^3.39.3"
},
"optionalDependencies": {
"@react-native-async-storage/async-storage": "^1.18.1"
"@react-native-async-storage/async-storage": "^1.19.3"
},
"packageManager": "yarn@1.22.21",
"engines": {
Expand Down
43 changes: 43 additions & 0 deletions packages/exchange/src/adapters/banxa/base-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
banxaDomainProduction,
banxaDomainSandbox,
} from '../../translators/banxa/domains'
import {generateBanxaBaseUrl} from './base-url'

describe('generateBanxaBaseUrl function', () => {
it('should generate a production base URL for a given partner', () => {
const isProduction = true
const partner = 'example'
const expectedBaseUrl = `https://example.${banxaDomainProduction}`
const baseUrl = generateBanxaBaseUrl(isProduction, partner)

expect(baseUrl).toBe(expectedBaseUrl)
})

it('should generate a sandbox base URL for a given partner', () => {
const isProduction = false
const partner = 'example'
const expectedBaseUrl = `https://example.${banxaDomainSandbox}`
const baseUrl = generateBanxaBaseUrl(isProduction, partner)

expect(baseUrl).toBe(expectedBaseUrl)
})

it('should correctly handle partner names with special characters', () => {
const isProduction = true
const partner = 'example-partner'
const expectedBaseUrl = `https://example-partner.${banxaDomainProduction}`
const baseUrl = generateBanxaBaseUrl(isProduction, partner)

expect(baseUrl).toBe(expectedBaseUrl)
})

it('should generate a URL with the correct domain when isProduction is undefined', () => {
const isProduction = undefined
const partner = 'example'
const expectedBaseUrl = `https://example.${banxaDomainSandbox}`
const baseUrl = generateBanxaBaseUrl(isProduction, partner)

expect(baseUrl).toBe(expectedBaseUrl)
})
})
15 changes: 15 additions & 0 deletions packages/exchange/src/adapters/banxa/base-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Exchange} from '@yoroi/types'
import {
banxaDomainProduction,
banxaDomainSandbox,
} from '../../translators/banxa/domains'

export const generateBanxaBaseUrl = (
isProduction: Exchange.ManagerOptions['isProduction'],
partner: string,
) => {
const domain = isProduction ? banxaDomainProduction : banxaDomainSandbox
const baseUrl = `https://${partner}.${domain}`

return baseUrl
}
18 changes: 0 additions & 18 deletions packages/exchange/src/adapters/banxa/errors.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/exchange/src/adapters/banxa/zod-schema.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {handleZodErrors} from '../zod-errors'
import {BanxaUnknownError, BanxaValidationError} from './errors'
import {Exchange} from '@yoroi/types'
import {handleZodErrors} from './zod-errors'
import {z} from 'zod'

describe('handleZodErrors', () => {
Expand All @@ -26,7 +26,7 @@ describe('handleZodErrors', () => {
} catch (e: any) {
handledError = e

expect(handledError).toBeInstanceOf(BanxaValidationError)
expect(handledError).toBeInstanceOf(Exchange.Errors.Validation)
expect(handledError?.message).toBe(
'Invalid data: name: Expected string, received number, age: Expected number, received string',
)
Expand All @@ -44,6 +44,6 @@ describe('handleZodErrors', () => {
handledError = e
}

expect(handledError).toBeInstanceOf(BanxaUnknownError)
expect(handledError).toBeInstanceOf(Exchange.Errors.Unknown)
})
})
Loading
Loading