Skip to content

Commit

Permalink
WT-2019 - migrate to bridge configs per chain (#1330)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrearampin authored Jan 9, 2024
1 parent 5f3b05b commit 86f2ca9
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 101 deletions.
2 changes: 1 addition & 1 deletion packages/checkout/sdk/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const config: Config = {
'@imtbl/blockchain-data': '<rootDir>../../blockchain-data/sdk/src',
'@imtbl/generated-clients': '<rootDir>../../internal/generated-clients/src'
},
testEnvironment: 'node',
testEnvironment: 'jsdom',
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ describe('allowListCheck', () => {
}],
};
bridgeConfig = {
tokens: [{
decimals: 18,
symbol: 'ETH',
name: 'Ethereum',
}],
[ChainId.SEPOLIA]: {
tokens: [{
decimals: 18,
symbol: 'ETH',
name: 'Ethereum',
}],
},
};
onRampConfig = {
[OnRampProvider.TRANSAK]: {
Expand Down Expand Up @@ -137,13 +139,13 @@ describe('allowListCheck', () => {
symbol: 'ETH',
decimals: 18,
}],
onRamp: {
[OnRampProvider.TRANSAK]: [{
onRamp: [
{
decimals: 18,
symbol: 'IMX',
name: 'IMX',
}],
},
},
],
});
});

