Skip to content

Commit

Permalink
feat(DAI withdrawals): call increaseAllowance before calling `initi…
Browse files Browse the repository at this point in the history
…ate_withdraw` (#200)
  • Loading branch information
maciejka authored May 25, 2022
1 parent 877e561 commit 7a2c284
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
32 changes: 27 additions & 5 deletions src/api/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,31 @@ export const maxTotalBalance = async ({decimals, symbol, contract}) => {
}
};

export const initiateWithdraw = async ({recipient, amount, decimals, contract}) => {
return sendL2Transaction(contract, 'initiate_withdraw', {
l1Recipient: parseToFelt(recipient),
amount: parseToUint256(amount, decimals)
});
const initiateWithdrawCall = ({bridge, recipient, amount, decimals}) => {
return {
contract: bridge,
method: 'initiate_withdraw',
args: {
l1Recipient: parseToFelt(recipient),
amount: parseToUint256(amount, decimals)
}
};
};

const increaseAllowanceCall = ({bridge, token, amount, decimals}) => {
return {
contract: token,
method: 'increaseAllowance',
args: {
spender: bridge.address,
amount: parseToUint256(amount, decimals)
}
};
};

export const initiateWithdraw = async ({symbol, bridge, token, recipient, amount, decimals}) => {
return sendL2Transaction([
...(isDai(symbol) ? [increaseAllowanceCall({bridge, token, amount, decimals})] : []),
initiateWithdrawCall({bridge, recipient, amount, decimals})
]);
};
12 changes: 9 additions & 3 deletions src/hooks/useTransferToL1.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {useL1Token} from '../providers/TokensProvider';
import {useSelectedToken} from '../providers/TransferProvider';
import {useL1Wallet, useL2Wallet} from '../providers/WalletsProvider';
import {waitForTransaction} from '../utils';
import {useL1TokenBridgeContract, useTokenBridgeContract} from './useContract';
import {useL1TokenBridgeContract, useL2TokenContract, useTokenBridgeContract} from './useContract';
import {useLogger} from './useLogger';
import {useCompleteTransferToL1Tracking, useTransferToL1Tracking} from './useTracking';
import {useTransfer} from './useTransfer';
Expand All @@ -28,12 +28,13 @@ export const useTransferToL1 = () => {
const {account: l2Account, config: l2Config} = useL2Wallet();
const selectedToken = useSelectedToken();
const getTokenBridgeContract = useTokenBridgeContract();
const getL2TokenContract = useL2TokenContract();
const {handleProgress, handleData, handleError} = useTransfer(TransferToL1Steps);
const progressOptions = useTransferProgress();

return useCallback(
async amount => {
const {decimals, bridgeAddress, name, symbol} = selectedToken;
const {decimals, bridgeAddress, tokenAddress, name, symbol} = selectedToken;

const sendInitiateWithdraw = () => {
trackInitiated({
Expand All @@ -42,10 +43,15 @@ export const useTransferToL1 = () => {
amount,
symbol
});

const bridgeContract = getTokenBridgeContract(bridgeAddress);
const tokenContract = getL2TokenContract(tokenAddress);

return initiateWithdraw({
recipient: l1Account,
contract: bridgeContract,
bridge: bridgeContract,
token: tokenContract,
symbol,
amount,
decimals
});
Expand Down
11 changes: 5 additions & 6 deletions src/utils/starknet.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ export const callL2Contract = async (contract, method, ...args) => {
}
};

export const sendL2Transaction = async (contract, method, args = {}) => {
export const sendL2Transaction = async calls => {
try {
const calldata = stark.compileCalldata(args);
const transaction = {
const transactions = calls.map(({contract, method, args = {}}) => ({
contractAddress: contract.address,
entrypoint: method,
calldata
};
return await getStarknet().account.execute(transaction);
calldata: stark.compileCalldata(args)
}));
return await getStarknet().account.execute(transactions);
} catch (ex) {
return Promise.reject(ex);
}
Expand Down

0 comments on commit 7a2c284

Please sign in to comment.