-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethereumClient.ts
108 lines (98 loc) · 2.89 KB
/
ethereumClient.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {
WalletClient,
createWalletClient,
createPublicClient,
http,
PrivateKeyAccount,
PublicClient,
} from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { optimism } from "viem/chains";
import { CONTRACT_ABI, OPTIMISTIC_ETHERSCAN_TX } from "./utils/constants";
import { Submission } from "./model/submission";
import { getUserById } from "./controller/userController";
import { generateSHA256Hash } from "./utils/shared";
import { sendMessage } from "./discordBot";
import { getLevelById } from "./controller/levelController";
enum SubmissionTypeConverter {
"solidity" = 0,
"yul" = 1,
"vyper" = 2,
"huff" = 3,
"bytecode" = 4,
}
enum SubmissionOptimizedForConverter {
"gas" = 0,
"size" = 1,
}
let ethereumClient: PublicClient;
let account: PrivateKeyAccount;
let walletClient: WalletClient;
let nonce: number;
const transport = http(process.env.API_URL);
const connect = async () => {
try {
account = privateKeyToAccount(`0x${process.env.PRIVATE_KEY}`);
//<any> seems to fix a viem error when working with optimism/base, etc..
ethereumClient = createPublicClient<any>({
batch: {
multicall: true,
},
chain: optimism,
transport,
});
walletClient = createWalletClient({
chain: optimism,
transport,
});
nonce = await ethereumClient.getTransactionCount({
address: account.address,
});
} catch (error) {
console.log("## ETHEREUM CLIENT CONNECT ERROR: ", error);
}
};
connect();
const storeSubmissionOnChain = async (
user_id: number,
level_id: number,
submissions: Submission[]
) => {
const user = await getUserById(user_id);
const level = await getLevelById(level_id);
for (const s of submissions) {
const { request } = await ethereumClient.simulateContract({
account: account,
nonce: nonce,
address: process.env.CONTRACT_ADDRESS as `0x${string}`,
abi: CONTRACT_ABI,
functionName: "submit",
args: [
user?.wallet_address,
{
id: s?.id,
level_id: s?.level_id,
gas: s?.gas,
size: s?.size,
solutionType:
SubmissionTypeConverter[
s?.type as keyof typeof SubmissionTypeConverter
], //just to avoid warnings
optimized_for:
SubmissionOptimizedForConverter[
s?.optimized_for as keyof typeof SubmissionOptimizedForConverter
], //just to avoid warnings
submitted_at: Date.parse(s?.submitted_at.toString()),
bytecode_hash: `0x${generateSHA256Hash(s?.bytecode)}`,
user_name: user?.name,
},
],
});
nonce++;
const txHash = await walletClient.writeContract(request);
sendMessage(
`${user?.name} submitted a new solution for level ${level?.name}. [See the transaction here.](${OPTIMISTIC_ETHERSCAN_TX}${txHash})`
);
}
};
export { storeSubmissionOnChain };