Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit e87da9d

Browse files
committed
feat: allow address input
1 parent 48b6c2e commit e87da9d

15 files changed

+550
-570
lines changed

abi/Multicall.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

argent-x-multicall-4.8.7.tgz

4.98 KB
Binary file not shown.

genSigners.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { BigNumber, Wallet } from "ethers";
2+
import { ec, number } from "starknet";
3+
import { BASE_DERIVATION_PATHS } from "./getAccounts";
4+
import { getPathForIndex, getStarkPair } from "./keyDerivation";
5+
6+
export function equalSigner(a: string, b: string): boolean {
7+
return BigNumber.from(a).eq(b);
8+
}
9+
10+
interface Signer {
11+
signer: string;
12+
privateKey: string;
13+
derivationPath?: string;
14+
}
15+
16+
const amountOfSignersToGenerate = 20;
17+
18+
export function getDefaultSigners(seedPhrase?: string): Array<Signer> {
19+
if (!seedPhrase) {
20+
return [];
21+
}
22+
const privateKey = Wallet.fromMnemonic(seedPhrase).privateKey;
23+
const arr = new Array(amountOfSignersToGenerate).fill(0);
24+
const keypairs = BASE_DERIVATION_PATHS.flatMap((p) =>
25+
arr.map((_, i) => ({
26+
keyPair: getStarkPair(i, privateKey, p),
27+
derivationPath: getPathForIndex(i, p),
28+
}))
29+
);
30+
return keypairs.map(({ keyPair, ...rest }) => ({
31+
...rest,
32+
signer: ec.getStarkKey(keyPair),
33+
privateKey: number.toHex(number.toBN(keyPair.getPrivate().toString())),
34+
}));
35+
}

