Skip to content

Commit f4774e1

Browse files
Merge pull request #299 from reown-com/feat/swaps
feat: swaps
2 parents 8bbbb44 + be91240 commit f4774e1

File tree

85 files changed

+3524
-278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3524
-278
lines changed

.changeset/fair-ravens-shop.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@reown/appkit-scaffold-react-native': minor
3+
'@reown/appkit-ethers5-react-native': minor
4+
'@reown/appkit-common-react-native': minor
5+
'@reown/appkit-ethers-react-native': minor
6+
'@reown/appkit-wallet-react-native': minor
7+
'@reown/appkit-wagmi-react-native': minor
8+
'@reown/appkit-core-react-native': minor
9+
'@reown/appkit-ui-react-native': minor
10+
'@reown/appkit-auth-ethers-react-native': minor
11+
'@reown/appkit-auth-wagmi-react-native': minor
12+
'@reown/appkit-coinbase-ethers-react-native': minor
13+
'@reown/appkit-coinbase-wagmi-react-native': minor
14+
'@reown/appkit-scaffold-utils-react-native': minor
15+
'@reown/appkit-siwe-react-native': minor
16+
---
17+
18+
feat: swaps feature

apps/native/App.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '@reown/appkit-wagmi-react-native';
1616

1717
import { authConnector } from '@reown/appkit-auth-wagmi-react-native';
18+
import { Text } from '@reown/appkit-ui-react-native';
1819

1920
import { siweConfig } from './src/utils/SiweUtils';
2021

@@ -75,8 +76,9 @@ createAppKit({
7576
debug: true,
7677
features: {
7778
email: true,
78-
socials: ['x', 'discord', 'apple'],
79-
emailShowWallets: true
79+
socials: ['x', 'farcaster', 'discord', 'apple'],
80+
emailShowWallets: true,
81+
swaps: true
8082
}
8183
});
8284

