Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ssz serialization and add WalletClient tests #13

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-nails-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nilfoundation/niljs": patch
---

Enable ssz serialization
5 changes: 5 additions & 0 deletions .changeset/thirty-buses-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nilfoundation/niljs": patch
---

Add WalletClient unit tests
80 changes: 0 additions & 80 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"require": "./dist/niljs.cjs.js"
}
},
"sideEffects": false,
"description": "Typescript library to interact with the Nil blockchain. Can be used in the browser or in Node.js.",
"scripts": {
"test": "vitest -c ./test/vitest.config.ts",
Expand All @@ -50,9 +51,7 @@
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/elliptic": "^6.4.18",
"@vitest/coverage-v8": "^1.6.0",
"elliptic": "^6.5.5",
"rimraf": "^5.0.7",
"rollup": "^4.17.2",
"rollup-plugin-dts": "^6.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/clients/PublicClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { defaultAddress } from "../../test/mocks/address.js";
import { endpoint } from "../../test/mocks/endpoint.js";
import { rawMsg } from "../../test/mocks/message.js";
import { masterShardId } from "../../test/mocks/shard.js";
import { testEnv } from "../../test/testEnv.js";
import { addHexPrefix } from "../index.js";
import { PublicClient } from "./PublicClient.js";

const client = new PublicClient({
endpoint,
endpoint: testEnv.endpoint,
});

test("getBlockByHash", async ({ expect }) => {
Expand Down
12 changes: 12 additions & 0 deletions src/clients/PublicClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,25 @@ class PublicClient extends BaseClient {

/**
* getGasPrice returns the gas price in wei.
* @param shardId - The shard id.
* @returns The gas price.
*/
public async getGasPrice(shardId: number): Promise<bigint> {
const stubGasPrice = BigInt(1000000000);

return stubGasPrice;
}

/**
* estimateGasLimit returns the gas limit.
* @param shardId - The shard id.
* @returns The gas limit.
*/
public async estimateGasLimit(shardId: number): Promise<bigint> {
const stubGasLimit = BigInt(1000000);

return stubGasLimit;
}
}

export { PublicClient };
86 changes: 29 additions & 57 deletions src/clients/WalletClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,47 @@
import { endpoint } from "../../test/mocks/endpoint.js";

import abi from "../../test/mocks/contracts/simpleStorage/bin/SimpleStorage.abi";
import { type IMessage, LocalKeySigner, generatePrivateKey } from "../index.js";
import { defaultAddress } from "../../test/mocks/address.js";
import { bytecode as precompiledContractBytecode } from "../../test/mocks/contracts/simpleStorage/bytecode.js";
import { testEnv } from "../../test/testEnv.js";
import { type IMessage, LocalKeySigner, addHexPrefix } from "../index.js";
import { WalletClient } from "./WalletClient.js";

const client = new WalletClient({
endpoint,
endpoint: testEnv.endpoint,
signer: new LocalKeySigner({
privateKey: generatePrivateKey(),
privateKey: testEnv.localPrivKey,
}),
});

test("sendMessage", async ({ expect }) => {
const newMessage = {
to: "0x1234",
data: 100,
} as unknown as IMessage;

const hash = await client.sendMessage(newMessage);

expect(hash).toBeDefined();
});

test("sendMessage with from field", async ({ expect }) => {
const newMessage = {
from: "0x1234",
to: "0x1234",
data: 100,
} as unknown as IMessage;
test("prepareMessage", async () => {
const message = {
to: addHexPrefix(defaultAddress),
};

const hash = await client.sendMessage(newMessage);
const preparedMessage = await client.prepareMessage(message as IMessage);

expect(hash).toBeDefined();
expect(preparedMessage.from).toBeDefined();
expect(preparedMessage.gasPrice).toBeDefined();
});

test("sendMessage with from field and shouldValidate false", async ({
expect,
}) => {
const newMessage = {
from: "0x1234",
to: "0x1234",
data: 100,
} as unknown as IMessage;
test("sendMessage", async () => {
const message = {
to: addHexPrefix(defaultAddress),
value: 0n,
};

const hash = await client.sendMessage(newMessage, { shouldValidate: false });
const result = await client.sendMessage(message);

expect(hash).toBeDefined();
expect(result).toBeDefined();
});

test("sendRawMessage", async ({ expect }) => {
const newMessage = {
to: "0x1234",
data: 100,
} as unknown as IMessage;
const signedMessage = client.signMessage(newMessage);

const hash = await client.sendRawMessage(signedMessage);

expect(hash).toBeDefined();
});

test("Deploy contract", async ({ expect }) => {
const newMessage = {
data: 100,
} as unknown as IMessage;

const hash = await client.deployContract({
bytecode: new Uint8Array(),
args: new Uint8Array(),
abi: abi,
test("deployContract", async () => {
const result = await client.deployContract({
deployData: {
bytecode: precompiledContractBytecode,
},
});

expect(hash).toBeDefined();
expect(result).toBeDefined();
});

// TODO: implement this test and this feature
// test("deployContract with constructor", async () => {
Loading
Loading