getAccounts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export async function getAccountsByPrivateKey(
126126
const accounts: {
127127
address: string;
128128
networkId: string;
129-
privateKey: string;
129+
privateKey?: string;
130130
}[] = [];
131131

132132
const promises = proxyClassHashAndAccountClassHash2DMap.map(

getAccountsInfo.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export const getAccountInfos = async (
99
networkId: "mainnet-alpha" | "goerli-alpha",
1010
oraProgress?: ora.Ora
1111
) => {
12-
const progress = oraProgress ?? ora("Retrieving signers").start();
12+
const progress = oraProgress ?? ora("Retrieving signers");
13+
progress.start();
1314
progress.text = "Retrieving signers";
1415
const signer = await getSigners(addresses, networkId);
1516
progress.text = "Retrieving versions";
@@ -18,9 +19,8 @@ export const getAccountInfos = async (
1819
const implementations = await getImplementation(addresses, networkId);
1920
progress.text = "Retrieving balances";
2021
const balances = await getBalances(addresses, networkId);
21-
if (!oraProgress) {
22-
progress.succeed();
23-
}
22+
progress.succeed();
23+
2424
return addresses.map((address, i) => ({
2525
address,
2626
signer: signer[i],

getImplementation.ts

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,23 @@
1-
import MULTICALL_ABI from "./abi/Multicall.json";
2-
import {
3-
SequencerProvider,
4-
Contract as SNContract,
5-
Abi,
6-
stark,
7-
hash,
8-
number,
9-
} from "starknet";
10-
11-
const MULTICALL_ADDRESS = {
12-
"mainnet-alpha":
13-
"0x0740a7a14618bb7e4688d10059bc42104d22c315bb647130630c77d3b6d3ee50",
14-
"goerli-alpha":
15-
"0x042a12c5a641619a6c58e623d5735273cdfb0e13df72c4bacb4e188892034bd6",
16-
};
1+
import { Multicall } from "@argent/x-multicall";
2+
import { SequencerProvider } from "starknet";
173

184
export async function getImplementation(
195
addresses: string[],
206
network: "mainnet-alpha" | "goerli-alpha"
217
) {
228
const provider = new SequencerProvider({ network });
23-
const multicallContract = new SNContract(
24-
MULTICALL_ABI as Abi,
25-
MULTICALL_ADDRESS[network],
26-
provider
27-
);
28-
29-
const calls = addresses.flatMap((address) => {
30-
const compiledCalldata = stark.compileCalldata({});
31-
return [
32-
address,
33-
hash.getSelectorFromName("get_implementation"),
34-
compiledCalldata.length,
35-
...compiledCalldata,
36-
];
37-
});
38-
39-
const response = await multicallContract.aggregate(calls);
9+
const multicallProvider = new Multicall(provider);
4010

41-
const results: string[] = response.result.map((res: any) =>
42-
number.toHex(res)
43-
);
11+
const implementations = (
12+
await Promise.all(
13+
addresses.map((address) =>
14+
multicallProvider.call({
15+
contractAddress: address,
16+
entrypoint: "get_implementation",
17+
})
18+
)
19+
)
20+
).flat();
4421

45-
return results;
22+
return implementations;
4623
}

getSigner.ts

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,30 @@
1-
import MULTICALL_ABI from "./abi/Multicall.json";
2-
import {
3-
SequencerProvider,
4-
Contract as SNContract,
5-
Abi,
6-
stark,
7-
hash,
8-
number,
9-
} from "starknet";
10-
11-
const MULTICALL_ADDRESS = {
12-
"mainnet-alpha":
13-
"0x0740a7a14618bb7e4688d10059bc42104d22c315bb647130630c77d3b6d3ee50",
14-
"goerli-alpha":
15-
"0x042a12c5a641619a6c58e623d5735273cdfb0e13df72c4bacb4e188892034bd6",
16-
};
1+
import { SequencerProvider } from "starknet";
2+
import { Multicall } from "@argent/x-multicall";
173

184
export async function getSigners(
195
addresses: string[],
206
network: "mainnet-alpha" | "goerli-alpha"
217
) {
228
const provider = new SequencerProvider({ network });
23-
const multicallContract = new SNContract(
24-
MULTICALL_ABI as Abi,
25-
MULTICALL_ADDRESS[network],
26-
provider
27-
);
28-
29-
const calls = addresses.flatMap((address) => {
30-
const compiledCalldata = stark.compileCalldata({});
31-
return [
32-
address,
33-
hash.getSelectorFromName("get_signer"),
34-
compiledCalldata.length,
35-
...compiledCalldata,
36-
];
37-
});
38-
39-
const response = await multicallContract.aggregate(calls);
9+
const multicallProvider = new Multicall(provider);
4010

41-
const results: string[] = response.result.map((res: any) =>
42-
number.toHex(res)
43-
);
11+
const signers = (
12+
await Promise.all(
13+
addresses.map((address) =>
14+
multicallProvider
15+
.call({
16+
contractAddress: address,
17+
entrypoint: "getSigner",
18+
})
19+
.catch(() =>
20+
multicallProvider.call({
21+
contractAddress: address,
22+
entrypoint: "get_signer",
23+
})
24+
)
25+
)
26+
)
27+
).flat();
4428

45-
return results;
29+
return signers;
4630
}

getTokenBalance.ts

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
1-
import { chunk } from "lodash";
2-
import MULTICALL_ABI from "./abi/Multicall.json";
31
import TOKENS from "./default-tokens.json";
4-
import {
5-
SequencerProvider,
6-
Contract as SNContract,
7-
Abi,
8-
stark,
9-
hash,
10-
number,
11-
uint256,
12-
} from "starknet";
2+
import { encode, SequencerProvider, uint256 } from "starknet";
133
import { formatTokenBalance } from "./formatTokenBalance";
14-
15-
const CHUNK_SIZE = 10;
16-
const MULTICALL_ADDRESS = {
17-
"mainnet-alpha":
18-
"0x0740a7a14618bb7e4688d10059bc42104d22c315bb647130630c77d3b6d3ee50",
19-
"goerli-alpha":
20-
"0x042a12c5a641619a6c58e623d5735273cdfb0e13df72c4bacb4e188892034bd6",
21-
};
4+
import { Multicall } from "@argent/x-multicall";
225

236
export async function getBalances(
247
addresses: string[],
@@ -27,46 +10,21 @@ export async function getBalances(
2710
const tokens = TOKENS.filter((token) => token.network === network);
2811
const tokenAddresses = tokens.map((token) => token.address);
2912
const provider = new SequencerProvider({ network });
30-
const multicallContract = new SNContract(
31-
MULTICALL_ABI as Abi,
32-
MULTICALL_ADDRESS[network],
33-
provider
34-
);
13+
const multicallProvider = new Multicall(provider);
3514

3615
const addressesTokensCombinations = tokenAddresses.flatMap((token) =>
3716
addresses.map((address) => ({ address, token }))
3817
);
3918

40-
const calls = addressesTokensCombinations.flatMap(({ address, token }) => {
41-
const compiledCalldata = stark.compileCalldata({
42-
address,
43-
});
44-
return [
45-
token,
46-
hash.getSelectorFromName("balanceOf"),
47-
compiledCalldata.length,
48-
...compiledCalldata,
49-
];
50-
});
51-
52-
const chunks = chunk(calls, CHUNK_SIZE * 4);
53-
const results: string[] = [];
54-
for (const lChunk of chunks) {
55-
const response = await multicallContract.aggregate(lChunk);
56-
const lResults: string[] = response.result.map((res: any) =>
57-
number.toHex(res)
58-
);
59-
const resultChunks = chunk(lResults, 2);
60-
const balances = resultChunks.reduce((acc, result) => {
61-
const balance = uint256
62-
.uint256ToBN({ low: result[0], high: result[1] })
63-
.toString();
64-
65-
acc.push(balance);
66-
return acc;
67-
}, [] as string[]);
68-
results.push(...balances);
69-
}
19+
const results = await Promise.all(
20+
addressesTokensCombinations.map(({ address, token }) =>
21+
multicallProvider.call({
22+
contractAddress: token,
23+
entrypoint: "balanceOf",
24+
calldata: [address],
25+
})
26+
)
27+
);
7028

7129
if (addressesTokensCombinations.length !== results.length) {
7230
throw new Error("Something went wrong");
@@ -76,7 +34,12 @@ export async function getBalances(
7634
address: addressToken.address,
7735
token: addressToken.token,
7836
balance: formatTokenBalance(
79-
results[index],
37+
uint256
38+
.uint256ToBN({
39+
low: encode.addHexPrefix(results[index][0]),
40+
high: encode.addHexPrefix(results[index][1]),
41+
})
42+
.toString(),
8043
tokens.find((x) => x.address === addressToken.token)!.decimals
8144
),
8245
}));

getVersion.ts

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,30 @@
1-
import MULTICALL_ABI from "./abi/Multicall.json";
2-
import {
3-
SequencerProvider,
4-
Contract as SNContract,
5-
Abi,
6-
stark,
7-
hash,
8-
number,
9-
shortString,
10-
} from "starknet";
11-
12-
const MULTICALL_ADDRESS = {
13-
"mainnet-alpha":
14-
"0x0740a7a14618bb7e4688d10059bc42104d22c315bb647130630c77d3b6d3ee50",
15-
"goerli-alpha":
16-
"0x042a12c5a641619a6c58e623d5735273cdfb0e13df72c4bacb4e188892034bd6",
17-
};
1+
import { Multicall } from "@argent/x-multicall";
2+
import { SequencerProvider } from "starknet";
183

194
export async function getVersion(
205
addresses: string[],
216
network: "mainnet-alpha" | "goerli-alpha"
227
) {
238
const provider = new SequencerProvider({ network });
24-
const multicallContract = new SNContract(
25-
MULTICALL_ABI as Abi,
26-
MULTICALL_ADDRESS[network],
27-
provider
28-
);
29-
30-
const calls = addresses.flatMap((address) => {
31-
const compiledCalldata = stark.compileCalldata({});
32-
return [
33-
address,
34-
hash.getSelectorFromName("get_version"),
35-
compiledCalldata.length,
36-
...compiledCalldata,
37-
];
38-
});
39-
40-
const response = await multicallContract.aggregate(calls);
9+
const multicallContract = new Multicall(provider);
4110

42-
const results: string[] = response.result.map((res: any) =>
43-
shortString.decodeShortString(number.toHex(res))
44-
);
11+
const versions = (
12+
await Promise.all(
13+
addresses.map((address) =>
14+
multicallContract
15+
.call({
16+
contractAddress: address,
17+
entrypoint: "getVersion",
18+
})
19+
.catch(() =>
20+
multicallContract.call({
21+
contractAddress: address,
22+
entrypoint: "get_version",
23+
})
24+
)
25+
)
26+
)
27+
).flat();
4528

46-
return results;
29+
return versions;
4730
}

0 commit comments

Comments
 (0)