Skip to content

Commit

Permalink
Wallet and contract deploy utils
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrskv committed Jul 18, 2024
1 parent ab39b1a commit 7f40c3a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
25 changes: 23 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nilfoundation/hardhat-plugin",
"version": "0.1.9",
"version": "0.1.10",
"description": "Custom Hardhat plugin to enable seamless deployment and interaction with applications within =nil;",
"repository": {
"type": "git",
Expand All @@ -25,6 +25,7 @@
"@nilfoundation/niljs": "^0.9.0",
"ethers": "^6.12.1",
"hardhat": "^2.22.4",
"@nomicfoundation/hardhat-ethers": "^3.0.6",
"viem": "^2.16.3"
},
"devDependencies": {
Expand Down
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ import type { HardhatUserConfig } from "hardhat/types";
import { unifiedInterceptor } from "./interceptors";
import { setupWalletAndClient } from "./setup";

extendEnvironment(async (hre) => {
const context = await setupWalletAndClient(hre);
extendEnvironment((hre) => {
const originalRequest = hre.network.provider.request.bind(
hre.network.provider,
);
const originalSend = hre.network.provider.send.bind(hre.network.provider);
const contextPromise = setupWalletAndClient(
hre,
originalRequest,
originalSend,
);

hre.network.provider.send = (method, params) => {
hre.network.provider.send = async (method, params) => {
const context = await contextPromise;
context.isRequest = false;
return unifiedInterceptor(method, params || [], context);
};

hre.network.provider.request = (args) => {
hre.network.provider.request = async (args) => {
const context = await contextPromise;
context.isRequest = true;
const safeParams = Array.isArray(args.params)
? args.params
Expand Down
40 changes: 31 additions & 9 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
HttpNetworkConfig,
NetworkConfig,
} from "hardhat/types";
import { bytesToHex } from "viem";
import type { HandlerContext } from "./context";
import { shardNumber } from "./utils/conversion";
import { ensure0xPrefix } from "./utils/hex";
Expand All @@ -32,6 +33,8 @@ function isHttpNetworkConfig(
// Function to setup the wallet and client
export async function setupWalletAndClient(
hre: HardhatRuntimeEnvironment,
originalRequest: any,
originalSend: any,
): Promise<HandlerContext> {
const networkName = "nil";
const networkConfig = hre.config.networks[networkName];
Expand All @@ -54,34 +57,53 @@ export async function setupWalletAndClient(
throw new Error("No private key configured for the network.");
}

const walletAddress = hre.config.walletAddress
const signer = new LocalECDSAKeySigner({ privateKey });
const pubKey = await signer.getPublicKey();

const newWalletSalt = new Uint8Array(32);
let walletAddress = hre.config.walletAddress
? ensure0xPrefix(hre.config.walletAddress)
: undefined;
if (!walletAddress) {
throw new Error("Wallet address is not configured.");
walletAddress = bytesToHex(
WalletV1.calculateWalletAddress({
pubKey,
shardId: 1,
salt: newWalletSalt,
}),
);

console.log(
`Wallet address not found in configuration.\nGenerated wallet address for current private key: ${walletAddress}`,
);
}

// Set up network components
const client = new PublicClient({
transport: new HttpTransport({ endpoint: url }),
shardId: shardNumber(walletAddress),
});
const faucet = new Faucet(client);

const signer = new LocalECDSAKeySigner({ privateKey });
const pubKey = await signer.getPublicKey();
const config: WalletV1Config = {
pubkey: pubKey,
client,
signer,
address: walletAddress,
};
const wallet = new WalletV1(config);
const faucet = new Faucet(client);

const originalRequest = hre.network.provider.request.bind(
hre.network.provider,
);
const originalSend = hre.network.provider.send.bind(hre.network.provider);
const existingWallet = await client.getCode(walletAddress, "latest");
if (existingWallet.length === 0) {
console.log("Deploying new wallet...");

wallet.salt = newWalletSalt;

await faucet.withdrawToWithRetry(walletAddress, 1_000_000_000n);
await wallet.selfDeploy();

console.log("Deployed new wallet.");
}

return {
hre,
Expand Down
5 changes: 4 additions & 1 deletion src/utils/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export function hexStringToUint8Array(hexString: string): Uint8Array {
const byteArray = new Uint8Array(numBytes);

for (let i = 0; i < numBytes; i++) {
byteArray[i] = Number.parseInt(cleanHexString.substr(i * 2, 2), 16);
byteArray[i] = Number.parseInt(
cleanHexString.substring(i * 2, i * 2 + 2),
16,
);
}

return byteArray;
Expand Down

0 comments on commit 7f40c3a

Please sign in to comment.