Skip to content

Commit

Permalink
feat: add asset to SN wallet after a successful deposit (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-ziv authored May 3, 2022
1 parent 206f27e commit 30d8ec3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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
}
}
});
};

0 comments on commit 30d8ec3

Please sign in to comment.