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

feat: watch asset to SN wallet #171

Merged
merged 9 commits into from
May 3, 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
14 changes: 13 additions & 1 deletion src/hooks/useTransferToL2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {allowance, approve, balanceOf, ethBalanceOf} from '../api/erc20';
import {ActionType, stepOf, TransferError, TransferStep, TransferToL2Steps} from '../enums';
import {starknet} from '../libs';
import {useDepositListener} from '../providers/EventManagerProvider';
import {useL2Token} from '../providers/TokensProvider';
import {useSelectedToken} from '../providers/TransferProvider';
import {useL1Wallet, useL2Wallet} from '../providers/WalletsProvider';
import utils from '../utils';
Expand All @@ -19,14 +20,15 @@ export const useTransferToL2 = () => {
const logger = useLogger('useTransferToL2');
const [trackInitiated, trackSuccess, trackError, trackReject] = useTransferToL2Tracking();
const {account: l1Account, chainId: l1ChainId, config: l1Config} = useL1Wallet();
const {account: l2Account} = useL2Wallet();
const {account: l2Account, chainId: l2ChainId} = useL2Wallet();
const {handleProgress, handleData, handleError} = useTransfer(TransferToL2Steps);
const selectedToken = useSelectedToken();
const getTokenContract = useTokenContract();
const getTokenBridgeContract = useTokenBridgeContract();
const progressOptions = useTransferProgress();
const {addListener, removeListener} = useDepositListener();
const maxTotalBalance = useMaxTotalBalance();
const getL2Token = useL2Token();

return useCallback(
async amount => {
Expand All @@ -35,6 +37,7 @@ export const useTransferToL2 = () => {
const bridgeContract = getTokenBridgeContract(bridgeAddress);
const isEthToken = utils.token.isEth(symbol);
const tokenBridgeAddress = bridgeAddress[l1ChainId];
const l2TokenAddress = getL2Token(symbol)?.tokenAddress[l2ChainId];

const readAllowance = () => {
return allowance({
Expand Down Expand Up @@ -98,6 +101,14 @@ export const useTransferToL2 = () => {
}
};

const maybeAddToken = async () => {
try {
await utils.token.addToken(l2TokenAddress);
} catch (ex) {
logger.warn(ex.message);
}
};

const isMaxBalanceExceeded = async () => {
const tokenBridgeBalance = await (isEthToken
? ethBalanceOf(tokenBridgeAddress)
Expand Down Expand Up @@ -138,6 +149,7 @@ export const useTransferToL2 = () => {
addListener(onDeposit);
logger.log('Calling deposit');
await sendDeposit();
await maybeAddToken(l2TokenAddress);
} catch (ex) {
removeListener();
trackError(ex);
Expand Down
13 changes: 13 additions & 0 deletions src/utils/token.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {NetworkType} from '../enums';
import {getStarknet} from '../libs';

export const isEth = tokenData => {
let symbol = '';
Expand All @@ -10,3 +11,15 @@ export const isEth = tokenData => {
}
return symbol === NetworkType.L1.symbol;
};

export const addToken = async address => {
await getStarknet().request({
type: 'wallet_watchAsset',
params: {
type: 'ERC20',
options: {
address
}
}
});
};