forked from argentlabs/argent-starknet-recover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.ts
77 lines (75 loc) · 2.63 KB
/
execute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
SequencerProvider as NewProvider,
Account as NewAccount,
} from "starknet-4220";
import {
Call,
SequencerProvider as OldProvider,
Account as OldAccount,
ec,
} from "starknet";
import { BigNumber } from "ethers";
import { Account } from "./ui/pickAccounts";
import { lte } from "semver";
export async function estimateFee(account: Account, call: Call[] | Call) {
const calls = Array.isArray(call) ? call : [call];
const lowerCaseAddress = account.address.toLowerCase();
const keyPair = ec.getKeyPair(account.privateKey);
const starkKey = ec.getStarkKey(keyPair);
if (!BigNumber.from(account.signer).eq(starkKey)) {
throw new Error(
"Account cant be controlled with the selected private key or seed"
);
}
try {
const oldProvider = new OldProvider({ network: account.networkId as any });
const a = new OldAccount(oldProvider, lowerCaseAddress, keyPair);
return await a.estimateFee(calls);
} catch {
const newProvider = new NewProvider({ network: account.networkId as any });
const a = new NewAccount(newProvider, lowerCaseAddress, keyPair);
return a.estimateFee(calls);
}
}
export async function execute(account: Account, call: Call[] | Call) {
const calls = Array.isArray(call) ? call : [call];
const lowerCaseAddress = account.address.toLowerCase();
const keyPair = ec.getKeyPair(account.privateKey);
const starkKey = ec.getStarkKey(keyPair);
if (!BigNumber.from(account.signer).eq(starkKey)) {
throw new Error(
"Account cant be controlled with the selected private key or seed"
);
}
if (account.version && lte(account.version, "0.2.2")) {
try {
const oldProvider = new OldProvider({
network: account.networkId as any,
});
const a = new OldAccount(oldProvider, lowerCaseAddress, keyPair);
return await a.execute(calls);
} catch (e) {
console.warn("Fallback to new provider", (e as any)?.errorCode);
const newProvider = new NewProvider({
network: account.networkId as any,
});
const a = new NewAccount(newProvider, lowerCaseAddress, keyPair);
return await a.execute(calls);
}
} else {
try {
const newProvider = new NewProvider({
network: account.networkId as any,
});
const a = new NewAccount(newProvider, lowerCaseAddress, keyPair);
return await a.execute(calls);
} catch (e) {
console.warn("Fallback to old provider", (e as any)?.errorCode);
const oldProvider = new OldProvider({
network: account.networkId as any,
});
const a = new OldAccount(oldProvider, lowerCaseAddress, keyPair);
return await a.execute(calls);
}
}
}