Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit

Permalink
Merge pull request #8 from NilFoundation/shard-id
Browse files Browse the repository at this point in the history
add shard setting
  • Loading branch information
Rolaman authored Jul 1, 2024
2 parents 0949fa3 + f85ea1b commit 538067b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 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.0",
"version": "0.1.1",
"description": "Custom Hardhat plugin to enable seamless deployment and interaction with applications within =nil;",
"repository": {
"type": "git",
Expand Down
7 changes: 4 additions & 3 deletions src/handlers/getBlockByNumber.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { HandlerContext } from "../context";
import { executeOriginalFunction } from "../interceptors";
import { shardNumber } from "../utils/conversion";

export async function getBlockByNumber(
method: string,
params: any[],
context: HandlerContext,
) {
const preparedParams = prepareInput(params);
const preparedParams = prepareInput(params, context);
const response = await executeOriginalFunction(
method,
preparedParams,
Expand All @@ -15,8 +16,8 @@ export async function getBlockByNumber(
return adaptResponse(response);
}

function prepareInput(params: any[]): any[] {
return [1, ...params];
function prepareInput(params: any[], context: HandlerContext): any[] {
return [shardNumber(context.wallet.getAddressHex()), ...params];
}

function adaptResponse(response: any): any {
Expand Down
18 changes: 15 additions & 3 deletions src/handlers/getTransactionByHash.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import type { HandlerContext } from "../context";
import { executeOriginalFunction } from "../interceptors";
import { shardNumber } from "../utils/conversion";

export async function getTransactionByHash(
method: string,
params: any[],
context: HandlerContext,
) {
const [preparedMethod, preparedParams] = prepareInput(method, params);
const [preparedMethod, preparedParams] = prepareInput(
method,
params,
context,
);
const result = await executeOriginalFunction(
preparedMethod,
preparedParams,
Expand All @@ -15,9 +20,16 @@ export async function getTransactionByHash(
return adaptResult(result);
}

function prepareInput(method: string, params: any[]): [string, any[]] {
function prepareInput(
method: string,
params: any[],
context: HandlerContext,
): [string, any[]] {
const preparedMethod = "eth_getInMessageByHash";
const preparedParams = [1, ...params];
const preparedParams = [
context.hre.config.shardId ?? shardNumber(context.wallet.getAddressHex()),
...params,
];
return [preparedMethod, preparedParams];
}

Expand Down
21 changes: 18 additions & 3 deletions src/handlers/getTransactionReceipt.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import type { HandlerContext } from "../context";
import { executeOriginalFunction } from "../interceptors";
import { shardNumber } from "../utils/conversion";

export async function getTransactionReceipt(
method: string,
params: any[],
context: HandlerContext,
) {
const [preparedMethod, preparedParams] = prepareInput(method, params);
const [preparedMethod, preparedParams] = prepareInput(
method,
params,
context,
);
const result = await executeOriginalFunction(
preparedMethod,
preparedParams,
Expand All @@ -15,8 +20,18 @@ export async function getTransactionReceipt(
return adaptResponse(result, preparedParams);
}

function prepareInput(method: string, params: any[]): [string, any[]] {
return ["eth_getInMessageReceipt", [1, ...params]];
function prepareInput(
method: string,
params: any[],
context: HandlerContext,
): [string, any[]] {
return [
"eth_getInMessageReceipt",
[
context.hre.config.shardId ?? shardNumber(context.wallet.getAddressHex()),
...params,
],
];
}

function adaptResponse(result: any, params: any[]): any {
Expand Down
14 changes: 11 additions & 3 deletions src/handlers/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@ async function prepareDeployment(
);
await waitTillCompleted(
context.client,
1,
shardNumber(context.wallet.getAddressHex()),
`0x${Buffer.from(hash).toString("hex")}`,
);

const deployed = await context.wallet.deployContract({
shardId: shardNumber(context.wallet.getAddressHex()),
shardId:
context.hre.config.shardId ?? shardNumber(context.wallet.getAddressHex()),
bytecode: hexStringToUint8Array(params[0].data),
args: [bytesToHex(context.wallet.pubkey)],
salt: BigInt(Math.floor(Math.random() * 1024)),
gas: context.gasLimit,
value: context.gasLimit * 10n,
});
return deployed.hash;

const receipt = await waitTillCompleted(
context.client,
shardNumber(context.wallet.getAddressHex()),
deployed.hash,
);

return receipt[0].outMessages?.[0] ?? "";
}

async function handleDirectTransaction(
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ extendEnvironment(async (hre) => {

export interface NilHardhatUserConfig extends HardhatUserConfig {
walletAddress?: string;
gasLimit?: number;
shardId?: number;
}
2 changes: 1 addition & 1 deletion src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function isHttpNetworkConfig(
export async function setupWalletAndClient(
hre: HardhatRuntimeEnvironment,
): Promise<HandlerContext> {
const networkName = "nil_cluster";
const networkName = "nil";
const networkConfig = hre.config.networks[networkName];

// Error handling if the network configuration is not HTTP or is missing
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare module "hardhat/types/config" {
export interface NilHardhatUserConfig extends HardhatUserConfig {
walletAddress?: string;
gasLimit?: number;
shardId?: number;
}

interface HardhatConfig extends NilHardhatUserConfig {} // Augmenting existing type
Expand Down

0 comments on commit 538067b

Please sign in to comment.