-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirdropWrapper.js
81 lines (71 loc) · 2.57 KB
/
AirdropWrapper.js
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
import { abi } from "./AirdropContract.json";
import { providers, Contract, ethers } from "ethers";
const { MerkleTree } = require("merkletreejs");
require("dotenv").config();
export async function getContract() {
console.log("hey");
const contractAddress = "0x4004aD23277E51E1086beba0C0E8644Cb0DAe1d5";
const contractABI = abi;
let supportTokenContract;
try {
const { ethereum } = window;
console.log(ethereum.chainId);
if (ethereum.chainId === "0xaef3") {
const provider = new providers.Web3Provider(ethereum);
console.log("provider", provider);
const signer = provider.getSigner();
supportTokenContract = new Contract(contractAddress, contractABI, signer);
} else {
throw new Error("Please connect to the Alfajores network");
}
} catch (error) {
console.log("ERROR:", error);
}
console.log(supportTokenContract);
return supportTokenContract;
}
export async function claimTokens(proof, amount) {
const contract = await getContract();
const tx = await contract.claimTokens(proof, amount, {
gasLimit: 300000,
});
await tx.wait();
console.log(await tx);
}
export async function checkEligibility(whitelist) {
const leaves = whitelist.map((address) => ethers.utils.keccak256(address));
console.log("whielist", whitelist);
console.log("leaves", leaves);
const tree = new MerkleTree(leaves, ethers.utils.keccak256);
const leaf = ethers.utils.keccak256(whitelist[0]);
console.log("leaf", leaf);
const proof = tree.getProof(leaf);
const root = tree.getRoot().toString("hex");
return tree.verify(proof, leaf, root);
}
export async function merkleRoot() {
const merkleRoot = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("test"));
return merkleRoot;
}
export async function getTheProof(whitelist) {
const leaves = whitelist.map((address) => ethers.utils.keccak256(address));
console.log("whielist", whitelist);
console.log("leaves", leaves);
const tree = new MerkleTree(leaves, ethers.utils.keccak256);
const leaf = ethers.utils.keccak256(whitelist[0]);
console.log("leaf", leaf);
const proof = tree.getProof(leaf);
const root = tree.getRoot().toString("hex");
console.log(tree.verify(proof, leaf, root));
console.log(proof);
const bytes32Array = [];
const buffer = proof[0].data;
for (let i = 0; i < buffer.length; i += 32) {
const slice = buffer.slice(i, i + 32);
const bytes32 = `0x${slice.toString("hex").padEnd(64, "0")}`;
bytes32Array.push(bytes32);
}
// Call the claimTokens function with a valid proof and an amount
console.log(bytes32Array);
return bytes32Array;
}