@@ -88,6 +90,9 @@ export default function Native() {
8890
<QueryClientProvider client={queryClient}>
8991
<SafeAreaView style={[styles.container, isDarkMode && styles.dark]}>
9092
<StatusBar style="auto" />
93+
<Text variant="medium-title-600" style={styles.title}>
94+
AppKit for React Native
95+
</Text>
9196
<AppKitButton
9297
connectStyle={styles.button}
9398
accountStyle={styles.button}
@@ -121,6 +126,9 @@ const styles = StyleSheet.create({
121126
text: {
122127
marginBottom: 20
123128
},
129+
title: {
130+
marginBottom: 30
131+
},
124132
button: {
125133
marginVertical: 6
126134
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"test": "turbo run test --parallel",
3232
"clean": "turbo clean && rm -rf node_modules && watchman watch-del-all",
3333
"format": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\" --ignore-path .gitignore",
34-
"changeset:prepublish": "yarn run clean; yarn install; yarn version:update; yarn run lint && yarn run prettier; yarn run build; yarn run test;",
34+
"playwright:test": "cd apps/native && yarn playwright:test",
35+
"changeset:prepublish": "yarn run clean; yarn install; yarn version:update; yarn run lint && yarn run prettier; yarn run build; yarn run test",
3536
"changeset:publish": "yarn run changeset:prepublish; yarn run changeset publish",
3637
"changeset:version": "changeset version; yarn run version:update; yarn install --refresh-lockfile",
3738
"version:update": "./scripts/bump-version.sh"

packages/common/babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['module:metro-react-native-babel-preset']
3+
};

packages/common/jest.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const commonConfig = {
2+
...require('../../jest.config'),
3+
setupFilesAfterEnv: ['../../jest-setup.ts']
4+
};
5+
module.exports = commonConfig;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ConstantsUtil, NamesUtil } from '../index';
2+
3+
describe('NamesUtil', () => {
4+
describe('isReownName', () => {
5+
test('returns true for names ending with legacy suffix', () => {
6+
const legacyName = `testname${ConstantsUtil.WC_NAME_SUFFIX_LEGACY}`;
7+
expect(NamesUtil.isReownName(legacyName)).toBe(true);
8+
});
9+
10+
test('returns true for names ending with current suffix', () => {
11+
const currentName = `testname${ConstantsUtil.WC_NAME_SUFFIX}`;
12+
expect(NamesUtil.isReownName(currentName)).toBe(true);
13+
});
14+
15+
test('returns false for names not ending with either suffix', () => {
16+
expect(NamesUtil.isReownName('testname')).toBe(false);
17+
expect(NamesUtil.isReownName('testname.com')).toBe(false);
18+
});
19+
20+
test('returns false for empty string', () => {
21+
expect(NamesUtil.isReownName('')).toBe(false);
22+
});
23+
});
24+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { NumberUtil } from '../utils/NumberUtil';
2+
3+
// -- Tests --------------------------------------------------------------------
4+
describe('NumberUtil', () => {
5+
it('should return isGreaterThan as expected', () => {
6+
const isGreaterThan = NumberUtil.bigNumber('6.348').isGreaterThan('0');
7+
expect(isGreaterThan).toBe(true);
8+
});
9+
});
10+
11+
describe('NumberUtil.parseLocalStringToNumber', () => {
12+
it('should return 0 when value is undefined', () => {
13+
const result = NumberUtil.parseLocalStringToNumber(undefined);
14+
expect(result).toBe(0);
15+
});
16+
17+
it('should return the number when value is a string', () => {
18+
const result = NumberUtil.parseLocalStringToNumber('123.45');
19+
expect(result).toBe(123.45);
20+
});
21+
22+
it('should return the number when value is a string with a lot of decimals', () => {
23+
const result = NumberUtil.parseLocalStringToNumber('123.4567890123456789');
24+
expect(result).toBe(123.4567890123456789);
25+
});
26+
27+
it('should return the number when value is a string with zero and a lot of decimals', () => {
28+
const result = NumberUtil.parseLocalStringToNumber('0.000000000000000001');
29+
expect(result).toBe(0.000000000000000001);
30+
});
31+
32+
it('should return the number when value is a string with a negative sign', () => {
33+
const result = NumberUtil.parseLocalStringToNumber('-123.45');
34+
expect(result).toBe(-123.45);
35+
});
36+
37+
it('should return the number when value is a string with commas', () => {
38+
const result = NumberUtil.parseLocalStringToNumber('123,456.78');
39+
expect(result).toBe(123456.78);
40+
});
41+
42+
it('should return the number when value is a string with a lot of commas', () => {
43+
const result = NumberUtil.parseLocalStringToNumber('123,456,789.123,456,789');
44+
expect(result).toBe(123456789.123456789);
45+
});
46+
});

packages/common/src/utils/NumberUtil.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import * as BigNumber from 'bignumber.js';
22

33
export const NumberUtil = {
44
bigNumber(value: BigNumber.BigNumber.Value) {
5+
if (typeof value === 'string') {
6+
return new BigNumber.BigNumber(value.replace(/,/g, ''));
7+
}
8+
59
return new BigNumber.BigNumber(value);
610
},
711

@@ -16,8 +20,8 @@ export const NumberUtil = {
1620
return BigNumber.BigNumber(0);
1721
}
1822

19-
const aBigNumber = new BigNumber.BigNumber(a);
20-
const bBigNumber = new BigNumber.BigNumber(b);
23+
const aBigNumber = new BigNumber.BigNumber(typeof a === 'string' ? a.replace(/,/gu, '') : a);
24+
const bBigNumber = new BigNumber.BigNumber(typeof b === 'string' ? b.replace(/,/gu, '') : b);
2125

2226
return aBigNumber.multipliedBy(bBigNumber);
2327
},
@@ -27,5 +31,42 @@ export const NumberUtil = {
2731
number.toString().length >= threshold ? Number(number).toFixed(fixed) : number;
2832

2933
return roundedNumber;
34+
},
35+
36+
/**
37+
* Format the given number or string to human readable numbers with the given number of decimals
38+
* @param value - The value to format. It could be a number or string. If it's a string, it will be parsed to a float then formatted.
39+
* @param decimals - number of decimals after dot
40+
* @returns
41+
*/
42+
formatNumberToLocalString(value: string | number | undefined, decimals = 2) {
43+
if (value === undefined) {
44+
return '0.00';
45+
}
46+
47+
if (typeof value === 'number') {
48+
return value.toLocaleString('en-US', {
49+
maximumFractionDigits: decimals,
50+
minimumFractionDigits: decimals
51+
});
52+
}
53+
54+
return parseFloat(value).toLocaleString('en-US', {
55+
maximumFractionDigits: decimals,
56+
minimumFractionDigits: decimals
57+
});
58+
},
59+
/**
60+
* Parse a formatted local string back to a number
61+
* @param value - The formatted string to parse
62+
* @returns
63+
*/
64+
parseLocalStringToNumber(value: string | undefined) {
65+
if (value === undefined) {
66+
return 0;
67+
}
68+
69+
// Remove any commas used as thousand separators and parse the float
70+
return parseFloat(value.replace(/,/gu, ''));
3071
}
3172
};

packages/core/src/controllers/ApiController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const ApiController = {
7272
'x-sdk-type': sdkType,
7373
'x-sdk-version': sdkVersion,
7474
'User-Agent': ApiUtil.getUserAgent(),
75-
'Origin': ApiUtil.getOrigin()
75+
'origin': ApiUtil.getOrigin()
7676
};
7777
},
7878

0 commit comments

Comments
 (0)