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: export convention #180

Merged
merged 4 commits into from
May 10, 2022
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {
'object-shorthand': 'error',
'prefer-const': 'warn',
'prefer-template': 'warn',
'import/no-anonymous-default-export': 'off',
'prettier/prettier': 'error',
'react/react-in-jsx-scope': 'off',
'react-hooks/rules-of-hooks': 'error',
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/utils/date.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import utils from '../../utils';

const {get24Time, getDate, getMsFromHrs} = utils.date;
import {get24Time, getDate, getMsFromHrs} from '../../utils';

describe('getDate', () => {
it('should return date of the form DD/MM/YYYY from timestamp', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/utils/number.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import utils from '../../utils';

const {afterDecimal, isNegative, isZero} = utils.number;
import {afterDecimal, isNegative, isZero} from '../../utils';

describe('afterDecimal', () => {
it('should return number of decimals places', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/utils/object.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import utils from '../../utils';

const {evaluate, findIndexById, getPropertyPath, toClasses} = utils.object;
import {evaluate, findIndexById, getPropertyPath, toClasses} from '../../utils';

describe('getPropertyPath', () => {
const obj = {
Expand Down
6 changes: 2 additions & 4 deletions src/__tests__/utils/parser.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import utils from '../../utils';

const {
import {
parseFromDecimals,
parseFromFelt,
parseFromUint256,
parseToDecimals,
parseToUint256,
UNIT_MAP
} = utils.parser;
} from '../../utils';

describe('UNIT_MAP', () => {
it('should init unit map', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import {ChainType, TransactionHashPrefix} from '../../../enums';
import utils from '../../../utils';

const {getTransactionHash, hashEquals} = utils.blockchain.starknet;
import {ChainType, TransactionHashPrefix} from '../../enums';
import {getTransactionHash} from '../../utils';

describe('starknet blockchain utils', () => {
describe('hashEquals', () => {
it('should compare hashes', () => {
expect(hashEquals([1, 2])).toBeTruthy();
expect(hashEquals([1, 2, 3], [1, 2, 3])).toBeTruthy();
expect(hashEquals([1, 2, 3], [1, 2, 3], [1, 2, 3])).toBeTruthy();
expect(hashEquals([1, 2, 3], [1, 2, 3], [1])).toBeFalsy();
expect(hashEquals([1, 2, 3], [1, 2])).toBeFalsy();
});
});

describe('getTransactionHash', () => {
it('should calc tx hash from message params', () => {
const from_address = '0xc3511006C04EF1d78af4C8E0e74Ec18A6E64Ff9e';
Expand Down
30 changes: 14 additions & 16 deletions src/__tests__/utils/storage.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import utils from '../../utils';

const {getItem, setItem} = utils.storage;
import {getStorageItem, setStorageItem} from '../../utils';

class LocalStorageMock {
constructor() {
Expand Down Expand Up @@ -35,14 +33,14 @@ describe('StorageManager', () => {
});

it('should return null for empty key', () => {
expect(getItem('test')).toBeNull();
expect(getStorageItem('test')).toBeNull();
});

it('should get item hash from local storage', () => {
setItem('testObject', testObject);
setItem('testArray', testArray);
setItem('testNumber', testNumber);
setItem('testString', testString);
setStorageItem('testObject', testObject);
setStorageItem('testArray', testArray);
setStorageItem('testNumber', testNumber);
setStorageItem('testString', testString);
expect(localStorage.getItem('testObject')).toEqual(
'eyIwIjoiZXc9PSIsIjEiOiJJZz09IiwiMiI6IllRPT0iLCIzIjoiSWc9PSIsIjQiOiJPZz09IiwiNSI6Ik1RPT0iLCI2IjoiZlE9PSJ9'
);
Expand All @@ -56,13 +54,13 @@ describe('StorageManager', () => {
});

it('should get item from storage manager', () => {
setItem('testObject', testObject);
setItem('testArray', testArray);
setItem('testNumber', testNumber);
setItem('testString', testString);
expect(getItem('testObject')).toEqual(testObject);
expect(getItem('testArray')).toEqual(testArray);
expect(getItem('testNumber')).toEqual(testNumber);
expect(getItem('testString')).toEqual(testString);
setStorageItem('testObject', testObject);
setStorageItem('testArray', testArray);
setStorageItem('testNumber', testNumber);
setStorageItem('testString', testString);
expect(getStorageItem('testObject')).toEqual(testObject);
expect(getStorageItem('testArray')).toEqual(testArray);
expect(getStorageItem('testNumber')).toEqual(testNumber);
expect(getStorageItem('testString')).toEqual(testString);
});
});
4 changes: 1 addition & 3 deletions src/__tests__/utils/string.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import utils from '../../utils';

const {capitalize} = utils.string;
import {capitalize} from '../../utils';

describe('capitalize', () => {
it('should capitalize string', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/utils/token.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import utils from '../../utils';

const {isEth} = utils.token;
import {isEth} from '../../utils';

describe('isEth', () => {
it('should return true for eth symbol as string', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/utils/wallet.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import utils from '../../utils';

const {formatBalance, shortenAddress, shortenBalance} = utils.wallet;
import {formatBalance, shortenAddress, shortenBalance} from '../../utils';

describe('formatBalance', () => {
it('should format balance as string', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/analytics/track.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import splitbee from '@splitbee/web';

import utils from '../utils';
import {getLogger} from '../utils';

const logger = utils.logger.getLogger('Analytics');
const logger = getLogger('Analytics');

export const track = async (event, data) => {
logger.debug('Sending track event...', {event, data});
Expand Down
97 changes: 43 additions & 54 deletions src/api/bridge.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,69 @@
import utils from '../utils';
import {
sendL1Transaction,
callL1Contract,
sendL2Transaction,
parseFromDecimals,
parseToDecimals,
parseToFelt,
parseToUint256
} from '../utils';

export const deposit = async ({recipient, amount, decimals, contract, options, emitter}) => {
try {
return utils.blockchain.ethereum.sendTransaction(
contract,
'deposit',
[utils.parser.parseToDecimals(amount, decimals), recipient],
options,
emitter
);
} catch (ex) {
return Promise.reject(ex);
}
return sendL1Transaction(
contract,
'deposit',
[parseToDecimals(amount, decimals), recipient],
options,
emitter
);
};

export const depositEth = async ({recipient, amount, contract, options, emitter}) => {
try {
return utils.blockchain.ethereum.sendTransaction(
contract,
'deposit',
[recipient],
{
...options,
value: utils.parser.parseToDecimals(amount)
},
emitter
);
} catch (ex) {
return Promise.reject(ex);
}
return sendL1Transaction(
contract,
'deposit',
[recipient],
{
...options,
value: parseToDecimals(amount)
},
emitter
);
};

export const withdraw = async ({recipient, amount, decimals, contract, emitter}) => {
try {
return utils.blockchain.ethereum.sendTransaction(
contract,
'withdraw',
[utils.parser.parseToDecimals(amount, decimals), recipient],
{
from: recipient
},
emitter
);
} catch (ex) {
return Promise.reject(ex);
}
return sendL1Transaction(
contract,
'withdraw',
[parseToDecimals(amount, decimals), recipient],
{
from: recipient
},
emitter
);
};

export const maxDeposit = async ({decimals, contract}) => {
try {
const maxDeposit = await utils.blockchain.ethereum.callContract(contract, 'maxDeposit');
return utils.parser.parseFromDecimals(maxDeposit, decimals);
const maxDeposit = await callL1Contract(contract, 'maxDeposit');
return parseFromDecimals(maxDeposit, decimals);
} catch (ex) {
return Promise.reject(ex);
}
};

export const maxTotalBalance = async ({decimals, contract}) => {
try {
const maxTotalBalance = await utils.blockchain.ethereum.callContract(
contract,
'maxTotalBalance'
);
return utils.parser.parseFromDecimals(maxTotalBalance, decimals);
const maxTotalBalance = await callL1Contract(contract, 'maxTotalBalance');
return parseFromDecimals(maxTotalBalance, decimals);
} catch (ex) {
return Promise.reject(ex);
}
};

export const initiateWithdraw = async ({recipient, amount, decimals, contract}) => {
try {
return utils.blockchain.starknet.sendTransaction(contract, 'initiate_withdraw', {
l1Recipient: utils.parser.parseToFelt(recipient),
amount: utils.parser.parseToUint256(amount, decimals)
});
} catch (ex) {
return Promise.reject(ex);
}
return sendL2Transaction(contract, 'initiate_withdraw', {
l1Recipient: parseToFelt(recipient),
amount: parseToUint256(amount, decimals)
});
};
45 changes: 17 additions & 28 deletions src/api/erc20.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import {TransactionStatus} from '../enums';
import {web3} from '../libs';
import utils from '../utils';
import {
sendL1Transaction,
callL1Contract,
callL2Contract,
parseFromDecimals,
parseFromUint256
} from '../utils';

export const approve = async ({spender, value, contract, options}) => {
try {
return await utils.blockchain.ethereum.sendTransaction(
contract,
'approve',
[spender, value],
options
);
} catch (ex) {
return Promise.reject(ex);
}
return sendL1Transaction(contract, 'approve', [spender, value], options);
};

export const allowance = async ({owner, spender, decimals, contract}) => {
try {
const allow = await utils.blockchain.ethereum.callContract(contract, 'allowance', [
owner,
spender
]);
return utils.parser.parseFromDecimals(allow, decimals);
const allow = await callL1Contract(contract, 'allowance', [owner, spender]);
return parseFromDecimals(allow, decimals);
} catch (ex) {
return Promise.reject(ex);
}
Expand All @@ -30,18 +24,13 @@ export const allowance = async ({owner, spender, decimals, contract}) => {
export const balanceOf = async ({account, decimals, contract}, isL1 = true) => {
try {
if (isL1) {
const balance = await utils.blockchain.ethereum.callContract(contract, 'balanceOf', [
account
]);
return utils.parser.parseFromDecimals(balance, decimals);
const balance = await callL1Contract(contract, 'balanceOf', [account]);
return parseFromDecimals(balance, decimals);
} else {
const {balance} = await utils.blockchain.starknet.callContract(
contract,
'balanceOf',
account,
{blockIdentifier: TransactionStatus.PENDING.toLowerCase()}
);
return utils.parser.parseFromUint256(balance, decimals);
const {balance} = await callL2Contract(contract, 'balanceOf', account, {
blockIdentifier: TransactionStatus.PENDING.toLowerCase()
});
return parseFromUint256(balance, decimals);
}
} catch (ex) {
return Promise.reject(ex);
Expand All @@ -51,7 +40,7 @@ export const balanceOf = async ({account, decimals, contract}, isL1 = true) => {
export const ethBalanceOf = async account => {
try {
const balance = await web3.eth.getBalance(account);
return utils.parser.parseFromDecimals(balance);
return parseFromDecimals(balance);
} catch (ex) {
return Promise.reject(ex);
}
Expand Down
14 changes: 7 additions & 7 deletions src/components/Containers/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {useLogin} from '../../../providers/AppProvider';
import {useMenu} from '../../../providers/MenuProvider';
import {useIsL1, useIsL2} from '../../../providers/TransferProvider';
import {useL1Wallet, useL2Wallet} from '../../../providers/WalletsProvider';
import utils from '../../../utils';
import {openInNewTab, toClasses} from '../../../utils';
import {Tab, WalletButton} from '../../UI';
import styles from './Header.module.scss';

Expand Down Expand Up @@ -61,20 +61,20 @@ export const Header = () => {

const onTabDiscordClick = () => {
trackDiscordClick();
utils.browser.openInNewTab(DISCORD_LINK_URL);
openInNewTab(DISCORD_LINK_URL);
};

return (
<div className={utils.object.toClasses(styles.header, styles[breakpoint.toLowerCase()], 'row')}>
<div className={utils.object.toClasses(styles.left, 'row')}>
<div className={utils.object.toClasses(styles.logo, 'row')} onClick={onLogoClick}>
<div className={toClasses(styles.header, styles[breakpoint.toLowerCase()], 'row')}>
<div className={toClasses(styles.left, 'row')}>
<div className={toClasses(styles.logo, 'row')} onClick={onLogoClick}>
<StarkGateLogo />
</div>
{supportedChainId === ChainType.L1.GOERLI && (
<div className={utils.object.toClasses(styles.chain, 'row')}>{chainTxt}</div>
<div className={toClasses(styles.chain, 'row')}>{chainTxt}</div>
)}
</div>
<div className={utils.object.toClasses(styles.right, 'row')}>
<div className={toClasses(styles.right, 'row')}>
<Tab color={colorDiscord} label={tabDiscordTxt} onClick={onTabDiscordClick} />
<Tab color={colorWhiteOp50} label={tabTermsTxt} onClick={onTabTermsClick} />
<Tab color={colorWhiteOp50} label={tabFaqTxt} onClick={onTabFaqClick} />
Expand Down
Loading