Skip to content

Commit

Permalink
enable ssz encoding, add unit test for wallet client #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ukorvl committed Jun 1, 2024
1 parent e1a1354 commit f47866e
Show file tree
Hide file tree
Showing 41 changed files with 359 additions and 412 deletions.
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

0 comments on commit f47866e

Please sign in to comment.