From 3b77ba3ba6450f9f299045c0abde0e5140c3fa7f Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Thu, 6 Jan 2022 13:13:37 +0200 Subject: [PATCH 01/12] feat --- src/assets/svg/tokens/dai.svg | 14 ---- src/assets/svg/tokens/frax.svg | 28 +++++++ src/assets/svg/tokens/hex.svg | 1 - src/assets/svg/tokens/mim.svg | 34 ++++++++ src/assets/svg/tokens/usdc.svg | 5 ++ src/assets/svg/tokens/usdt.svg | 1 + src/assets/svg/tokens/wbtc.svg | 1 + .../Features/Bridge/Bridge.module.scss | 1 - .../SelectToken/SelectToken.module.scss | 1 + .../addresses/contracts/contracts.ethereum.js | 5 -- .../addresses/tokens/tokens.ethereum.js | 81 ++++++++++++++++++- .../addresses/tokens/tokens.starknet.js | 81 +++++++++++++++++-- src/hooks/useContract.js | 9 +-- src/hooks/useEventListener.js | 10 +-- src/hooks/useTransfer.js | 13 +-- .../TokensProvider/tokens-reducer.js | 14 +--- 16 files changed, 232 insertions(+), 67 deletions(-) delete mode 100644 src/assets/svg/tokens/dai.svg create mode 100644 src/assets/svg/tokens/frax.svg delete mode 100644 src/assets/svg/tokens/hex.svg create mode 100644 src/assets/svg/tokens/mim.svg create mode 100644 src/assets/svg/tokens/usdc.svg create mode 100644 src/assets/svg/tokens/usdt.svg create mode 100644 src/assets/svg/tokens/wbtc.svg diff --git a/src/assets/svg/tokens/dai.svg b/src/assets/svg/tokens/dai.svg deleted file mode 100644 index cc501bc4..00000000 --- a/src/assets/svg/tokens/dai.svg +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - diff --git a/src/assets/svg/tokens/frax.svg b/src/assets/svg/tokens/frax.svg new file mode 100644 index 00000000..9f8e2f7b --- /dev/null +++ b/src/assets/svg/tokens/frax.svg @@ -0,0 +1,28 @@ + + + + + + + + + + + diff --git a/src/assets/svg/tokens/hex.svg b/src/assets/svg/tokens/hex.svg deleted file mode 100644 index 0e24299a..00000000 --- a/src/assets/svg/tokens/hex.svg +++ /dev/null @@ -1 +0,0 @@ -Asset 1 diff --git a/src/assets/svg/tokens/mim.svg b/src/assets/svg/tokens/mim.svg new file mode 100644 index 00000000..903e3052 --- /dev/null +++ b/src/assets/svg/tokens/mim.svg @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + diff --git a/src/assets/svg/tokens/usdc.svg b/src/assets/svg/tokens/usdc.svg new file mode 100644 index 00000000..5dfea926 --- /dev/null +++ b/src/assets/svg/tokens/usdc.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/src/assets/svg/tokens/usdt.svg b/src/assets/svg/tokens/usdt.svg new file mode 100644 index 00000000..e5308224 --- /dev/null +++ b/src/assets/svg/tokens/usdt.svg @@ -0,0 +1 @@ +tether-usdt-logo \ No newline at end of file diff --git a/src/assets/svg/tokens/wbtc.svg b/src/assets/svg/tokens/wbtc.svg new file mode 100644 index 00000000..676c786b --- /dev/null +++ b/src/assets/svg/tokens/wbtc.svg @@ -0,0 +1 @@ +wrapped-bitcoin-wbtc \ No newline at end of file diff --git a/src/components/Features/Bridge/Bridge.module.scss b/src/components/Features/Bridge/Bridge.module.scss index 1c96da1b..26649c54 100644 --- a/src/components/Features/Bridge/Bridge.module.scss +++ b/src/components/Features/Bridge/Bridge.module.scss @@ -5,7 +5,6 @@ $background-color: $--color-alpha; .bridge { display: flex; flex-direction: column; - overflow: hidden; background: $background-color; border-radius: 20px; padding: 20px; diff --git a/src/components/Features/SelectToken/SelectToken.module.scss b/src/components/Features/SelectToken/SelectToken.module.scss index 807c6383..c93b7b75 100644 --- a/src/components/Features/SelectToken/SelectToken.module.scss +++ b/src/components/Features/SelectToken/SelectToken.module.scss @@ -8,6 +8,7 @@ $network-color: $--color-beta; position: relative; display: flex; flex-direction: column; + max-height: 100%; .background { content: ''; diff --git a/src/config/addresses/contracts/contracts.ethereum.js b/src/config/addresses/contracts/contracts.ethereum.js index 2ffe300e..210e347a 100644 --- a/src/config/addresses/contracts/contracts.ethereum.js +++ b/src/config/addresses/contracts/contracts.ethereum.js @@ -1,10 +1,5 @@ import {ChainType} from '../../../enums'; -export const ETH_BRIDGE_CONTRACT_ADDRESS = { - [ChainType.MAIN.id]: '', - [ChainType.GOERLI.id]: '0x7419275fe04ab65c9c9FB8bCB21601A4A34BF1b9' -}; - export const MESSAGING_CONTRACT_ADDRESS = { [ChainType.MAIN.id]: '', [ChainType.GOERLI.id]: '0xde29d060D45901Fb19ED6C6e959EB22d8626708e' diff --git a/src/config/addresses/tokens/tokens.ethereum.js b/src/config/addresses/tokens/tokens.ethereum.js index 2b227d4f..d0e088de 100644 --- a/src/config/addresses/tokens/tokens.ethereum.js +++ b/src/config/addresses/tokens/tokens.ethereum.js @@ -1,7 +1,19 @@ -import {ChainType} from '../../../enums'; +import {ChainType, NetworkType} from '../../../enums'; export const EthereumTokens = [ { + name: NetworkType.ETHEREUM.tokenName, + symbol: NetworkType.ETHEREUM.symbol, + decimals: 18, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0xcf98f0A8edC6a730E1CA6B64a2528c6bE031Cb12' + } + }, + { + name: 'Mock ERC20 Token', + symbol: 'TEST', + decimals: 18, bridgeAddress: { [ChainType.MAIN.id]: '', [ChainType.GOERLI.id]: '0x5542fd7dc64008978B50Ccb56274a00DEF2B57FE' @@ -9,8 +21,71 @@ export const EthereumTokens = [ tokenAddress: { [ChainType.MAIN.id]: '', [ChainType.GOERLI.id]: '0xe4C2493C66e068D65Cf1C38a2d7bDa25d9f08980' + } + }, + { + name: 'Wrapped BTC', + symbol: 'WBTC', + decimals: 8, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0xf29aE3446Ce4688fCc792b232C21D1B9581E7baC' }, - name: 'Mock ERC20 Token', - symbol: 'TEST' + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0xC04B0d3107736C32e19F1c62b2aF67BE61d63a05' + } + }, + { + name: 'Goerli USD Coin', + symbol: 'USDC', + decimals: 6, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0xc175127A2AA180F48D47fC20e6ED581Bd9c369c7' + }, + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x2f3A40A3db8a7e3D09B0adfEfbCe4f6F81927557' + } + }, + { + name: 'Tether USD', + symbol: 'USDT', + decimals: 6, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0xA1f590F18b23EFece02804704E5006E91348C997' + }, + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x509Ee0d083DdF8AC028f2a56731412edD63223B9' + } + }, + { + name: 'Magic Internet Money', + symbol: 'MIM', + decimals: 18, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x865C602d591eB18e946e9edF73895F6e699EB118' + }, + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x86124F8CF3C4AB702cEF895483067e46CE8764f9' + } + }, + { + name: 'Frax', + symbol: 'FRAX', + decimals: 18, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x9818b35b62B1B394614f97b5e8BC158907E4eb1C' + }, + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0xBd97097dF545cc9734b96A5f3C5b1752C9179649' + } } ]; diff --git a/src/config/addresses/tokens/tokens.starknet.js b/src/config/addresses/tokens/tokens.starknet.js index e4fd0256..9edc2303 100644 --- a/src/config/addresses/tokens/tokens.starknet.js +++ b/src/config/addresses/tokens/tokens.starknet.js @@ -2,18 +2,22 @@ import {ChainType, NetworkType} from '../../../enums'; export const StarknetTokens = [ { + name: NetworkType.ETHEREUM.tokenName, + symbol: NetworkType.ETHEREUM.symbol, + decimals: 18, bridgeAddress: { [ChainType.MAIN.id]: '', - [ChainType.GOERLI.id]: '0x012291a5bcd1b08e237047277a6055aec5a33d0e15610f869d517575ca983a82' + [ChainType.GOERLI.id]: '0x30fab1f1cc35d6ab0a992240528e122bc46c196eebfed9d21a5f800f72f066d' }, tokenAddress: { [ChainType.MAIN.id]: '', - [ChainType.GOERLI.id]: '0x03188903406daaaedd123598a8bd1f5dbec34720089037f4bf1473e51857e190' - }, - name: NetworkType.ETHEREUM.tokenName, - symbol: NetworkType.ETHEREUM.symbol + [ChainType.GOERLI.id]: '0x2dd93e385742984bf2fc887cd5d8b5ec6917d80af09cf7a00a63710ad51ba53' + } }, { + name: 'Mock ERC20 Token', + symbol: 'TEST', + decimals: 18, bridgeAddress: { [ChainType.MAIN.id]: '', [ChainType.GOERLI.id]: '0x0061f462a8a2ea511189e4ec73d115ef65fad071e643e99644818249604a525d' @@ -21,8 +25,71 @@ export const StarknetTokens = [ tokenAddress: { [ChainType.MAIN.id]: '', [ChainType.GOERLI.id]: '0x021eb73bd66eb18c37ec7b1e2567bc3fa8ba9ac0d391b53745a5f9d483217595' + } + }, + { + name: 'Wrapped BTC', + symbol: 'WBTC', + decimals: 8, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x72eeb90833bae233a9585f2fa9afc99c187f0a3a82693becd6a4d700b37fc6b' }, - name: 'Mock ERC20 Token', - symbol: 'TEST' + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x12d537dc323c439dc65c976fad242d5610d27cfb5f31689a0a319b8be7f3d56' + } + }, + { + name: 'Goerli USD Coin', + symbol: 'USDC', + decimals: 6, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x28248893ca3bab78fe7b0a053d3ddc450c291f3572e175699dbd1f7076dc800' + }, + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x77367abf77245c0e74728e4f9dc7c944b2bc5c1bdb6902575ff97aea7653842' + } + }, + { + name: 'Tether USD', + symbol: 'USDT', + decimals: 6, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x71d54658ca3c6ccd84ff958adb7498b2e71ba008e29b643983221ed2bd71b69' + }, + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x386e8d061177f19b3b485c20e31137e6f6bc497cc635ccdfcab96fadf5add6a' + } + }, + { + name: 'Magic Internet Money', + symbol: 'MIM', + decimals: 18, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x341747554ec54280d2e92eab57295055955fd1e8ec2292910163ca60c85e74f' + }, + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x1dd62766ff2a0d3b996857e6a04490c74649386dc52dc4b6153b9acf5f4cae3' + } + }, + { + name: 'Frax', + symbol: 'FRAX', + decimals: 18, + bridgeAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x254e681ffbf99c79e9e56aa38b2212914cadafa97d6c4f470589b1d43932a5f' + }, + tokenAddress: { + [ChainType.MAIN.id]: '', + [ChainType.GOERLI.id]: '0x1a53260bb912e49d668ae4ea8c4b5c1ebf1441f2acc1fd7fde74836738ed8da' + } } ]; diff --git a/src/hooks/useContract.js b/src/hooks/useContract.js index 98845d90..ae9af226 100644 --- a/src/hooks/useContract.js +++ b/src/hooks/useContract.js @@ -1,9 +1,9 @@ import {useCallback, useMemo} from 'react'; -import {ERC20_ABI, ERC20_BRIDGE_ABI, ETH_BRIDGE_ABI, MESSAGING_ABI} from '../abis/ethereum'; +import {ERC20_ABI, ERC20_BRIDGE_ABI, MESSAGING_ABI} from '../abis/ethereum'; import {STARKNET_BRIDGE_ABI, STARKNET_ERC20_ABI} from '../abis/starknet'; import {useTransferData} from '../components/Features/Transfer/Transfer.hooks'; -import {ETH_BRIDGE_CONTRACT_ADDRESS, MESSAGING_CONTRACT_ADDRESS} from '../config/addresses'; +import {MESSAGING_CONTRACT_ADDRESS} from '../config/addresses'; import {useWallets} from '../providers/WalletsProvider'; import {eth_getContract, starknet_getContract} from '../utils/contract'; @@ -79,11 +79,6 @@ export const useEthereumTokenContracts = () => { return useCallback(tokensAddresses => getContracts(tokensAddresses), [getContracts]); }; -export const useEthBridgeContract = () => { - const getContract = useContract(ETH_BRIDGE_ABI); - return useMemo(() => getContract(ETH_BRIDGE_CONTRACT_ADDRESS), [getContract]); -}; - export const useMessagingContract = () => { const getContract = useContract(MESSAGING_ABI); return useMemo(() => getContract(MESSAGING_CONTRACT_ADDRESS), [getContract]); diff --git a/src/hooks/useEventListener.js b/src/hooks/useEventListener.js index 70e54216..ef4eff52 100644 --- a/src/hooks/useEventListener.js +++ b/src/hooks/useEventListener.js @@ -4,8 +4,8 @@ import {stark} from 'starknet'; import {useSelectedToken} from '../components/Features/Transfer/Transfer.hooks'; import {useEthereumToken, useStarknetToken} from '../providers/TokensProvider'; import {useEthereumWallet, useStarknetWallet} from '../providers/WalletsProvider'; -import {isEth, txHash} from '../utils'; -import {useEthBridgeContract, useMessagingContract, useTokenBridgeContract} from './useContract'; +import {txHash} from '../utils'; +import {useMessagingContract, useTokenBridgeContract} from './useContract'; import {useLogger} from './useLogger'; const HOOK_MODULE = 'useEventListener'; @@ -53,7 +53,6 @@ export const useLogMessageToL2Listener = () => { export const useLogDepositListener = () => { const logger = useLogger(`${HOOK_MODULE}:useLogDepositListener`); const selectedToken = useSelectedToken(); - const ethBridgeContract = useEthBridgeContract(); const getTokenBridgeContract = useTokenBridgeContract(); const addEventListener = useEventListener(); const {account: ethereumAccount} = useEthereumWallet(); @@ -61,8 +60,8 @@ export const useLogDepositListener = () => { return useCallback(async () => { logger.log('Registering to LogDeposit event'); - const {symbol, bridgeAddress} = selectedToken; - const contract = isEth(symbol) ? ethBridgeContract : getTokenBridgeContract(bridgeAddress); + const {bridgeAddress} = selectedToken; + const contract = getTokenBridgeContract(bridgeAddress); try { const event = await addEventListener(contract, 'LogDeposit', { filter: { @@ -78,7 +77,6 @@ export const useLogDepositListener = () => { } }, [ addEventListener, - ethBridgeContract, ethereumAccount, starknetAccount, getTokenBridgeContract, diff --git a/src/hooks/useTransfer.js b/src/hooks/useTransfer.js index 121029a9..68d33a7b 100644 --- a/src/hooks/useTransfer.js +++ b/src/hooks/useTransfer.js @@ -17,7 +17,6 @@ import {useEthereumWallet, useStarknetWallet} from '../providers/WalletsProvider import {isEth} from '../utils'; import {starknet_waitForTransaction} from '../utils/contract'; import { - useEthBridgeContract, useEthereumTokenBridgeContract, useTokenBridgeContract, useTokenContract @@ -34,7 +33,6 @@ export const useTransferToL2 = () => { const {account: starknetAccount} = useStarknetWallet(); const {handleProgress, handleData, handleError} = useTransfer(); const selectedToken = useSelectedToken(); - const ethBridgeContract = useEthBridgeContract(); const getTokenContract = useTokenContract(); const getTokenBridgeContract = useTokenBridgeContract(); const progressOptions = useTransferProgress(); @@ -47,9 +45,7 @@ export const useTransferToL2 = () => { logger.log('TransferToL2 called'); const {symbol, tokenAddress, bridgeAddress, name} = selectedToken; const isEthToken = isEth(symbol); - const bridgeContract = isEthToken - ? ethBridgeContract - : getTokenBridgeContract(bridgeAddress); + const bridgeContract = getTokenBridgeContract(bridgeAddress); const depositHandler = isEthToken ? depositEth : deposit; logger.log('Prepared contract and handler', {isEthToken, bridgeContract, depositHandler}); if (!isEthToken) { @@ -108,7 +104,6 @@ export const useTransferToL2 = () => { addLogDepositListener, addLogMessageToL2Listener, chainId, - ethBridgeContract, ethereumAccount, ethereumConfig, getTokenBridgeContract, @@ -193,7 +188,6 @@ export const useCompleteTransferToL1 = () => { const {handleProgress, handleData, handleError} = useTransfer(); const progressOptions = useTransferProgress(); const getEthereumToken = useEthereumToken(); - const ethBridgeContract = useEthBridgeContract(); const getEthereumTokenBridgeContract = useEthereumTokenBridgeContract(); return useCallback( @@ -202,9 +196,7 @@ export const useCompleteTransferToL1 = () => { logger.log('CompleteTransferToL1 called'); const {symbol, amount} = transfer; const ethereumToken = getEthereumToken(symbol); - const tokenBridgeContract = isEth(symbol) - ? ethBridgeContract - : getEthereumTokenBridgeContract(ethereumToken.bridgeAddress); + const tokenBridgeContract = getEthereumTokenBridgeContract(ethereumToken.bridgeAddress); logger.log('Prepared token and bridge contract', {ethereumToken, tokenBridgeContract}); handleProgress(progressOptions.waitForConfirm(ethereumConfig.name)); logger.log('Calling withdraw'); @@ -227,7 +219,6 @@ export const useCompleteTransferToL1 = () => { } }, [ - ethBridgeContract, ethereumAccount, ethereumConfig, getEthereumToken, diff --git a/src/providers/TokensProvider/tokens-reducer.js b/src/providers/TokensProvider/tokens-reducer.js index bb226620..59c06943 100644 --- a/src/providers/TokensProvider/tokens-reducer.js +++ b/src/providers/TokensProvider/tokens-reducer.js @@ -1,21 +1,11 @@ -import {ETH_BRIDGE_CONTRACT_ADDRESS, EthereumTokens, StarknetTokens} from '../../config/addresses'; -import {NetworkType} from '../../enums'; +import {EthereumTokens, StarknetTokens} from '../../config/addresses'; export const actions = { UPDATE_TOKEN_STATE: 'Tokens/UPDATE_TOKEN_STATE' }; -const ethereumTokens = [ - { - symbol: NetworkType.ETHEREUM.symbol, - name: NetworkType.ETHEREUM.tokenName, - bridgeAddress: ETH_BRIDGE_CONTRACT_ADDRESS - }, - ...EthereumTokens -].map(t => ({...t, isEthereum: true})); - +const ethereumTokens = EthereumTokens.map(t => ({...t, isEthereum: true})); const starknetTokens = StarknetTokens.map(t => ({...t, isStarknet: true})); - export const initialState = [...ethereumTokens, ...starknetTokens]; export const reducer = (state, action) => { From 3b0e9d8218c09f8a2130880ef23c86bedab1fe6c Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Thu, 6 Jan 2022 14:14:34 +0200 Subject: [PATCH 02/12] fix api --- src/api/bridge.js | 30 ++++++++--------- src/api/erc20.js | 59 ++++++-------------------------- src/hooks/useContract.js | 21 +++++++++--- src/hooks/useTokenBalance.js | 5 +-- src/hooks/useTransfer.js | 65 +++++++++++++++++++----------------- 5 files changed, 78 insertions(+), 102 deletions(-) diff --git a/src/api/bridge.js b/src/api/bridge.js index 931870ed..0af89384 100644 --- a/src/api/bridge.js +++ b/src/api/bridge.js @@ -1,16 +1,13 @@ import {parseToDecimals, parseToFelt, parseToUint256} from '../utils'; import {eth_sendTransaction, starknet_sendTransaction} from '../utils/contract'; -import {decimals} from './erc20'; -export const deposit = async (recipient, amount, tokenBridgeContract, from, emitter) => { +export const deposit = async ({recipient, amount, decimals, contract, options, emitter}) => { try { return eth_sendTransaction( - tokenBridgeContract, + contract, 'deposit', - [parseToDecimals(amount), recipient], - { - from - }, + [parseToDecimals(amount, decimals), recipient], + options, emitter ); } catch (ex) { @@ -18,14 +15,14 @@ export const deposit = async (recipient, amount, tokenBridgeContract, from, emit } }; -export const depositEth = async (recipient, amount, ethBridgeContract, from, emitter) => { +export const depositEth = async ({recipient, amount, contract, options, emitter}) => { try { return eth_sendTransaction( - ethBridgeContract, + contract, 'deposit', [recipient], { - from, + ...options, value: parseToDecimals(amount) }, emitter @@ -35,12 +32,12 @@ export const depositEth = async (recipient, amount, ethBridgeContract, from, emi } }; -export const withdraw = async (recipient, amount, tokenBridgeContract, emitter) => { +export const withdraw = async ({recipient, amount, decimals, contract, emitter}) => { try { return eth_sendTransaction( - tokenBridgeContract, + contract, 'withdraw', - [parseToDecimals(amount), recipient], + [parseToDecimals(amount, decimals), recipient], { from: recipient }, @@ -51,12 +48,11 @@ export const withdraw = async (recipient, amount, tokenBridgeContract, emitter) } }; -export const initiateWithdraw = async (recipient, amount, tokenBridgeContract, tokenContract) => { +export const initiateWithdraw = async ({recipient, amount, decimals, contract}) => { try { - const dec = await decimals(tokenContract, false); - return starknet_sendTransaction(tokenBridgeContract, 'initiate_withdraw', { + return starknet_sendTransaction(contract, 'initiate_withdraw', { l1Recipient: parseToFelt(recipient), - amount: parseToUint256(amount, dec) + amount: parseToUint256(amount, decimals) }); } catch (ex) { return Promise.reject(ex); diff --git a/src/api/erc20.js b/src/api/erc20.js index dff788dd..09d30610 100644 --- a/src/api/erc20.js +++ b/src/api/erc20.js @@ -1,69 +1,32 @@ -import { - parseFromDecimals, - parseFromFelt, - parseFromUint256, - parseToFelt, - parseToUint256 -} from '../utils'; -import { - eth_callContract, - eth_sendTransaction, - starknet_callContract, - starknet_sendTransaction -} from '../utils/contract'; +import {parseFromDecimals, parseFromUint256} from '../utils'; +import {eth_callContract, eth_sendTransaction, starknet_callContract} from '../utils/contract'; import {web3} from '../web3'; -export const approve = async (spender, value, contract, from, isEthereum = true) => { +export const approve = async ({spender, value, contract, options}) => { try { - if (isEthereum) { - return await eth_sendTransaction(contract, 'approve', [spender, value], {from}); - } else { - const dec = await decimals(contract, false); - return await starknet_sendTransaction(contract, 'approve', { - spender: parseToFelt(spender), - amount: parseToUint256(value, dec) - }); - } + return await eth_sendTransaction(contract, 'approve', [spender, value], options); } catch (ex) { return Promise.reject(ex); } }; -export const allowance = async (owner, spender, contract, isEthereum = true) => { +export const allowance = async ({owner, spender, decimals, contract}) => { try { - if (isEthereum) { - const allow = await eth_callContract(contract, 'allowance', [owner, spender]); - return parseFromDecimals(allow); - } + const allow = await eth_callContract(contract, 'allowance', [owner, spender]); + return parseFromDecimals(allow, decimals); } catch (ex) { return Promise.reject(ex); } }; -export const balanceOf = async (account, contract, isEthereum = true) => { +export const balanceOf = async ({account, decimals, contract}, isEthereum = true) => { try { if (isEthereum) { const balance = await eth_callContract(contract, 'balanceOf', [account]); - return parseFromDecimals(balance); - } else { - const [{balance}, dec] = await Promise.all([ - starknet_callContract(contract, 'balanceOf', [{account}]), - decimals(contract, false) - ]); - return parseFromUint256(balance, dec); - } - } catch (ex) { - return Promise.reject(ex); - } -}; - -export const decimals = async (contract, isEthereum = true) => { - try { - if (isEthereum) { - return await eth_callContract(contract, 'decimals'); + return parseFromDecimals(balance, decimals); } else { - const {decimals} = await starknet_callContract(contract, 'decimals'); - return parseFromFelt(decimals); + const {balance} = await starknet_callContract(contract, 'balanceOf', [{account}]); + return parseFromUint256(balance, decimals); } } catch (ex) { return Promise.reject(ex); diff --git a/src/hooks/useContract.js b/src/hooks/useContract.js index ae9af226..15ab5f72 100644 --- a/src/hooks/useContract.js +++ b/src/hooks/useContract.js @@ -1,10 +1,12 @@ import {useCallback, useMemo} from 'react'; -import {ERC20_ABI, ERC20_BRIDGE_ABI, MESSAGING_ABI} from '../abis/ethereum'; +import {ERC20_ABI, ERC20_BRIDGE_ABI, ETH_BRIDGE_ABI, MESSAGING_ABI} from '../abis/ethereum'; import {STARKNET_BRIDGE_ABI, STARKNET_ERC20_ABI} from '../abis/starknet'; import {useTransferData} from '../components/Features/Transfer/Transfer.hooks'; -import {MESSAGING_CONTRACT_ADDRESS} from '../config/addresses'; -import {useWallets} from '../providers/WalletsProvider'; +import {EthereumTokens, MESSAGING_CONTRACT_ADDRESS} from '../config/addresses'; +import {NetworkType} from '../enums'; +import {useEthereumToken} from '../providers/TokensProvider'; +import {useEthereumWallet, useWallets} from '../providers/WalletsProvider'; import {eth_getContract, starknet_getContract} from '../utils/contract'; export const useContracts = (ABI, getContractHandler = eth_getContract) => { @@ -90,6 +92,15 @@ export const useStarknetTokenBridgeContract = () => { }; export const useEthereumTokenBridgeContract = () => { - const getContract = useContract(ERC20_BRIDGE_ABI); - return useCallback(bridgeAddress => getContract(bridgeAddress), [getContract]); + const getTokenBridgeContract = useContract(ERC20_BRIDGE_ABI); + const getEthBridgeContract = useContract(ETH_BRIDGE_ABI); + const ethToken = useEthereumToken()(NetworkType.ETHEREUM.symbol); + const {chainId} = useEthereumWallet(); + return useCallback( + bridgeAddress => + bridgeAddress[chainId] === ethToken.bridgeAddress[chainId] + ? getEthBridgeContract(bridgeAddress) + : getTokenBridgeContract(bridgeAddress), + [getTokenBridgeContract, getEthBridgeContract] + ); }; diff --git a/src/hooks/useTokenBalance.js b/src/hooks/useTokenBalance.js index c92f96f2..8dabc9d9 100644 --- a/src/hooks/useTokenBalance.js +++ b/src/hooks/useTokenBalance.js @@ -20,7 +20,8 @@ export const useTokenBalance = account => { export const useStarknetTokenBalance = account => { const getContract = useStarknetTokenContract(); return useCallback( - async tokenAddresses => await balanceOf(account, getContract(tokenAddresses), false), + async tokenAddresses => + await balanceOf({account, contract: getContract(tokenAddresses)}, false), [account, getContract] ); }; @@ -30,7 +31,7 @@ export const useEthereumTokenBalance = account => { return useCallback( async tokenAddresses => tokenAddresses - ? await balanceOf(account, getContract(tokenAddresses), true) + ? await balanceOf({account, contract: getContract(tokenAddresses)}, true) : await eth_ethBalanceOf(account), [account, getContract] ); diff --git a/src/hooks/useTransfer.js b/src/hooks/useTransfer.js index 68d33a7b..45e37daa 100644 --- a/src/hooks/useTransfer.js +++ b/src/hooks/useTransfer.js @@ -43,7 +43,7 @@ export const useTransferToL2 = () => { async amount => { try { logger.log('TransferToL2 called'); - const {symbol, tokenAddress, bridgeAddress, name} = selectedToken; + const {symbol, decimals, tokenAddress, bridgeAddress, name} = selectedToken; const isEthToken = isEth(symbol); const bridgeContract = getTokenBridgeContract(bridgeAddress); const depositHandler = isEthToken ? depositEth : deposit; @@ -52,33 +52,39 @@ export const useTransferToL2 = () => { logger.log('Token needs approval', {isEthToken}); const tokenContract = getTokenContract(tokenAddress); handleProgress(progressOptions.approval(symbol)); - const allow = await allowance(ethereumAccount, bridgeAddress[chainId], tokenContract); + const allow = await allowance({ + owner: ethereumAccount, + spender: bridgeAddress[chainId], + decimals, + contract: tokenContract + }); logger.log('Current allow value', {allow}); if (allow < amount) { logger.log('Allow value is smaller then amount, sending approve tx', {amount}); - await approve( - bridgeAddress[chainId], - constants.MASK_250, - tokenContract, - ethereumAccount - ); + await approve({ + spender: bridgeAddress[chainId], + value: constants.MASK_250, + contract: tokenContract, + options: {from: ethereumAccount} + }); } } handleProgress(progressOptions.waitForConfirm(ethereumConfig.name)); const logMessageToL2EventPromise = addLogMessageToL2Listener(); logger.log('Calling deposit'); - const depositPromise = await depositHandler( - starknetAccount, + const depositPromise = await depositHandler({ + recipient: starknetAccount, amount, - bridgeContract, - ethereumAccount, - (error, transactionHash) => { + decimals, + contract: bridgeContract, + options: {from: ethereumAccount}, + emitter: (error, transactionHash) => { if (!error) { logger.log('Tx hash received', {transactionHash}); handleProgress(progressOptions.deposit(amount, symbol)); } } - ); + }); const [{transactionHash: l1hash}, l2hash] = await Promise.all([ depositPromise, logMessageToL2EventPromise @@ -123,7 +129,6 @@ export const useTransferToL1 = () => { const {account: ethereumAccount} = useEthereumWallet(); const {account: starknetAccount, config: starknetConfig} = useStarknetWallet(); const selectedToken = useSelectedToken(); - const getTokenContract = useTokenContract(); const getTokenBridgeContract = useTokenBridgeContract(); const {handleProgress, handleData, handleError} = useTransfer(); const progressOptions = useTransferProgress(); @@ -132,18 +137,17 @@ export const useTransferToL1 = () => { async amount => { try { logger.log('TransferToL1 called'); - const {tokenAddress, bridgeAddress, name, symbol} = selectedToken; - const tokenContract = getTokenContract(tokenAddress); + const {decimals, bridgeAddress, name, symbol} = selectedToken; const bridgeContract = getTokenBridgeContract(bridgeAddress); - logger.log('Prepared contracts', {bridgeContract, tokenContract}); + logger.log('Prepared contract', {bridgeContract}); handleProgress(progressOptions.waitForConfirm(starknetConfig.name)); logger.log('Calling initiate withdraw'); - const {transaction_hash} = await initiateWithdraw( - ethereumAccount, + const {transaction_hash} = await initiateWithdraw({ + recipient: ethereumAccount, amount, - bridgeContract, - tokenContract - ); + decimals, + contract: bridgeContract + }); logger.log('Tx hash received', {transaction_hash}); handleProgress(progressOptions.initiateWithdraw(amount, symbol)); logger.log('Waiting for tx to be received on L2'); @@ -169,7 +173,6 @@ export const useTransferToL1 = () => { [ ethereumAccount, getTokenBridgeContract, - getTokenContract, handleData, handleError, handleProgress, @@ -196,21 +199,23 @@ export const useCompleteTransferToL1 = () => { logger.log('CompleteTransferToL1 called'); const {symbol, amount} = transfer; const ethereumToken = getEthereumToken(symbol); - const tokenBridgeContract = getEthereumTokenBridgeContract(ethereumToken.bridgeAddress); + const {bridgeAddress, decimals} = ethereumToken; + const tokenBridgeContract = getEthereumTokenBridgeContract(bridgeAddress); logger.log('Prepared token and bridge contract', {ethereumToken, tokenBridgeContract}); handleProgress(progressOptions.waitForConfirm(ethereumConfig.name)); logger.log('Calling withdraw'); - const {transactionHash} = await withdraw( - ethereumAccount, + const {transactionHash} = await withdraw({ + recipient: ethereumAccount, amount, - tokenBridgeContract, - (error, transactionHash) => { + decimals, + contract: tokenBridgeContract, + emitter: (error, transactionHash) => { if (!error) { logger.log('Tx hash received', {transactionHash}); handleProgress(progressOptions.withdraw(amount, symbol)); } } - ); + }); logger.log('Done', {transactionHash}); handleData({...transfer, eth_hash: transactionHash}); } catch (ex) { From 9141d5884a8ac5f17caf679526fd361cf03abd82 Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Thu, 6 Jan 2022 14:15:30 +0200 Subject: [PATCH 03/12] format --- src/hooks/useContract.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useContract.js b/src/hooks/useContract.js index 15ab5f72..c02d898d 100644 --- a/src/hooks/useContract.js +++ b/src/hooks/useContract.js @@ -3,7 +3,7 @@ import {useCallback, useMemo} from 'react'; import {ERC20_ABI, ERC20_BRIDGE_ABI, ETH_BRIDGE_ABI, MESSAGING_ABI} from '../abis/ethereum'; import {STARKNET_BRIDGE_ABI, STARKNET_ERC20_ABI} from '../abis/starknet'; import {useTransferData} from '../components/Features/Transfer/Transfer.hooks'; -import {EthereumTokens, MESSAGING_CONTRACT_ADDRESS} from '../config/addresses'; +import {MESSAGING_CONTRACT_ADDRESS} from '../config/addresses'; import {NetworkType} from '../enums'; import {useEthereumToken} from '../providers/TokensProvider'; import {useEthereumWallet, useWallets} from '../providers/WalletsProvider'; From ee5c754c85838d43e033dafd85492e911f63f95b Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Thu, 6 Jan 2022 14:47:59 +0200 Subject: [PATCH 04/12] Update tokens.ethereum.js --- src/config/addresses/tokens/tokens.ethereum.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/config/addresses/tokens/tokens.ethereum.js b/src/config/addresses/tokens/tokens.ethereum.js index d0e088de..1929fd01 100644 --- a/src/config/addresses/tokens/tokens.ethereum.js +++ b/src/config/addresses/tokens/tokens.ethereum.js @@ -10,19 +10,6 @@ export const EthereumTokens = [ [ChainType.GOERLI.id]: '0xcf98f0A8edC6a730E1CA6B64a2528c6bE031Cb12' } }, - { - name: 'Mock ERC20 Token', - symbol: 'TEST', - decimals: 18, - bridgeAddress: { - [ChainType.MAIN.id]: '', - [ChainType.GOERLI.id]: '0x5542fd7dc64008978B50Ccb56274a00DEF2B57FE' - }, - tokenAddress: { - [ChainType.MAIN.id]: '', - [ChainType.GOERLI.id]: '0xe4C2493C66e068D65Cf1C38a2d7bDa25d9f08980' - } - }, { name: 'Wrapped BTC', symbol: 'WBTC', From fe045818b305f76055530ceeab87ffa01c899a48 Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Thu, 6 Jan 2022 14:48:20 +0200 Subject: [PATCH 05/12] Update tokens.starknet.js --- src/config/addresses/tokens/tokens.starknet.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/config/addresses/tokens/tokens.starknet.js b/src/config/addresses/tokens/tokens.starknet.js index 9edc2303..ec6d8dfa 100644 --- a/src/config/addresses/tokens/tokens.starknet.js +++ b/src/config/addresses/tokens/tokens.starknet.js @@ -14,19 +14,6 @@ export const StarknetTokens = [ [ChainType.GOERLI.id]: '0x2dd93e385742984bf2fc887cd5d8b5ec6917d80af09cf7a00a63710ad51ba53' } }, - { - name: 'Mock ERC20 Token', - symbol: 'TEST', - decimals: 18, - bridgeAddress: { - [ChainType.MAIN.id]: '', - [ChainType.GOERLI.id]: '0x0061f462a8a2ea511189e4ec73d115ef65fad071e643e99644818249604a525d' - }, - tokenAddress: { - [ChainType.MAIN.id]: '', - [ChainType.GOERLI.id]: '0x021eb73bd66eb18c37ec7b1e2567bc3fa8ba9ac0d391b53745a5f9d483217595' - } - }, { name: 'Wrapped BTC', symbol: 'WBTC', From d235483adc62b677e7011e69392e1a4685784101 Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Thu, 6 Jan 2022 20:02:39 +0200 Subject: [PATCH 06/12] feat --- package.json | 2 +- src/providers/BlockHashProvider/BlockHashProvider.js | 3 ++- yarn.lock | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 82bbb183..caf3d65c 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "react-hot-toast": "^2.1.1", "react-redux": "^7.2.6", "react-scripts": "4.0.3", - "starknet": "^2.5.0", + "starknet": "^2.7.1", "use-deep-compare-effect": "^1.8.1", "use-wallet": "^0.13.4", "uuid": "^8.3.2", diff --git a/src/providers/BlockHashProvider/BlockHashProvider.js b/src/providers/BlockHashProvider/BlockHashProvider.js index 66f80a27..1f7d938f 100644 --- a/src/providers/BlockHashProvider/BlockHashProvider.js +++ b/src/providers/BlockHashProvider/BlockHashProvider.js @@ -2,6 +2,7 @@ import {getStarknet} from '@argent/get-starknet'; import PropTypes from 'prop-types'; import React, {useCallback, useEffect, useState} from 'react'; +import {TransactionStatus} from '../../enums'; import {useConfig} from '../../hooks'; import {BlockHashContext} from './block-hash-context'; @@ -11,7 +12,7 @@ export const BlockHashProvider = ({children}) => { const [blockHash, setBlockHash] = useState(); const fetchBlockHash = useCallback(async () => { - const {block_hash} = await provider.getBlock(); + const {block_hash} = await provider.getBlock(null, TransactionStatus.PENDING); setBlockHash(block_hash); }, [provider]); diff --git a/yarn.lock b/yarn.lock index c7b47687..d5656cdf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15239,10 +15239,10 @@ standard-version@^9.3.1: stringify-package "^1.0.1" yargs "^16.0.0" -starknet@^2.5.0: - version "2.5.0" - resolved "https://registry.yarnpkg.com/starknet/-/starknet-2.5.0.tgz#451e0519567419823214ac967b98c66630b97139" - integrity sha512-vW07z9JcHQVxk6US2nh1A+Z2ZBWlWviy32KTBm7MUzNewT8lgRRrIK9pPH6yWxpAPaRlD/u+7C0Vjp1Nbowtjg== +starknet@^2.7.1: + version "2.7.1" + resolved "https://registry.yarnpkg.com/starknet/-/starknet-2.7.1.tgz#722077230dc932093a9009587bb049945e035bfa" + integrity sha512-R1AjCLb3NR8ZJadw/6xYfJELwVsjLJ0lXopMpzoepr2Tv4amjKc6XlGaFirKJ154MC09Y8fN+qw9t3VycKZbgg== dependencies: axios "^0.23.0" bn.js "^5.2.0" From 83ed9a6eb4801cedef2d7645bac7a4726d5afadf Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Tue, 1 Feb 2022 12:17:52 +0200 Subject: [PATCH 07/12] update starknet --- package.json | 2 +- yarn.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 6102cdc8..0396b603 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "react-hot-toast": "^2.1.1", "react-redux": "^7.2.6", "react-scripts": "4.0.3", - "starknet": "^2.7.1", + "starknet": "^2.7.2", "use-deep-compare-effect": "^1.8.1", "use-wallet": "^0.13.4", "uuid": "^8.3.2", diff --git a/yarn.lock b/yarn.lock index d5656cdf..292a4333 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15239,10 +15239,10 @@ standard-version@^9.3.1: stringify-package "^1.0.1" yargs "^16.0.0" -starknet@^2.7.1: - version "2.7.1" - resolved "https://registry.yarnpkg.com/starknet/-/starknet-2.7.1.tgz#722077230dc932093a9009587bb049945e035bfa" - integrity sha512-R1AjCLb3NR8ZJadw/6xYfJELwVsjLJ0lXopMpzoepr2Tv4amjKc6XlGaFirKJ154MC09Y8fN+qw9t3VycKZbgg== +starknet@^2.7.2: + version "2.7.2" + resolved "https://registry.yarnpkg.com/starknet/-/starknet-2.7.2.tgz#927d7aaa226bd213420584e4397911f56f75e664" + integrity sha512-TLFrpQEOYx7o9wgF6fPOQwplxFu+CSaxy5Yidn7TW5B9PkMHaQRPmQdx508oxadeaJnye6wJTQ1zfYBrMrxnrg== dependencies: axios "^0.23.0" bn.js "^5.2.0" From f0cf95dfb13b41bd81c52a02df0b45c16b94ffa5 Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Wed, 2 Feb 2022 12:09:13 +0200 Subject: [PATCH 08/12] fixes --- src/api/erc20.js | 8 +++++++- .../BlockHashProvider/BlockHashProvider.js | 2 +- src/utils/contract.js | 18 ++++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/api/erc20.js b/src/api/erc20.js index c14d18f7..aa3b01c5 100644 --- a/src/api/erc20.js +++ b/src/api/erc20.js @@ -1,3 +1,4 @@ +import {TransactionStatus} from '../enums'; import {parseFromDecimals, parseFromUint256} from '../utils'; import {l1_callContract, l1_sendTransaction, l2_callContract} from '../utils/contract'; import {web3} from '../web3'; @@ -25,7 +26,12 @@ export const balanceOf = async ({account, decimals, contract}, isL1 = true) => { const balance = await l1_callContract(contract, 'balanceOf', [account]); return parseFromDecimals(balance, decimals); } else { - const {balance} = await l2_callContract(contract, 'balanceOf', [{account}]); + const {balance} = await l2_callContract( + contract, + 'balanceOf', + [{account}], + TransactionStatus.PENDING.toLowerCase() + ); return parseFromUint256(balance, decimals); } } catch (ex) { diff --git a/src/providers/BlockHashProvider/BlockHashProvider.js b/src/providers/BlockHashProvider/BlockHashProvider.js index 1f7d938f..6f73e86e 100644 --- a/src/providers/BlockHashProvider/BlockHashProvider.js +++ b/src/providers/BlockHashProvider/BlockHashProvider.js @@ -12,7 +12,7 @@ export const BlockHashProvider = ({children}) => { const [blockHash, setBlockHash] = useState(); const fetchBlockHash = useCallback(async () => { - const {block_hash} = await provider.getBlock(null, TransactionStatus.PENDING); + const {block_hash} = await provider.getBlock(); setBlockHash(block_hash); }, [provider]); diff --git a/src/utils/contract.js b/src/utils/contract.js index a412802f..d6d416e4 100644 --- a/src/utils/contract.js +++ b/src/utils/contract.js @@ -1,5 +1,6 @@ import {getStarknet} from '@argent/get-starknet'; import {compileCalldata, Contract, stark} from 'starknet'; +import {getSelectorFromName} from 'starknet/utils/stark'; import {TransactionConsumedStatuses} from '../enums'; import {getLogger} from '../services'; @@ -31,9 +32,22 @@ export const l1_sendTransaction = async ( export const l2_getContract = (address, ABI) => new Contract(ABI, address, getStarknet().provider); -export const l2_callContract = async (contract, method, args = []) => { +export const l2_callContract = async (contract, method, args = [], blockNumber = null) => { try { - return await contract.call(method, ...args); + const entrypointSelector = getSelectorFromName(method); + console.log({entrypointSelector}); + const calldata = compileCalldata(...args); + console.log({calldata}); + const response = await getStarknet().provider.callContract( + { + contract_address: contract.connectedTo, + calldata, + entry_point_selector: entrypointSelector + }, + null, + blockNumber + ); + return contract.parseResponse(method, response.result); } catch (ex) { return Promise.reject(ex); } From ea707e9d9617f2328c4e433a61c7de527e8fe6ed Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Sun, 13 Feb 2022 15:32:36 +0200 Subject: [PATCH 09/12] fix --- package.json | 2 +- .../BlockHashProvider/BlockHashProvider.js | 1 - src/utils/contract.js | 18 ++---------------- yarn.lock | 8 ++++---- 4 files changed, 7 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index c447966b..492e355b 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "react-hot-toast": "^2.1.1", "react-redux": "^7.2.6", "react-scripts": "4.0.3", - "starknet": "^2.7.2", + "starknet": "^3.0.0", "use-async-memo": "^1.2.3", "use-deep-compare-effect": "^1.8.1", "use-wallet": "^0.13.4", diff --git a/src/providers/BlockHashProvider/BlockHashProvider.js b/src/providers/BlockHashProvider/BlockHashProvider.js index 6f73e86e..66f80a27 100644 --- a/src/providers/BlockHashProvider/BlockHashProvider.js +++ b/src/providers/BlockHashProvider/BlockHashProvider.js @@ -2,7 +2,6 @@ import {getStarknet} from '@argent/get-starknet'; import PropTypes from 'prop-types'; import React, {useCallback, useEffect, useState} from 'react'; -import {TransactionStatus} from '../../enums'; import {useConfig} from '../../hooks'; import {BlockHashContext} from './block-hash-context'; diff --git a/src/utils/contract.js b/src/utils/contract.js index d6d416e4..16ccb0f8 100644 --- a/src/utils/contract.js +++ b/src/utils/contract.js @@ -1,6 +1,5 @@ import {getStarknet} from '@argent/get-starknet'; import {compileCalldata, Contract, stark} from 'starknet'; -import {getSelectorFromName} from 'starknet/utils/stark'; import {TransactionConsumedStatuses} from '../enums'; import {getLogger} from '../services'; @@ -32,22 +31,9 @@ export const l1_sendTransaction = async ( export const l2_getContract = (address, ABI) => new Contract(ABI, address, getStarknet().provider); -export const l2_callContract = async (contract, method, args = [], blockNumber = null) => { +export const l2_callContract = async (contract, method, args = [], blockIdentifier = null) => { try { - const entrypointSelector = getSelectorFromName(method); - console.log({entrypointSelector}); - const calldata = compileCalldata(...args); - console.log({calldata}); - const response = await getStarknet().provider.callContract( - { - contract_address: contract.connectedTo, - calldata, - entry_point_selector: entrypointSelector - }, - null, - blockNumber - ); - return contract.parseResponse(method, response.result); + return await contract.call(method, ...args, blockIdentifier); } catch (ex) { return Promise.reject(ex); } diff --git a/yarn.lock b/yarn.lock index d8534bb2..7bee7d56 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15390,10 +15390,10 @@ standard-version@^9.3.1: stringify-package "^1.0.1" yargs "^16.0.0" -starknet@^2.7.2: - version "2.7.2" - resolved "https://registry.yarnpkg.com/starknet/-/starknet-2.7.2.tgz#927d7aaa226bd213420584e4397911f56f75e664" - integrity sha512-TLFrpQEOYx7o9wgF6fPOQwplxFu+CSaxy5Yidn7TW5B9PkMHaQRPmQdx508oxadeaJnye6wJTQ1zfYBrMrxnrg== +starknet@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/starknet/-/starknet-3.0.0.tgz#9519bcd4730f7251286c76ca7ad37c5ef0500afc" + integrity sha512-cm51mRfF+N8RGfzsRnvpP9d+58K3snBI+zlnyKxhjAb7ZoHXd/JDRfSYYuzep+QqFQetYa3uvTPPRZ5uJIRoAg== dependencies: axios "^0.23.0" bn.js "^5.2.0" From 775bf2564e73233ba5324efdabcfedba8bcafc17 Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Mon, 14 Feb 2022 10:38:03 +0200 Subject: [PATCH 10/12] fix starknet reference --- package.json | 2 +- src/api/erc20.js | 2 +- src/hooks/useEventListener.js | 4 ++-- src/hooks/useTransfer.js | 4 ++-- src/libs/index.js | 2 ++ src/libs/starknet.js | 4 ++++ src/{ => libs}/web3.js | 0 .../BlockHashProvider/BlockHashProvider.js | 7 +++---- .../TransfersProvider/TransfersProvider.js | 4 ++-- src/providers/WalletsProvider/WalletsProvider.js | 9 +++------ src/providers/WalletsProvider/wallets-reducer.js | 1 - src/utils/contract.js | 13 +++++-------- src/utils/hash.js | 11 +++++------ src/utils/number.js | 15 +++++---------- yarn.lock | 8 ++++---- 15 files changed, 39 insertions(+), 47 deletions(-) create mode 100644 src/libs/index.js create mode 100644 src/libs/starknet.js rename src/{ => libs}/web3.js (100%) diff --git a/package.json b/package.json index 492e355b..77828837 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ ] }, "dependencies": { - "@argent/get-starknet": "^0.1.4", + "@argent/get-starknet": "^1.1.3", "@headlessui/react": "^1.4.2", "@metamask/onboarding": "^1.0.1", "@reduxjs/toolkit": "^1.6.2", diff --git a/src/api/erc20.js b/src/api/erc20.js index aa3b01c5..7a7ec91b 100644 --- a/src/api/erc20.js +++ b/src/api/erc20.js @@ -1,7 +1,7 @@ import {TransactionStatus} from '../enums'; +import {web3} from '../libs'; import {parseFromDecimals, parseFromUint256} from '../utils'; import {l1_callContract, l1_sendTransaction, l2_callContract} from '../utils/contract'; -import {web3} from '../web3'; export const approve = async ({spender, value, contract, options}) => { try { diff --git a/src/hooks/useEventListener.js b/src/hooks/useEventListener.js index fae509b0..542abd00 100644 --- a/src/hooks/useEventListener.js +++ b/src/hooks/useEventListener.js @@ -1,8 +1,8 @@ import {useCallback} from 'react'; -import {stark} from 'starknet'; import {useSelectedToken} from '../components/Features/Transfer/Transfer.hooks'; import {TransactionHashPrefix} from '../enums'; +import {starknet} from '../libs'; import {useL1Token, useL2Token} from '../providers/TokensProvider'; import {useL1Wallet, useL2Wallet} from '../providers/WalletsProvider'; import {txHash} from '../utils'; @@ -30,7 +30,7 @@ export const useLogMessageToL2Listener = () => { filter: { to_address: l2BridgeAddress, from_address: l1BridgeAddress, - selector: stark.getSelectorFromName('handle_deposit') + selector: starknet.stark.getSelectorFromName('handle_deposit') } }); logger.log('Event received', {event}); diff --git a/src/hooks/useTransfer.js b/src/hooks/useTransfer.js index 93a07320..632ed5ff 100644 --- a/src/hooks/useTransfer.js +++ b/src/hooks/useTransfer.js @@ -1,5 +1,4 @@ import {useCallback} from 'react'; -import {constants} from 'starknet'; import {deposit, depositEth, initiateWithdraw, withdraw} from '../api/bridge'; import {allowance, approve} from '../api/erc20'; @@ -11,6 +10,7 @@ import { } from '../components/Features/ModalProvider/ModalProvider.hooks'; import {useAmount, useSelectedToken} from '../components/Features/Transfer/Transfer.hooks'; import {ActionType, TransactionStatus} from '../enums'; +import {starknet} from '../libs'; import {useL1Token, useTokens} from '../providers/TokensProvider'; import {useTransfers} from '../providers/TransfersProvider'; import {useL1Wallet, useL2Wallet} from '../providers/WalletsProvider'; @@ -58,7 +58,7 @@ export const useTransferToL2 = () => { logger.log('Allow value is smaller then amount, sending approve tx', {amount}); await approve({ spender: bridgeAddress[chainId], - value: constants.MASK_250, + value: starknet.constants.MASK_250, contract: tokenContract, options: {from: l1Account} }); diff --git a/src/libs/index.js b/src/libs/index.js new file mode 100644 index 00000000..fbaa2845 --- /dev/null +++ b/src/libs/index.js @@ -0,0 +1,2 @@ +export * from './starknet'; +export * from './web3'; diff --git a/src/libs/starknet.js b/src/libs/starknet.js new file mode 100644 index 00000000..98040233 --- /dev/null +++ b/src/libs/starknet.js @@ -0,0 +1,4 @@ +import {getStarknet} from '@argent/get-starknet'; +import * as starknet from 'starknet'; + +export {starknet, getStarknet}; diff --git a/src/web3.js b/src/libs/web3.js similarity index 100% rename from src/web3.js rename to src/libs/web3.js diff --git a/src/providers/BlockHashProvider/BlockHashProvider.js b/src/providers/BlockHashProvider/BlockHashProvider.js index 66f80a27..5c48229b 100644 --- a/src/providers/BlockHashProvider/BlockHashProvider.js +++ b/src/providers/BlockHashProvider/BlockHashProvider.js @@ -1,19 +1,18 @@ -import {getStarknet} from '@argent/get-starknet'; import PropTypes from 'prop-types'; import React, {useCallback, useEffect, useState} from 'react'; import {useConfig} from '../../hooks'; +import {starknet} from '../../libs'; import {BlockHashContext} from './block-hash-context'; export const BlockHashProvider = ({children}) => { - const {provider} = getStarknet(); const {pollBlockNumberInterval} = useConfig(); const [blockHash, setBlockHash] = useState(); const fetchBlockHash = useCallback(async () => { - const {block_hash} = await provider.getBlock(); + const {block_hash} = await starknet.defaultProvider.getBlock(); setBlockHash(block_hash); - }, [provider]); + }, []); useEffect(() => { fetchBlockHash(); diff --git a/src/providers/TransfersProvider/TransfersProvider.js b/src/providers/TransfersProvider/TransfersProvider.js index 20bac27e..6af38379 100644 --- a/src/providers/TransfersProvider/TransfersProvider.js +++ b/src/providers/TransfersProvider/TransfersProvider.js @@ -1,4 +1,3 @@ -import {getStarknet} from '@argent/get-starknet'; import PropTypes from 'prop-types'; import React, {useEffect, useReducer} from 'react'; import useDeepCompareEffect from 'use-deep-compare-effect'; @@ -6,6 +5,7 @@ import useDeepCompareEffect from 'use-deep-compare-effect'; import {LOCAL_STORAGE_TRANSFERS_KEY} from '../../constants'; import {isCompleted} from '../../enums'; import {useLogger} from '../../hooks'; +import {starknet} from '../../libs'; import {StorageManager} from '../../services'; import {useBlockHash} from '../BlockHashProvider'; import {TransfersContext} from './transfers-context'; @@ -38,7 +38,7 @@ export const TransfersProvider = ({children}) => { } try { logger.log(`Checking tx status ${transfer.l2hash}`); - const newStatus = await getStarknet().provider.getTransactionStatus(transfer.l2hash); + const newStatus = await starknet.defaultProvider.getTransactionStatus(transfer.l2hash); if (transfer.status !== newStatus.tx_status) { logger.log( !transfer.status diff --git a/src/providers/WalletsProvider/WalletsProvider.js b/src/providers/WalletsProvider/WalletsProvider.js index 8428ddb8..28bdae4f 100644 --- a/src/providers/WalletsProvider/WalletsProvider.js +++ b/src/providers/WalletsProvider/WalletsProvider.js @@ -1,4 +1,3 @@ -import {getStarknet} from '@argent/get-starknet'; import PropTypes from 'prop-types'; import React, {useEffect, useReducer} from 'react'; import {useWallet} from 'use-wallet'; @@ -6,7 +5,7 @@ import {useWallet} from 'use-wallet'; import {useIsL1, useIsL2} from '../../components/Features/Transfer/Transfer.hooks'; import {WalletStatus} from '../../enums'; import {useConfig} from '../../hooks'; -import {web3} from '../../web3'; +import {getStarknet} from '../../libs'; import {WalletsContext} from './wallets-context'; import {actions, initialState, reducer} from './wallets-reducer'; @@ -82,8 +81,7 @@ export const WalletsProvider = ({children}) => { chainId, error: serialError, chainName: networkName, - isConnected: isConnected(), - library: web3 + isConnected: isConnected() }); }; @@ -103,8 +101,7 @@ export const WalletsProvider = ({children}) => { error, isConnected: isL2Connected, account: selectedAddress, - chainName: networkName, - library: getStarknet() + chainName: networkName }); } }; diff --git a/src/providers/WalletsProvider/wallets-reducer.js b/src/providers/WalletsProvider/wallets-reducer.js index 2d2902a5..4cc7883b 100644 --- a/src/providers/WalletsProvider/wallets-reducer.js +++ b/src/providers/WalletsProvider/wallets-reducer.js @@ -8,7 +8,6 @@ export const actions = { }; const initialWalletState = { - library: null, account: '', isConnected: false, status: WalletStatus.DISCONNECTED, diff --git a/src/utils/contract.js b/src/utils/contract.js index 16ccb0f8..288cdd44 100644 --- a/src/utils/contract.js +++ b/src/utils/contract.js @@ -1,9 +1,6 @@ -import {getStarknet} from '@argent/get-starknet'; -import {compileCalldata, Contract, stark} from 'starknet'; - import {TransactionConsumedStatuses} from '../enums'; +import {getStarknet, starknet, web3} from '../libs'; import {getLogger} from '../services'; -import {web3} from '../web3'; export const l1_getContract = (address, ABI) => new web3.eth.Contract(ABI, address); @@ -29,7 +26,7 @@ export const l1_sendTransaction = async ( } }; -export const l2_getContract = (address, ABI) => new Contract(ABI, address, getStarknet().provider); +export const l2_getContract = (address, ABI) => new starknet.Contract(ABI, address); export const l2_callContract = async (contract, method, args = [], blockIdentifier = null) => { try { @@ -41,8 +38,8 @@ export const l2_callContract = async (contract, method, args = [], blockIdentifi export const l2_sendTransaction = async (contract, method, args = {}) => { try { - const methodSelector = stark.getSelectorFromName(method); - const compiledCalldata = compileCalldata(args); + const methodSelector = starknet.stark.getSelectorFromName(method); + const compiledCalldata = starknet.compileCalldata(args); return getStarknet().signer.invokeFunction( contract.connectedTo, methodSelector, @@ -62,7 +59,7 @@ export const l2_waitForTransaction = async (hash, customStatus, retryInterval = const intervalId = setInterval(async () => { if (processing) return; logger.debug(`Checking transaction again`); - const statusPromise = getStarknet().provider.getTransactionStatus(hash); + const statusPromise = starknet.defaultProvider.getTransactionStatus(hash); processing = true; try { const {tx_status} = await statusPromise; diff --git a/src/utils/hash.js b/src/utils/hash.js index cd089620..f17bce92 100644 --- a/src/utils/hash.js +++ b/src/utils/hash.js @@ -1,6 +1,5 @@ -import {hash, number} from 'starknet'; - import {byChainId} from '../enums'; +import {starknet} from '../libs'; export const txHash = ( txHashPrefix, @@ -11,10 +10,10 @@ export const txHash = ( chainId, ...additionalData ) => { - const calldata = [number.hexToDecimalString(fromAddress), ...payload]; - const calldataHash = hash.hashCalldata(calldata); + const calldata = [starknet.number.hexToDecimalString(fromAddress), ...payload]; + const calldataHash = starknet.hash.hashCalldata(calldata); const {l2IdPrefix} = byChainId(chainId); - return hash.computeHashOnElements([ + return starknet.hash.computeHashOnElements([ txHashPrefix, toAddress, selector, @@ -26,6 +25,6 @@ export const txHash = ( export const hashEquals = (...data) => { return !!data.reduce((d1, d2) => - hash.computeHashOnElements(d1) === hash.computeHashOnElements(d2) ? d1 : '' + starknet.hash.computeHashOnElements(d1) === starknet.hash.computeHashOnElements(d2) ? d1 : '' ); }; diff --git a/src/utils/number.js b/src/utils/number.js index c3651f9c..652b2f03 100644 --- a/src/utils/number.js +++ b/src/utils/number.js @@ -1,9 +1,4 @@ -import {number, uint256} from 'starknet'; - -import {web3} from '../web3'; - -const {uint256ToBN, bnToUint256} = uint256; -const {toBN} = number; +import {web3, starknet} from '../libs'; const TEN = 10; const DEFAULT_DECIMALS = 18; @@ -22,16 +17,16 @@ export const parseFromDecimals = (value, decimals = DEFAULT_DECIMALS) => { }; export const parseFromFelt = value => { - return toBN(value).toNumber(); + return starknet.number.toBN(value).toNumber(); }; export const parseToFelt = value => { - return toBN(value).toString(); + return starknet.number.toBN(value).toString(); }; export const parseToUint256 = (value, decimals = DEFAULT_DECIMALS) => { const decimalsValue = parseToDecimals(value, decimals); - const uint256 = bnToUint256(toBN(decimalsValue)); + const uint256 = starknet.uint256.bnToUint256(toBN(decimalsValue)); return { type: 'struct', ...uint256 @@ -39,6 +34,6 @@ export const parseToUint256 = (value, decimals = DEFAULT_DECIMALS) => { }; export const parseFromUint256 = (value, decimals = DEFAULT_DECIMALS) => { - const bnString = uint256ToBN(value).toString(); + const bnString = starknet.uint256.uint256ToBN(value).toString(); return parseFromDecimals(bnString, decimals); }; diff --git a/yarn.lock b/yarn.lock index 7bee7d56..95538e3f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -113,10 +113,10 @@ "@web3-react/types" "^6.0.7" tiny-warning "^1.0.3" -"@argent/get-starknet@^0.1.4": - version "0.1.4" - resolved "https://registry.yarnpkg.com/@argent/get-starknet/-/get-starknet-0.1.4.tgz#e237a05f5e547795f8a86ae286b75cb2b4aa5883" - integrity sha512-owF5nPajqjPGhirI+FyK8UbXwkzif646Wymwee8gsz/uaftc6vCh+HkKUYLmD7Y3qLnEXzHykc6VnwLHk+Velw== +"@argent/get-starknet@^1.1.3": + version "1.1.3" + resolved "https://registry.yarnpkg.com/@argent/get-starknet/-/get-starknet-1.1.3.tgz#d5254038fa5aa8645cf8d845373d595a7b8b4cfe" + integrity sha512-upw+/BWg2lnf5WTWI9VJFceucn/Rjq1O4zpVUZFl/jQ7bl+XXWotrQSPD0PZVyFt0rwPu4sOkDcLEdzl+kmykw== "@babel/code-frame@7.10.4": version "7.10.4" From 65be2e1730138efaa5232798a904b77351e6dbcb Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Mon, 14 Feb 2022 10:42:00 +0200 Subject: [PATCH 11/12] fix --- src/services/wallet-handlers/argent-handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/wallet-handlers/argent-handler.js b/src/services/wallet-handlers/argent-handler.js index 366e7698..f4c530df 100644 --- a/src/services/wallet-handlers/argent-handler.js +++ b/src/services/wallet-handlers/argent-handler.js @@ -1,4 +1,4 @@ -import {getStarknet} from '@argent/get-starknet'; +import {getStarknet} from '../../libs'; export class ArgentHandler { constructor(config) { From f4c6ef325753e93ceab72fa3eebf7e8dea2c977d Mon Sep 17 00:00:00 2001 From: Dan Ziv Date: Mon, 14 Feb 2022 10:43:28 +0200 Subject: [PATCH 12/12] fix --- src/utils/number.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/number.js b/src/utils/number.js index 652b2f03..c8012581 100644 --- a/src/utils/number.js +++ b/src/utils/number.js @@ -26,7 +26,7 @@ export const parseToFelt = value => { export const parseToUint256 = (value, decimals = DEFAULT_DECIMALS) => { const decimalsValue = parseToDecimals(value, decimals); - const uint256 = starknet.uint256.bnToUint256(toBN(decimalsValue)); + const uint256 = starknet.uint256.bnToUint256(starknet.number.toBN(decimalsValue)); return { type: 'struct', ...uint256