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

fix: stateful currency network for ETH and BTC #161

Merged
merged 1 commit into from
Mar 9, 2020
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
66 changes: 28 additions & 38 deletions packages/request-client.js/src/api/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,37 @@ import {
getSupportedERC20Tokens,
} from './currency/erc20';

// List of our supported cryptocurrencies
const currencyList = new Map([
[
'BTC',
{
// Simple function to get the currency from the value alone
const getCurrency = (currencyValue: string, network: string): RequestLogicTypes.ICurrency => {
// Check if it's a supported cryptocurrency
if (currencyValue === 'BTC') {
return {
type: RequestLogicTypes.CURRENCY.BTC,
value: 'BTC',
},
],

[
'ETH',
{
};
}
if (currencyValue === 'ETH') {
return {
type: RequestLogicTypes.CURRENCY.ETH,
value: 'ETH',
},
],
]);
};
}

// Check if it's an ERC20 token and return it if found
const erc20Currency = getErc20Currency(currencyValue, network);
if (erc20Currency) {
return erc20Currency;
}

// Check if it's one of ISO4217 currencies
if (isoCurrencyCodes.codes().includes(currencyValue)) {
return {
type: RequestLogicTypes.CURRENCY.ISO4217,
value: currencyValue,
};
}
throw new Error(`The currency ${currencyValue} is not supported`);
};

/**
* Returns a Currency object from a user-friendly currency string.
Expand All @@ -43,30 +56,7 @@ export function stringToCurrency(currencyString: string): RequestLogicTypes.ICur
// Split the currency string value and network (if available)
const [value, network] = currencyString.split('-');

// Simple function to get the currency from the value alone
const getCurrency = (): RequestLogicTypes.ICurrency => {
// Check if it's it's a cryptocurrency
if (currencyList.has(value)) {
return currencyList.get(value)!;
}

// Check if it's an ERC20 token and return it if found
const erc20Currency = getErc20Currency(value, network);
if (erc20Currency) {
return erc20Currency;
}

// Check if it's one of ISO4217 currencies
if (isoCurrencyCodes.codes().includes(value)) {
return {
type: RequestLogicTypes.CURRENCY.ISO4217,
value,
};
}
throw new Error(`The currency ${value} is not supported`);
};

const currency = getCurrency();
const currency = getCurrency(value, network);

// If a network was declared, add it to the currency object
if (network) {
Expand Down
16 changes: 16 additions & 0 deletions packages/request-client.js/test/api/currency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ describe('api/currency', () => {
it('throws for an unsupported currency', () => {
assert.throws(() => stringToCurrency('XXXXXXX'));
});

it('does not persist state between calls', () => {
assert.deepEqual(stringToCurrency('ETH'), {
type: RequestLogicTypes.CURRENCY.ETH,
value: 'ETH',
});
assert.deepEqual(stringToCurrency('ETH-rinkeby'), {
network: 'rinkeby',
type: RequestLogicTypes.CURRENCY.ETH,
value: 'ETH',
});
assert.deepEqual(stringToCurrency('ETH'), {
type: RequestLogicTypes.CURRENCY.ETH,
value: 'ETH',
});
});
});

describe('currencyToString', () => {
Expand Down