Skip to content

Commit

Permalink
feat: allow address input
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Nov 15, 2022
1 parent 48b6c2e commit e87da9d
Show file tree
Hide file tree
Showing 15 changed files with 550 additions and 570 deletions.
31 changes: 0 additions & 31 deletions abi/Multicall.json

This file was deleted.

Binary file added argent-x-multicall-4.8.7.tgz
Binary file not shown.
35 changes: 35 additions & 0 deletions genSigners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BigNumber, Wallet } from "ethers";
import { ec, number } from "starknet";
import { BASE_DERIVATION_PATHS } from "./getAccounts";
import { getPathForIndex, getStarkPair } from "./keyDerivation";

export function equalSigner(a: string, b: string): boolean {
return BigNumber.from(a).eq(b);
}

interface Signer {
signer: string;
privateKey: string;
derivationPath?: string;
}

const amountOfSignersToGenerate = 20;

export function getDefaultSigners(seedPhrase?: string): Array<Signer> {
if (!seedPhrase) {
return [];
}
const privateKey = Wallet.fromMnemonic(seedPhrase).privateKey;
const arr = new Array(amountOfSignersToGenerate).fill(0);
const keypairs = BASE_DERIVATION_PATHS.flatMap((p) =>
arr.map((_, i) => ({
keyPair: getStarkPair(i, privateKey, p),
derivationPath: getPathForIndex(i, p),
}))
);
return keypairs.map(({ keyPair, ...rest }) => ({
...rest,
signer: ec.getStarkKey(keyPair),
privateKey: number.toHex(number.toBN(keyPair.getPrivate().toString())),
}));
}
2 changes: 1 addition & 1 deletion getAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export async function getAccountsByPrivateKey(
const accounts: {
address: string;
networkId: string;
privateKey: string;
privateKey?: string;
}[] = [];

const promises = proxyClassHashAndAccountClassHash2DMap.map(
Expand Down
8 changes: 4 additions & 4 deletions getAccountsInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const getAccountInfos = async (
networkId: "mainnet-alpha" | "goerli-alpha",
oraProgress?: ora.Ora
) => {
const progress = oraProgress ?? ora("Retrieving signers").start();
const progress = oraProgress ?? ora("Retrieving signers");
progress.start();
progress.text = "Retrieving signers";
const signer = await getSigners(addresses, networkId);
progress.text = "Retrieving versions";
Expand All @@ -18,9 +19,8 @@ export const getAccountInfos = async (
const implementations = await getImplementation(addresses, networkId);
progress.text = "Retrieving balances";
const balances = await getBalances(addresses, networkId);
if (!oraProgress) {
progress.succeed();
}
progress.succeed();

return addresses.map((address, i) => ({
address,
signer: signer[i],
Expand Down
51 changes: 14 additions & 37 deletions getImplementation.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,23 @@
import MULTICALL_ABI from "./abi/Multicall.json";
import {
SequencerProvider,
Contract as SNContract,
Abi,
stark,
hash,
number,
} from "starknet";

const MULTICALL_ADDRESS = {
"mainnet-alpha":
"0x0740a7a14618bb7e4688d10059bc42104d22c315bb647130630c77d3b6d3ee50",
"goerli-alpha":
"0x042a12c5a641619a6c58e623d5735273cdfb0e13df72c4bacb4e188892034bd6",
};
import { Multicall } from "@argent/x-multicall";
import { SequencerProvider } from "starknet";

export async function getImplementation(
addresses: string[],
network: "mainnet-alpha" | "goerli-alpha"
) {
const provider = new SequencerProvider({ network });
const multicallContract = new SNContract(
MULTICALL_ABI as Abi,
MULTICALL_ADDRESS[network],
provider
);

const calls = addresses.flatMap((address) => {
const compiledCalldata = stark.compileCalldata({});
return [
address,
hash.getSelectorFromName("get_implementation"),
compiledCalldata.length,
...compiledCalldata,
];
});

const response = await multicallContract.aggregate(calls);
const multicallProvider = new Multicall(provider);

const results: string[] = response.result.map((res: any) =>
number.toHex(res)
);
const implementations = (
await Promise.all(
addresses.map((address) =>
multicallProvider.call({
contractAddress: address,
entrypoint: "get_implementation",
})
)
)
).flat();

return results;
return implementations;
}
58 changes: 21 additions & 37 deletions getSigner.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
import MULTICALL_ABI from "./abi/Multicall.json";
import {
SequencerProvider,
Contract as SNContract,
Abi,
stark,
hash,
number,
} from "starknet";

const MULTICALL_ADDRESS = {
"mainnet-alpha":
"0x0740a7a14618bb7e4688d10059bc42104d22c315bb647130630c77d3b6d3ee50",
"goerli-alpha":
"0x042a12c5a641619a6c58e623d5735273cdfb0e13df72c4bacb4e188892034bd6",
};
import { SequencerProvider } from "starknet";
import { Multicall } from "@argent/x-multicall";

export async function getSigners(
addresses: string[],
network: "mainnet-alpha" | "goerli-alpha"
) {
const provider = new SequencerProvider({ network });
const multicallContract = new SNContract(
MULTICALL_ABI as Abi,
MULTICALL_ADDRESS[network],
provider
);

const calls = addresses.flatMap((address) => {
const compiledCalldata = stark.compileCalldata({});
return [
address,
hash.getSelectorFromName("get_signer"),
compiledCalldata.length,
...compiledCalldata,
];
});

const response = await multicallContract.aggregate(calls);
const multicallProvider = new Multicall(provider);

const results: string[] = response.result.map((res: any) =>
number.toHex(res)
);
const signers = (
await Promise.all(
addresses.map((address) =>
multicallProvider
.call({
contractAddress: address,
entrypoint: "getSigner",
})
.catch(() =>
multicallProvider.call({
contractAddress: address,
entrypoint: "get_signer",
})
)
)
)
).flat();

return results;
return signers;
}
73 changes: 18 additions & 55 deletions getTokenBalance.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import { chunk } from "lodash";
import MULTICALL_ABI from "./abi/Multicall.json";
import TOKENS from "./default-tokens.json";
import {
SequencerProvider,
Contract as SNContract,
Abi,
stark,
hash,
number,
uint256,
} from "starknet";
import { encode, SequencerProvider, uint256 } from "starknet";
import { formatTokenBalance } from "./formatTokenBalance";

const CHUNK_SIZE = 10;
const MULTICALL_ADDRESS = {
"mainnet-alpha":
"0x0740a7a14618bb7e4688d10059bc42104d22c315bb647130630c77d3b6d3ee50",
"goerli-alpha":
"0x042a12c5a641619a6c58e623d5735273cdfb0e13df72c4bacb4e188892034bd6",
};
import { Multicall } from "@argent/x-multicall";

export async function getBalances(
addresses: string[],
Expand All @@ -27,46 +10,21 @@ export async function getBalances(
const tokens = TOKENS.filter((token) => token.network === network);
const tokenAddresses = tokens.map((token) => token.address);
const provider = new SequencerProvider({ network });
const multicallContract = new SNContract(
MULTICALL_ABI as Abi,
MULTICALL_ADDRESS[network],
provider
);
const multicallProvider = new Multicall(provider);

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

const calls = addressesTokensCombinations.flatMap(({ address, token }) => {
const compiledCalldata = stark.compileCalldata({
address,
});
return [
token,
hash.getSelectorFromName("balanceOf"),
compiledCalldata.length,
...compiledCalldata,
];
});

const chunks = chunk(calls, CHUNK_SIZE * 4);
const results: string[] = [];
for (const lChunk of chunks) {
const response = await multicallContract.aggregate(lChunk);
const lResults: string[] = response.result.map((res: any) =>
number.toHex(res)
);
const resultChunks = chunk(lResults, 2);
const balances = resultChunks.reduce((acc, result) => {
const balance = uint256
.uint256ToBN({ low: result[0], high: result[1] })
.toString();

acc.push(balance);
return acc;
}, [] as string[]);
results.push(...balances);
}
const results = await Promise.all(
addressesTokensCombinations.map(({ address, token }) =>
multicallProvider.call({
contractAddress: token,
entrypoint: "balanceOf",
calldata: [address],
})
)
);

if (addressesTokensCombinations.length !== results.length) {
throw new Error("Something went wrong");
Expand All @@ -76,7 +34,12 @@ export async function getBalances(
address: addressToken.address,
token: addressToken.token,
balance: formatTokenBalance(
results[index],
uint256
.uint256ToBN({
low: encode.addHexPrefix(results[index][0]),
high: encode.addHexPrefix(results[index][1]),
})
.toString(),
tokens.find((x) => x.address === addressToken.token)!.decimals
),
}));
Expand Down
59 changes: 21 additions & 38 deletions getVersion.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
import MULTICALL_ABI from "./abi/Multicall.json";
import {
SequencerProvider,
Contract as SNContract,
Abi,
stark,
hash,
number,
shortString,
} from "starknet";

const MULTICALL_ADDRESS = {
"mainnet-alpha":
"0x0740a7a14618bb7e4688d10059bc42104d22c315bb647130630c77d3b6d3ee50",
"goerli-alpha":
"0x042a12c5a641619a6c58e623d5735273cdfb0e13df72c4bacb4e188892034bd6",
};
import { Multicall } from "@argent/x-multicall";
import { SequencerProvider } from "starknet";

export async function getVersion(
addresses: string[],
network: "mainnet-alpha" | "goerli-alpha"
) {
const provider = new SequencerProvider({ network });
const multicallContract = new SNContract(
MULTICALL_ABI as Abi,
MULTICALL_ADDRESS[network],
provider
);

const calls = addresses.flatMap((address) => {
const compiledCalldata = stark.compileCalldata({});
return [
address,
hash.getSelectorFromName("get_version"),
compiledCalldata.length,
...compiledCalldata,
];
});

const response = await multicallContract.aggregate(calls);
const multicallContract = new Multicall(provider);

const results: string[] = response.result.map((res: any) =>
shortString.decodeShortString(number.toHex(res))
);
const versions = (
await Promise.all(
addresses.map((address) =>
multicallContract
.call({
contractAddress: address,
entrypoint: "getVersion",
})
.catch(() =>
multicallContract.call({
contractAddress: address,
entrypoint: "get_version",
})
)
)
)
).flat();

return results;
return versions;
}
Loading

0 comments on commit e87da9d

Please sign in to comment.