-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploySparkSelfClaim.ts
129 lines (99 loc) · 4.74 KB
/
deploySparkSelfClaim.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import hre from "hardhat";
import {PRIVATE_KEY, RPC_URL} from "./arguments/blockchainArgs"
import {unsparkingAIProxy} from "./arguments/sparkFactoryArgs";
import {initialSupply, tokenName, tokenSymbol} from "./arguments/srkArgs";
import {ethers} from "ethers";
(async () => {
try {
if (!PRIVATE_KEY) {
throw new Error("Missing environment variable: PRIVATE_KEY");
}
const { ethers } = require("hardhat");
const provider = ethers.getDefaultProvider(RPC_URL); // or your local node URL
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const [deployer, address1, address2] = await ethers.getSigners();
const initialOwner = wallet.address;
// =================== Deploy SRK ====================
const SparkSelfClaim = await hre.ethers.getContractFactory("SparkSelfClaim");
const sparkSelfClaim = await SparkSelfClaim.deploy();
await sparkSelfClaim.waitForDeployment();
console.log(`SparkSelfClaim deployed to ${sparkSelfClaim.target}`);
// ===================================================
// =================== Deploy ERC1967Proxy ===================
const ERC1967Proxy = await hre.ethers.getContractFactory("contracts/proxy/ERC1967.sol:ERC1967Proxy");
const erc1967Proxy = await ERC1967Proxy.deploy(sparkSelfClaim.target, "0x");
await erc1967Proxy.waitForDeployment();
console.log(`SparkSelfClaim Proxy deployed to ${erc1967Proxy.target}`);
// ===========================================================
// =================== Initialize Contracts ===================
SparkSelfClaim.attach(erc1967Proxy.target);
console.log(`SparkSelfClaim initialized`);
// ===========================================================
// =================== Verify Contracts ====================
if(process.env.ENV == 'testnet' || process.env.ENV == 'mainnet') {
await hre.run("verify:verify", {
address: sparkSelfClaim.target,
constructorArguments: [],
});
await hre.run("verify:verify", {
address: erc1967Proxy.target,
constructorArguments: [
sparkSelfClaim.target,
"0x"
],
contract: "contracts/proxy/ERC1967.sol:ERC1967Proxy",
sourcePath: "./contracts/proxy/ERC1967.sol" // Ensure this path is correct
});
}
// ==========================================================
if(process.env.ENV == 'localhost') {
console.log("")
const { ethers } = require('ethers');
const SRK = await hre.ethers.getContractFactory("SRK");
const srk = await SRK.deploy(deployer.address, tokenName, tokenSymbol, initialSupply);
await srk.waitForDeployment();
console.log(`SRK deployed to ${srk.target}`);
console.log("")
console.log("srk.approve")
await srk.approve(sparkSelfClaim.target, ethers.parseUnits("1000"));
console.log("")
const { MerkleTree } = require('merkletreejs');
const keccak256 = require('keccak256');
// Example data (Index, Address, Amount in Wei)
const data = [
{ index: 0, address: deployer.address, amount: ethers.parseUnits("200", 18) },
{ index: 1, address: address2.address, amount: ethers.parseUnits("700", 18) },
{ index: 2, address: address2.address, amount: ethers.parseUnits("100", 18) },
];
// Create leaves with ABI encoding (MUST MATCH Solidity)
const leaves = data.map(item =>
keccak256(
ethers.solidityPacked(
["uint256", "address", "uint256"], // Matches Solidity abi.encodePacked()
[item.index, item.address, item.amount]
)
)
);
// Generate Merkle Tree
const tree = new MerkleTree(leaves, keccak256, { sortPairs: true });
// Get Merkle Root
const root = '0x' + tree.getRoot().toString('hex');
// Get Proof for First User
const leaf = leaves[0]; // First user’s hashed leaf
const proof = tree.getProof(leaf).map(p => '0x' + p.data.toString('hex'));
// Verify the proof
const isValid = tree.verify(proof, keccak256(ethers.solidityPacked(["uint256", "address", "uint256"], [data[0].index, data[0].address, data[0].amount])), root);
// Print Results
console.log('Merkle Root:', root);
console.log('Merkle Proof for the first user:', proof);
console.log('User Leaf Hash:', '0x' + leaf.toString('hex')); // Useful for debugging
console.log("Proof is valid:", isValid);
console.log('sparkSelfClaim.register');
await sparkSelfClaim.register("0", root, srk.target, ethers.parseUnits("1000", 18))
console.log('sparkSelfClaim.claim');
await sparkSelfClaim.claim("0", 0, deployer.address, ethers.parseUnits("200", 18), proof)
}
} catch (e) {
console.log(e);
}
})();