|
1 | 1 | import { BigNumber, utils } from "ethers"; |
2 | | -import { |
3 | | - Account as SNAccount, |
4 | | - ec, |
5 | | - number, |
6 | | - SequencerProvider, |
7 | | - uint256, |
8 | | -} from "starknet"; |
| 2 | +import { number, uint256 } from "starknet"; |
9 | 3 | import { compileCalldata } from "starknet/dist/utils/stark"; |
10 | 4 | import { Account } from "../../ui/pickAccounts"; |
11 | 5 | import TOKENS from "../../default-tokens.json"; |
12 | 6 | import { Ora } from "ora"; |
13 | 7 | import { oraLog } from "../../oraLog"; |
14 | 8 | import { estimateFee, execute } from "../../execute"; |
| 9 | +import { formatAddress } from "../../addressFormatting"; |
15 | 10 |
|
16 | 11 | export async function transferAll(acc: Account, newAddress: string, ora: Ora) { |
17 | 12 | const { privateKey } = acc; |
18 | 13 | if (!privateKey) { |
19 | 14 | throw new Error("No private key for account to credit"); |
20 | 15 | } |
21 | 16 |
|
22 | | - const provider = new SequencerProvider({ network: acc.networkId as any }); |
23 | 17 | const tokens = TOKENS.filter((t) => t.network === acc.networkId); |
24 | 18 | const calls = Object.entries(acc.balances) |
25 | 19 | .filter(([, balance]) => utils.parseEther(balance).gt(0)) |
@@ -47,36 +41,44 @@ export async function transferAll(acc: Account, newAddress: string, ora: Ora) { |
47 | 41 | if (calls.length) { |
48 | 42 | const { suggestedMaxFee } = await estimateFee(acc, calls); |
49 | 43 |
|
50 | | - const callsWithFee = calls.map((c) => { |
51 | | - const tokenDetails = tokens.find((t) => t.symbol === "ETH"); |
52 | | - if (c.contractAddress === tokenDetails?.address) { |
53 | | - const balance = acc.balances[c.contractAddress]; |
54 | | - return { |
55 | | - ...c, |
56 | | - calldata: compileCalldata({ |
57 | | - to: newAddress.toLowerCase(), |
58 | | - value: { |
59 | | - type: "struct", |
60 | | - ...uint256.bnToUint256( |
61 | | - number.toBN( |
62 | | - utils |
63 | | - .parseUnits(balance, tokenDetails?.decimals ?? 18) |
64 | | - .sub(number.toHex(suggestedMaxFee)) |
65 | | - .toString() |
66 | | - ) |
67 | | - ), |
68 | | - }, |
69 | | - }), |
70 | | - }; |
71 | | - } |
72 | | - return c; |
73 | | - }); |
| 44 | + const callsWithFee = calls |
| 45 | + .map((c) => { |
| 46 | + const tokenDetails = tokens.find((t) => t.symbol === "ETH"); |
| 47 | + if (c.contractAddress === tokenDetails?.address) { |
| 48 | + const balance = acc.balances[c.contractAddress]; |
| 49 | + const amount = utils |
| 50 | + .parseUnits(balance, tokenDetails?.decimals ?? 18) |
| 51 | + .sub(number.toHex(suggestedMaxFee)); |
| 52 | + |
| 53 | + if (amount.lte(0)) { |
| 54 | + ora.info( |
| 55 | + `Account ${formatAddress( |
| 56 | + acc.address |
| 57 | + )} has not enough ETH to do a transfer` |
| 58 | + ); |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + ...c, |
| 64 | + calldata: compileCalldata({ |
| 65 | + to: newAddress.toLowerCase(), |
| 66 | + value: { |
| 67 | + type: "struct", |
| 68 | + ...uint256.bnToUint256(number.toBN(amount.toString())), |
| 69 | + }, |
| 70 | + }), |
| 71 | + }; |
| 72 | + } |
| 73 | + return c; |
| 74 | + }) |
| 75 | + .filter(Boolean); |
74 | 76 |
|
75 | 77 | // execute with suggested max fee substracted from a potential eth transfer |
76 | 78 | const transaction = await execute(acc, callsWithFee); |
77 | 79 |
|
78 | 80 | oraLog(ora, `Transaction ${transaction.transaction_hash} created`); |
79 | 81 |
|
80 | | - await provider.waitForTransaction(transaction.transaction_hash); |
| 82 | + return transaction.transaction_hash; |
81 | 83 | } |
82 | 84 | } |
0 commit comments