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: utils #82

Merged
merged 7 commits into from
Mar 22, 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
2 changes: 0 additions & 2 deletions .env.development
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
REACT_APP_AUTO_CONNECT=true
# 10 seconds
REACT_APP_POLL_BALANCE_INTERVAL=10000
# 10 seconds
REACT_APP_POLL_BLOCK_NUMBER_INTERVAL=10000
# goerli testnet
REACT_APP_SUPPORTED_CHAIN_IDS=5
2 changes: 0 additions & 2 deletions .env.production
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
REACT_APP_AUTO_CONNECT=true
# 1 minute
REACT_APP_POLL_BALANCE_INTERVAL=60000
# 30 seconds
REACT_APP_POLL_BLOCK_NUMBER_INTERVAL=30000
# mainnet
Expand Down
2 changes: 0 additions & 2 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
REACT_APP_AUTO_CONNECT=true
# 30 seconds
REACT_APP_POLL_BALANCE_INTERVAL=30000
# 20 seconds
REACT_APP_POLL_BLOCK_NUMBER_INTERVAL=20000
# goerli testnet
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/utils/blockchain/starknet.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {ChainType, TransactionHashPrefix} from '../../../enums';
import utils from '../../../utils';

describe('starknet', () => {
describe('getTransactionHash', () => {
it('should calc tx hash', () => {
const from_address = '0xcf98f0a8edc6a730e1ca6b64a2528c6be031cb12';
const to_address =
'1384622289134235426972866085149619554404298343372540338336104355150443775597';
const selector =
'1285101517810983806491589552491143496277809242732141897358598292095611420389';
const nonce = '55';
const payload = [
'17466514784613283928575916580398045172482824287888203092305238190565527099',
'52145000000000',
'0'
];

expect(
utils.blockchain.starknet.getTransactionHash(
TransactionHashPrefix.L1_HANDLER,
from_address,
to_address,
selector,
payload,
ChainType.GOERLI.id,
nonce
)
).toEqual('0x35ab0e4de971ac0736844ef36a05796dc41490c165373923c423f4b995983e8');
});
});
});
4 changes: 3 additions & 1 deletion src/__tests__/utils/date.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {get24Time, getDate} from '../../utils';
import utils from '../../utils';

const {get24Time, getDate} = utils.date;