Expand All @@ -157,7 +159,7 @@ describe('allowListCheck', () => {
symbol: 'ETH',
decimals: 18,
}],
onRamp: {},
onRamp: [],
swap: [],
});
});
Expand All @@ -168,7 +170,7 @@ describe('allowListCheck', () => {
const allowList = await allowListCheck(config, balances, availableRoutingOptions);
expect(allowList).toEqual({
bridge: [],
onRamp: {},
onRamp: [],
swap: [{
decimals: 18,
symbol: 'IMX',
Expand All @@ -183,13 +185,13 @@ describe('allowListCheck', () => {
const allowList = await allowListCheck(config, balances, availableRoutingOptions);
expect(allowList).toEqual({
bridge: [],
onRamp: {
[OnRampProvider.TRANSAK]: [{
onRamp: [
{
decimals: 18,
symbol: 'IMX',
name: 'IMX',
}],
},
},
],
swap: [],
});
});
Expand All @@ -200,7 +202,7 @@ describe('allowListCheck', () => {
const allowList = await allowListCheck(config, balances, availableRoutingOptions);
expect(allowList).toEqual({
bridge: [],
onRamp: {},
onRamp: [],
swap: [],
});
});
Expand Down Expand Up @@ -245,17 +247,19 @@ describe('allowListCheck', () => {
]);

bridgeConfig = {
tokens: [{
address: '0x0000000',
decimals: 18,
symbol: 'MEGA',
name: 'Mega',
[ChainId.SEPOLIA]: {
tokens: [{
address: '0x0000000',
decimals: 18,
symbol: 'MEGA',
name: 'Mega',
},
{
decimals: 18,
symbol: 'ETH',
name: 'Ethereum',
}],
},
{
decimals: 18,
symbol: 'ETH',
name: 'Ethereum',
}],
};

const result = await allowListCheckForBridge(config, balances, { bridge: true });
Expand Down Expand Up @@ -284,7 +288,9 @@ describe('allowListCheck', () => {

it('should return an empty array if bridge allowlist is empty', async () => {
bridgeConfig = {
tokens: [],
[ChainId.IMTBL_ZKEVM_TESTNET]: {
tokens: [],
},
};

const result = await allowListCheckForBridge(config, balances, { bridge: true });
Expand All @@ -293,12 +299,14 @@ describe('allowListCheck', () => {

it('should return an empty array if allowlist tokens have no balance', async () => {
bridgeConfig = {
tokens: [{
address: '0x0000000',
decimals: 18,
symbol: 'MEGA',
name: 'Mega',
}],
[ChainId.IMTBL_ZKEVM_TESTNET]: {
tokens: [{
address: '0x0000000',
decimals: 18,
symbol: 'MEGA',
name: 'Mega',
}],
},
};

const result = await allowListCheckForBridge(config, balances, { bridge: true });
Expand Down Expand Up @@ -353,25 +361,23 @@ describe('allowListCheck', () => {
describe('allowListCheckForOnRamp', () => {
it('should return onRamp allowlist', async () => {
const result = await allowListCheckForOnRamp(config, { onRamp: true });
expect(result).toEqual({
[OnRampProvider.TRANSAK]: [{
decimals: 18,
symbol: 'IMX',
name: 'IMX',
}],
});
expect(result).toEqual([{
decimals: 18,
symbol: 'IMX',
name: 'IMX',
}]);
});

it('should return an empty object if onRamp option is disabled', async () => {
it('should return an empty array if onRamp option is disabled', async () => {
const result = await allowListCheckForOnRamp(config, { onRamp: false });
expect(result).toEqual({});
expect(result).toEqual([]);
});

it('should return an empty object if no onRamp providers configured', async () => {
it('should return an empty array if no onRamp providers configured', async () => {
onRampConfig = {};

const result = await allowListCheckForOnRamp(config, { onRamp: true });
expect(result).toEqual({});
expect(result).toEqual([]);
});

it('should return an empty array if onRamp allowlist is empty', async () => {
Expand All @@ -384,9 +390,7 @@ describe('allowListCheck', () => {
};

const result = await allowListCheckForOnRamp(config, { onRamp: true });
expect(result).toEqual({
[OnRampProvider.TRANSAK]: [],
});
expect(result).toEqual([]);
});
});
});
27 changes: 11 additions & 16 deletions packages/checkout/sdk/src/smartCheckout/allowList/allowListCheck.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CheckoutConfiguration, getL1ChainId, getL2ChainId } from '../../config';
import { getTokenAllowList } from '../../tokens';
import {
BridgeConfig,
DexConfig, OnRampConfig,
AvailableRoutingOptions,
TokenInfo,
TokenFilterTypes,
} from '../../types';
import { TokenBalanceResult, TokenBalances } from '../routing/types';
import { OnRampTokensAllowList, RoutingTokensAllowList } from './types';
import { RoutingTokensAllowList } from './types';

const filterTokens = (allowedTokens: TokenInfo[], balances: TokenBalanceResult | undefined) => {
if (balances && balances.success) {
Expand All @@ -24,19 +24,13 @@ const filterTokens = (allowedTokens: TokenInfo[], balances: TokenBalanceResult |
export const allowListCheckForOnRamp = async (
config: CheckoutConfiguration,
availableRoutingOptions: AvailableRoutingOptions,
) : Promise<OnRampTokensAllowList> => {
) : Promise<TokenInfo[]> => {
if (availableRoutingOptions.onRamp) {
const onRampOptions = await config.remote.getConfig('onramp') as OnRampConfig;
const onRampAllowList: OnRampTokensAllowList = {};
Object.entries(onRampOptions)
.forEach(([onRampProvider, onRampProviderConfig]) => {
// Allowed list per onRamp provider
onRampAllowList[onRampProvider] = onRampProviderConfig.tokens ?? [];
});
return onRampAllowList;
const onRampOptions = (await getTokenAllowList(config, { type: TokenFilterTypes.ONRAMP }));
return onRampOptions.tokens;
}

return {};
return [];
};

export const allowListCheckForBridge = async (
Expand All @@ -45,8 +39,9 @@ export const allowListCheckForBridge = async (
availableRoutingOptions: AvailableRoutingOptions,
) : Promise<TokenInfo[]> => {
if (availableRoutingOptions.bridge) {
const allowedTokens = ((await config.remote.getConfig('bridge')) as BridgeConfig)?.tokens ?? [];
const balances = tokenBalances.get(getL1ChainId(config));
const chainId = getL1ChainId(config);
const allowedTokens = (await getTokenAllowList(config, { type: TokenFilterTypes.BRIDGE, chainId })).tokens;
const balances = tokenBalances.get(chainId);
return filterTokens(allowedTokens, balances);
}

Expand All @@ -59,7 +54,7 @@ export const allowListCheckForSwap = async (
availableRoutingOptions: AvailableRoutingOptions,
) : Promise<TokenInfo[]> => {
if (availableRoutingOptions.swap) {
const allowedTokens = ((await config.remote.getConfig('dex')) as DexConfig)?.tokens ?? [];
const allowedTokens = (await getTokenAllowList(config, { type: TokenFilterTypes.SWAP })).tokens;
const balances = tokenBalances.get(getL2ChainId(config));
return filterTokens(allowedTokens, balances);
}
Expand Down
4 changes: 1 addition & 3 deletions packages/checkout/sdk/src/smartCheckout/allowList/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { TokenInfo } from '../../types';

export type OnRampTokensAllowList = { [key: string]: TokenInfo[] };

export type RoutingTokensAllowList = {
'bridge'?: TokenInfo[],
'swap'?: TokenInfo[],
'onRamp'?: OnRampTokensAllowList,
'onRamp'?: TokenInfo[],
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ChainId,
FundingStepType,
ItemType,
OnRampProvider,
} from '../../../types';
import { allowListCheckForOnRamp } from '../../allowList';
import { onRampRoute } from './onRampRoute';
Expand All @@ -21,8 +20,8 @@ describe('onRampRoute', () => {
}, mockedHttpClient);

beforeEach(() => {
(allowListCheckForOnRamp as jest.Mock).mockResolvedValue({
[OnRampProvider.TRANSAK]: [
(allowListCheckForOnRamp as jest.Mock).mockResolvedValue(
[
{
address: '0x65AA7a21B0f3ce9B478aAC3408fE75b423939b1F',
name: 'Ethereum',
Expand All @@ -35,7 +34,7 @@ describe('onRampRoute', () => {
decimals: 18,
},
],
});
);
});

it('should return the onRamp route if the ERC20 balance requirement is in the allowlist', async () => {
Expand Down Expand Up @@ -189,9 +188,7 @@ describe('onRampRoute', () => {
},
} as BalanceERC20Requirement;

(allowListCheckForOnRamp as jest.Mock).mockResolvedValue({
[OnRampProvider.TRANSAK]: [],
});
(allowListCheckForOnRamp as jest.Mock).mockResolvedValue([]);

const route = await onRampRoute(
config,
Expand Down Expand Up @@ -228,9 +225,7 @@ describe('onRampRoute', () => {
},
} as BalanceERC721Requirement;

(allowListCheckForOnRamp as jest.Mock).mockResolvedValue({
[OnRampProvider.TRANSAK]: [],
});
(allowListCheckForOnRamp as jest.Mock).mockResolvedValue([]);

const route = await onRampRoute(
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export const onRampRoute = async (
const { required, current, delta } = balanceRequirement;

let hasAllowList = false;
const onRampProvidersAllowList = await allowListCheckForOnRamp(config, availableRoutingOptions);
Object.values(onRampProvidersAllowList).forEach((onRampAllowList) => {
if (onRampAllowList.length > 0 && !hasAllowList) {
hasAllowList = !!onRampAllowList.find((token) => token.address === required.token?.address);
const onRampAllowList = await allowListCheckForOnRamp(config, availableRoutingOptions);

onRampAllowList.forEach((token) => {
if (token.address === required.token?.address) {
hasAllowList = true;
}
});

Expand Down
28 changes: 15 additions & 13 deletions packages/checkout/sdk/src/tokens/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,28 @@ describe('token related functions', () => {
type: TokenFilterTypes.BRIDGE,
chainId: ChainId.IMTBL_ZKEVM_DEVNET,
result: [
{
address: '0x1',
decimals: 18,
name: 'token-aa-testnet',
symbol: 'AA',
},
{
address: '0x2',
decimals: 18,
name: 'token-bb-testnet',
symbol: 'BB',
},
{
address: '',
decimals: 18,
name: 'token-cc-testnet',
symbol: 'CC',
},
],
remoteConfigMockReturn,
remoteConfigMockReturn: {
...remoteConfigMockReturn,
getConfig: jest.fn().mockResolvedValue({
[ChainId.IMTBL_ZKEVM_DEVNET]: {
tokens: [
{
address: '',
decimals: 18,
name: 'token-cc-testnet',
symbol: 'CC',
},
],
},
}),
},
},
{
text: 'undefined SWAP tokens list',
Expand Down
Loading

0 comments on commit 86f2ca9

Please sign in to comment.