Skip to content

Commit

Permalink
Merge pull request #67 from map3xyz/phil/map-108-add-the-following-fi…
Browse files Browse the repository at this point in the history
…elds-to

feat: add userId to Map3InitConfig
  • Loading branch information
plondon authored Jan 8, 2023
2 parents 4b33f8d + f697b8e commit 26370ed
Show file tree
Hide file tree
Showing 24 changed files with 314 additions and 285 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enabling cross-chain deposits and increasing volumes.
const map3 = initMap3Sdk({
theme: 'dark',
anonKey: '<ANON_KEY>',
generateDepositAddress: async (coin, network, memoEnabled) => {
generateDepositAddress: async (coin, network) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return {address: '0x0000000000000000000000000000000000000000'};
Expand Down
8 changes: 8 additions & 0 deletions jest/__mocks__/mockConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const mockConfig = {
anonKey: process.env.CONSOLE_ANON_KEY || '',
generateDepositAddress: async () => {
return { address: '0x0000000000000000000000000000000000000000' };
},
theme: 'dark' as const,
userId: 'test',
};
10 changes: 10 additions & 0 deletions jest/__mocks__/web3Mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const web3Mock = {
addChain: jest.fn(),
authorizeTransactionProxy: jest.fn(),
getBalance: jest.fn(),
getChainId: jest.fn(),
providers: {},
sendTransaction: jest.fn(),
switchChain: jest.fn(),
waitForTransaction: jest.fn(),
};
51 changes: 7 additions & 44 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { mockConfig } from '~/jest/__mocks__/mockConfig';
import { render, screen } from '~/jest/test-utils';

import App from './App';

describe('App', () => {
it('renders', async () => {
render(
<App
config={{
anonKey: process.env.CONSOLE_ANON_KEY || '',
generateDepositAddress: async () => {
return { address: '0x0000000000000000000000000000000000000000' };
},
theme: 'dark',
}}
onClose={() => {}}
/>
);
render(<App config={mockConfig} onClose={() => {}} />);

expect(await screen.findByText('Loading...')).toBeInTheDocument();
const assetSelection = await screen.findByText('Select Asset');
Expand All @@ -26,12 +16,8 @@ describe('App', () => {
render(
<App
config={{
anonKey: process.env.CONSOLE_ANON_KEY || '',
...mockConfig,
assetId: 'satoshi123',
generateDepositAddress: async () => {
return { address: '0x0000000000000000000000000000000000000000' };
},
theme: 'dark',
}}
onClose={() => {}}
/>
Expand All @@ -48,13 +34,9 @@ describe('App', () => {
render(
<App
config={{
...mockConfig,
address: '0xf61B443A155b07D2b2cAeA2d99715dC84E839EEf',
anonKey: process.env.CONSOLE_ANON_KEY || '',
generateDepositAddress: async () => {
return { address: '0x0000000000000000000000000000000000000000' };
},
networkCode: 'ethereum',
theme: 'dark',
}}
onClose={() => {}}
/>
Expand All @@ -71,12 +53,8 @@ describe('App', () => {
render(
<App
config={{
anonKey: process.env.CONSOLE_ANON_KEY || '',
generateDepositAddress: async () => {
return { address: '0x0000000000000000000000000000000000000000' };
},
...mockConfig,
networkCode: 'bitcoin',
theme: 'dark',
}}
onClose={() => {}}
/>
Expand All @@ -93,15 +71,11 @@ describe('App', () => {
render(
<App
config={{
anonKey: process.env.CONSOLE_ANON_KEY || '',
...mockConfig,
authorizeTransaction: async () => {
return true;
},
generateDepositAddress: async () => {
return { address: '0x0000000000000000000000000000000000000000' };
},
rainbowRoad: true,
theme: 'dark',
}}
onClose={() => {}}
/>
Expand All @@ -115,18 +89,7 @@ describe('App', () => {
jest.useFakeTimers();
const closeMock = jest.fn();

render(
<App
config={{
anonKey: process.env.CONSOLE_ANON_KEY || '',
generateDepositAddress: async () => {
return { address: '0x000' };
},
theme: 'dark',
}}
onClose={closeMock}
/>
);
render(<App config={mockConfig} onClose={closeMock} />);

const closeButton = await screen.findByLabelText('Close');
expect(closeButton).toBeInTheDocument();
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/tests/useWeb3.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generateTestingUtils } from 'eth-testing';

import { mockConfig } from '~/jest/__mocks__/mockConfig';
import { act, fireEvent, render, screen } from '~/jest/test-utils';

import App from '../../App';
Expand All @@ -9,11 +10,10 @@ describe('useWeb3', () => {
render(
<App
config={{
anonKey: process.env.CONSOLE_ANON_KEY || '',
...mockConfig,
generateDepositAddress: async () => {
throw 'Error generating deposit address.';
},
theme: 'dark',
}}
onClose={() => {}}
/>
Expand Down
16 changes: 8 additions & 8 deletions src/hooks/useDepositAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { Context } from '../providers/Store';
export const useDepositAddress = () => {
const [state, dispatch, { generateDepositAddress }] = useContext(Context);

const getDepositAddress = async (
memoEnabled: boolean
): Promise<{ address: string; memo?: string }> => {
const getDepositAddress = async (): Promise<{
address: string;
memo?: string;
}> => {
try {
if (
state.depositAddress.status === 'success' &&
Expand All @@ -16,17 +17,16 @@ export const useDepositAddress = () => {
return state.depositAddress.data;
}
dispatch({ type: 'GENERATE_DEPOSIT_ADDRESS_LOADING' });
const { address, memo } = await generateDepositAddress(
const { address } = await generateDepositAddress(
state.asset?.symbol as string,
state.network?.symbol as string,
memoEnabled
state.network?.symbol as string
);
dispatch({
payload: { address, memo },
payload: { address },
type: 'GENERATE_DEPOSIT_ADDRESS_SUCCESS',
});

return { address, memo };
return { address };
} catch (e) {
dispatch({ type: 'GENERATE_DEPOSIT_ADDRESS_ERROR' });
throw new Error('Error generating a deposit address.');
Expand Down
5 changes: 1 addition & 4 deletions src/hooks/usePrebuildTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ export const usePrebuildTx = () => {

dispatch({ type: 'SET_PREBUILT_TX_LOADING' });
const { assetBalance, chainBalance } = await getBalance(assetContract);
const { address, memo } = await getDepositAddress(
state.method?.flags?.memo || false
);
const { address } = await getDepositAddress();
const tx = buildTx({
address,
amount,
assetContract,
decimals,
from: state.account.data,
memo,
});
let estimatedGas = BigNumber.from(0);
if (assetContract) {
Expand Down
29 changes: 16 additions & 13 deletions src/hooks/useWeb3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { useContext, useEffect, useState } from 'react';
import { isMobile } from 'react-device-detect';

import { CONSOLE_API_URL } from '../constants';
import { Context, Steps } from '../providers/Store';
import { Context } from '../providers/Store';
import { erc20Abi } from '../utils/abis/erc20';
import { toHex } from '../utils/toHex';
import { buildTx } from '../utils/transactions/evm';

export const useWeb3 = () => {
const [state, dispatch, { authorizeTransaction }] = useContext(Context);
const [state, _, { authorizeTransaction }] = useContext(Context);
const [providers, setProviders] = useState<{
[key in string]: boolean;
}>({});
Expand Down Expand Up @@ -129,14 +129,9 @@ export const useWeb3 = () => {
throw new Error('No account');
}

// @ts-ignore
if (!isMobile && !state.method?.value === 'isWalletConnect') {
dispatch({ type: 'SET_TRANSACTION_LOADING' });
}
let hash;

const decimals = state.asset?.decimals;
const memo = state.depositAddress.data?.memo;

if (!decimals) {
throw new Error('No decimals.');
Expand All @@ -152,7 +147,6 @@ export const useWeb3 = () => {
assetContract,
decimals,
from: state.account.data,
memo,
});

try {
Expand All @@ -170,14 +164,22 @@ export const useWeb3 = () => {
throw new Error('No transaction hash.');
}

dispatch({ type: 'SET_TRANSACTION_LOADING' });
dispatch({ payload: Steps.Result, type: 'SET_STEP' });
await state.provider?.data?.waitForTransaction(hash, 1);
return hash;
} catch (e: any) {
dispatch({ payload: e.message, type: 'SET_TRANSACTION_ERROR' });
throw e;
}
dispatch({ payload: hash, type: 'SET_TRANSACTION_SUCCESS' });
};

const waitForTransaction = async (hash: string, confirmations: number) => {
if (!state.provider?.data) {
throw new Error('No provider.');
}

const tx = await state.provider?.data?.waitForTransaction(
hash,
confirmations
);
return tx;
};

return {
Expand All @@ -188,5 +190,6 @@ export const useWeb3 = () => {
providers,
sendTransaction,
switchChain,
waitForTransaction,
};
};
60 changes: 58 additions & 2 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('Map3Sdk', () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { address: '0x0000000000000000000000000000000000000000' };
},
userId: 'test',
});
await act(async () => {
map3.open();
Expand All @@ -31,6 +32,7 @@ describe('Map3Sdk', () => {
return { address: '0x0000000000000000000000000000000000000000' };
},
theme: 'dark',
userId: 'test',
});
await act(async () => {
map3.open();
Expand All @@ -46,6 +48,7 @@ describe('Map3Sdk', () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { address: '0x0000000000000000000000000000000000000000' };
},
userId: 'test',
});
expect(initFn).not.toThrow();
});
Expand All @@ -57,6 +60,7 @@ describe('Map3Sdk', () => {
anonKey: 'test',
fiat: 'USD',
generateDepositAddress: async () => ({ address: '0x000000' }),
userId: 'test',
});
expect(initFn).not.toThrow();
expect(warnSpy).toBeCalledWith(
Expand All @@ -66,7 +70,7 @@ describe('Map3Sdk', () => {
it('should throw if no generateDepositAddress function is passed', () => {
// @ts-expect-error
const initFn = () => initMap3Sdk({});
expect(initFn).toThrow('generateDepositAddress is required');
expect(initFn).toThrow('generateDepositAddress is required.');
});
it('should throw if no anonKey is passed', () => {
const initFn = () =>
Expand All @@ -77,7 +81,19 @@ describe('Map3Sdk', () => {
return { address: '0x0000000000000000000000000000000000000000' };
},
});
expect(initFn).toThrow('anonKey is required');
expect(initFn).toThrow('anonKey is required.');
});
it('should throw if no userId is passed', () => {
const initFn = () =>
// @ts-expect-error
initMap3Sdk({
anonKey: 'test',
generateDepositAddress: async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { address: '0x0000000000000000000000000000000000000000' };
},
});
expect(initFn).toThrow('userId is required.');
});
it('should allow optional rainbowRoad config', () => {
const initFn = () =>
Expand All @@ -88,6 +104,46 @@ describe('Map3Sdk', () => {
return { address: '0x0000000000000000000000000000000000000000' };
},
rainbowRoad: true,
userId: 'test',
});
expect(initFn).not.toThrow();
});
it('should allow optional onClose', () => {
const initFn = () =>
initMap3Sdk({
anonKey: 'test',
generateDepositAddress: async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { address: '0x0000000000000000000000000000000000000000' };
},
onClose: () => {},
userId: 'test',
});
expect(initFn).not.toThrow();
});
it('should allow optional onFailure', () => {
const initFn = () =>
initMap3Sdk({
anonKey: 'test',
generateDepositAddress: async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { address: '0x0000000000000000000000000000000000000000' };
},
onFailure: () => {},
userId: 'test',
});
expect(initFn).not.toThrow();
});
it('should allow optional onSuccess', () => {
const initFn = () =>
initMap3Sdk({
anonKey: 'test',
generateDepositAddress: async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return { address: '0x0000000000000000000000000000000000000000' };
},
onSuccess: () => {},
userId: 'test',
});
expect(initFn).not.toThrow();
});
Expand Down
Loading

0 comments on commit 26370ed

Please sign in to comment.