describe('getDate', () => {
it('should get date', () => {
Expand Down
30 changes: 2 additions & 28 deletions src/__tests__/utils/hash.spec.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
import {ChainType, TransactionHashPrefix} from '../../enums';
import {hashEquals, txHash} from '../../utils';
import utils from '../../utils';

describe('txHash', () => {
it('should calc tx hash', () => {
const from_address = '0xcf98f0a8edc6a730e1ca6b64a2528c6be031cb12';
const to_address =
'1384622289134235426972866085149619554404298343372540338336104355150443775597';
const selector = '1285101517810983806491589552491143496277809242732141897358598292095611420389';
const nonce = '55';
const payload = [
'17466514784613283928575916580398045172482824287888203092305238190565527099',
'52145000000000',
'0'
];

expect(
txHash(
TransactionHashPrefix.L1_HANDLER,
from_address,
to_address,
selector,
payload,
ChainType.GOERLI.id,
nonce
)
).toEqual('0x35ab0e4de971ac0736844ef36a05796dc41490c165373923c423f4b995983e8');
});
});
const {hashEquals} = utils.hash;

describe('hashEquals', () => {
it('should compare hashes', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/utils/object.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {evaluate, findIndexById, getPropertyPath, toClasses} from '../../utils';
import utils from '../../utils';

const {evaluate, findIndexById, getPropertyPath, toClasses} = utils.object;

describe('getPropertyPath', () => {
const obj = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
import utils from '../../utils';

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

describe('UNIT_MAP', () => {
it('should init unit map', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {StorageManager} from '../../services';
import utils from '../../utils';

const {getItem, setItem} = utils.storage;

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

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

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

it('should get item from storage manager', () => {
StorageManager.setItem('testObject', testObject);
StorageManager.setItem('testArray', testArray);
StorageManager.setItem('testNumber', testNumber);
StorageManager.setItem('testString', testString);
expect(StorageManager.getItem('testObject')).toEqual(testObject);
expect(StorageManager.getItem('testArray')).toEqual(testArray);
expect(StorageManager.getItem('testNumber')).toEqual(testNumber);
expect(StorageManager.getItem('testString')).toEqual(testString);
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);
});

it('should backward compatible for saved arrays', () => {
const jsonArray = JSON.stringify(testArray);
localStorage.setItem('test', jsonArray);
expect(localStorage.getItem('test')).toEqual(jsonArray);
expect(StorageManager.getItem('test')).toEqual(testArray);
expect(getItem('test')).toEqual(testArray);
expect(localStorage.getItem('test')).toEqual(
'eyIwIjoiV3c9PSIsIjEiOiJldz09IiwiMiI6IklnPT0iLCIzIjoiWVE9PSIsIjQiOiJJZz09IiwiNSI6Ik9nPT0iLCI2IjoiTVE9PSIsIjciOiJmUT09IiwiOCI6IlhRPT0ifQ=='
);
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/utils/string.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {capitalize} from '../../utils';
import utils from '../../utils';

const {capitalize} = utils.string;

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

const {isEth} = utils.token;

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

const {formatBalance, shortenAddress} = utils.wallet;

describe('formatBalance', () => {
it('should format balance to 5 digits precision', () => {
Expand Down
25 changes: 12 additions & 13 deletions src/api/bridge.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {parseFromDecimals, parseToDecimals, parseToFelt, parseToUint256} from '../utils';
import {l1_callContract, l1_sendTransaction, l2_sendTransaction} from '../utils/contract';
import utils from '../utils';

export const deposit = async ({recipient, amount, decimals, contract, options, emitter}) => {
try {
return l1_sendTransaction(
return utils.blockchain.ethereum.sendTransaction(
contract,
'deposit',
[parseToDecimals(amount, decimals), recipient],
[utils.parser.parseToDecimals(amount, decimals), recipient],
options,
emitter
);
Expand All @@ -17,13 +16,13 @@ export const deposit = async ({recipient, amount, decimals, contract, options, e

export const depositEth = async ({recipient, amount, contract, options, emitter}) => {
try {
return l1_sendTransaction(
return utils.blockchain.ethereum.sendTransaction(
contract,
'deposit',
[recipient],
{
...options,
value: parseToDecimals(amount)
value: utils.parser.parseToDecimals(amount)
},
emitter
);
Expand All @@ -34,10 +33,10 @@ export const depositEth = async ({recipient, amount, contract, options, emitter}

export const withdraw = async ({recipient, amount, decimals, contract, emitter}) => {
try {
return l1_sendTransaction(
return utils.blockchain.ethereum.sendTransaction(
contract,
'withdraw',
[parseToDecimals(amount, decimals), recipient],
[utils.parser.parseToDecimals(amount, decimals), recipient],
{
from: recipient
},
Expand All @@ -50,18 +49,18 @@ export const withdraw = async ({recipient, amount, decimals, contract, emitter})

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

export const initiateWithdraw = async ({recipient, amount, decimals, contract}) => {
try {
return l2_sendTransaction(contract, 'initiate_withdraw', {
l1Recipient: parseToFelt(recipient),
amount: parseToUint256(amount, decimals)
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);
Expand Down
29 changes: 19 additions & 10 deletions src/api/erc20.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import {TransactionStatus} from '../enums';
import {web3} from '../libs';
import {parseFromDecimals, parseFromUint256} from '../utils';
import {l1_callContract, l1_sendTransaction, l2_callContract} from '../utils/contract';
import utils from '../utils';

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

export const allowance = async ({owner, spender, decimals, contract}) => {
try {
const allow = await l1_callContract(contract, 'allowance', [owner, spender]);
return parseFromDecimals(allow, decimals);
const allow = await utils.blockchain.ethereum.callContract(contract, 'allowance', [
owner,
spender
]);
return utils.parser.parseFromDecimals(allow, decimals);
} catch (ex) {
return Promise.reject(ex);
}
Expand All @@ -23,16 +30,18 @@ export const allowance = async ({owner, spender, decimals, contract}) => {
export const balanceOf = async ({account, decimals, contract}, isL1 = true) => {
try {
if (isL1) {
const balance = await l1_callContract(contract, 'balanceOf', [account]);
return parseFromDecimals(balance, decimals);
const balance = await utils.blockchain.ethereum.callContract(contract, 'balanceOf', [
account
]);
return utils.parser.parseFromDecimals(balance, decimals);
} else {
const {balance} = await l2_callContract(
const {balance} = await utils.blockchain.starknet.callContract(
contract,
'balanceOf',
[{account}],
TransactionStatus.PENDING.toLowerCase()
);
return parseFromUint256(balance, decimals);
return utils.parser.parseFromUint256(balance, decimals);
}
} catch (ex) {
return Promise.reject(ex);
Expand All @@ -42,7 +51,7 @@ export const balanceOf = async ({account, decimals, contract}, isL1 = true) => {
export const ethBalanceOf = async account => {
try {
const balance = await web3.eth.getBalance(account);
return parseFromDecimals(balance);
return utils.parser.parseFromDecimals(balance);
} catch (ex) {
return Promise.reject(ex);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Containers/Footer/Footer.strings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import {getString} from '../../../utils';
import utils from '../../../utils';

export const RIGHTS_TXT = getString('containers.footer.rights_txt');
export const RIGHTS_TXT = utils.getTranslation('containers.footer.rights_txt');
12 changes: 6 additions & 6 deletions src/components/Containers/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import {ReactComponent as StarkGateLogo} from '../../../assets/img/starkgate.svg';
import {useL1Wallet, useL2Wallet, useWallets} from '../../../providers/WalletsProvider';
import {toClasses} from '../../../utils';
import utils from '../../../utils';
import {useBridgeActions} from '../../Features/Bridge/Bridge.hooks';
import {useIsL1, useIsL2} from '../../Features/Transfer/Transfer.hooks';
import {WalletButton} from '../../UI';
Expand Down Expand Up @@ -32,17 +32,17 @@ export const Header = () => {
};

return (
<div className={toClasses(styles.header, 'row')}>
<div className={toClasses(styles.left, 'row')}>
<div className={toClasses(styles.logo, 'row')} onClick={onLogoClick}>
<div className={utils.object.toClasses(styles.header, 'row')}>
<div className={utils.object.toClasses(styles.left, 'row')}>
<div className={utils.object.toClasses(styles.logo, 'row')} onClick={onLogoClick}>
<StarkGateLogo />
</div>
{isConnected && (
<div className={toClasses(styles.chain, 'row')}>{CHAIN_TXT(chainName)}</div>
<div className={utils.object.toClasses(styles.chain, 'row')}>{CHAIN_TXT(chainName)}</div>
)}
</div>

<div className={toClasses(styles.right, 'row')}>
<div className={utils.object.toClasses(styles.right, 'row')}>
{isL1AccountConnected && (
<WalletButton
account={l1Account}
Expand Down
6 changes: 4 additions & 2 deletions src/components/Containers/Header/Header.strings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {capitalize, evaluate, getString} from '../../../utils';
import utils from '../../../utils';

export const CHAIN_TXT = chainName =>
capitalize(evaluate(getString('containers.header.chain_txt'), {chainName}));
utils.string.capitalize(
utils.object.evaluate(utils.getTranslation('containers.header.chain_txt'), {chainName})
);
Loading