Skip to content

Commit

Permalink
remove gas from messages + change on fee credit #100
Browse files Browse the repository at this point in the history
  • Loading branch information
shermike authored and KlonD90 committed Jul 18, 2024
1 parent 7d2ac80 commit f707e16
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-phones-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nilfoundation/niljs": minor
---

Breaking changes! Fee credit instead of gas
2 changes: 1 addition & 1 deletion examples/bounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const hash = await wallet.sendMessage({
to: anotherWallet.getAddressHex(),
value: 10_000_000n,
bounceTo: bounceAddress,
gas: 100_000n,
gas: 100_000n * 10n,
data: encodeFunctionData({
abi: WalletV1.abi,
functionName: "syncCall",
Expand Down
2 changes: 1 addition & 1 deletion examples/deployInternalMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const { address, hash } = await wallet.deployContract({
abi: abi,
args: [bytesToHex(pubkey), walletAddress],
value: 10000000n,
gas: 1000000n,
gas: 1000000n * 10n,
salt: 400n,
shardId: 1,
});
Expand Down
2 changes: 1 addition & 1 deletion examples/syncCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const anotherAddress = WalletV1.calculateWalletAddress({
await wallet.syncSendMessage({
to: anotherAddress,
value: 10n,
gas: 100_000n,
gas: 100_000n * 10n,
});

while (true) {
Expand Down
4 changes: 2 additions & 2 deletions examples/tokenMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ console.log("walletAddress", walletAddress);

const hashMessage = await wallet.sendMessage({
to: MINTER_ADDRESS,
gas: 1_000_000n,
gas: 1_000_000n * 10n,
value: 100_000_000n,
data: encodeFunctionData({
abi: MINTER_ABI,
Expand All @@ -72,7 +72,7 @@ const anotherAddress = WalletV1.calculateWalletAddress({
const sendHash = await wallet.sendMessage({
to: anotherAddress,
value: 10_000_000n,
gas: 100_000n,
gas: 100_000n * 10n,
tokens: [
{
id: n,
Expand Down
200 changes: 176 additions & 24 deletions src/clients/PublicClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,259 @@
import { defaultAddress } from "../../test/mocks/address.js";
import { testEnv } from "../../test/testEnv.js";
import { HttpTransport, addHexPrefix } from "../index.js";
import { addHexPrefix } from "../index.js";
import { MockTransport } from "../transport/MockTransport.js";
import { PublicClient } from "./PublicClient.js";

const client = new PublicClient({
transport: new HttpTransport({
endpoint: testEnv.endpoint,
}),
shardId: 1,
});

test("getBlockByHash", async ({ expect }) => {
const block = await client.getBlockByHash(
const fn = vi.fn();
fn.mockReturnValue({});
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
await client.getBlockByHash(
"0x158c4be17b52b92dc03cef7e8cd9cec64c6413175df3cce9f6ae1fb0d12106fa",
);
expect(fn).toHaveBeenCalledOnce();

expect(block).toBeDefined();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getBlockByHash",
params: [
1,
"0x158c4be17b52b92dc03cef7e8cd9cec64c6413175df3cce9f6ae1fb0d12106fa",
false,
],
});
});

test("getBlockByNumber", async ({ expect }) => {
const block = await client.getBlockByNumber("0x1b4");

expect(block).toBeDefined();
const fn = vi.fn();
fn.mockReturnValue({});
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
await client.getBlockByNumber("0x1b4");
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getBlockByNumber",
params: [1, "0x1b4", false],
});
});

test("getBlockMessageCountByNumber", async ({ expect }) => {
const fn = vi.fn();
fn.mockReturnValue(1);
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const count = await client.getBlockMessageCountByNumber("0x1b4", 1);

expect(count).toBeDefined();
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getBlockTransactionCountByNumber",
params: [1, "0x1b4"],
});
});

test("getBlockMessageCountByHash", async ({ expect }) => {
const fn = vi.fn();
fn.mockReturnValue(1);
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const count = await client.getBlockMessageCountByHash(
"0x158c4be17b52b92dc03cef7e8cd9cec64c6413175df3cce9f6ae1fb0d12106fa",
);

expect(count).toBeDefined();
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getBlockTransactionCountByHash",
params: [
1,
"0x158c4be17b52b92dc03cef7e8cd9cec64c6413175df3cce9f6ae1fb0d12106fa",
],
});
});

test("getCode", async ({ expect }) => {
const code = await client.getCode(addHexPrefix(defaultAddress), "0x1b4");
const fn = vi.fn();
fn.mockReturnValue("0x");
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const code = await client.getCode(addHexPrefix(defaultAddress), "latest");

expect(code).toBeDefined();
expect(code).toHaveLength(0);
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getCode",
params: [addHexPrefix(defaultAddress), "latest"],
});
});

test("getMessageCount", async ({ expect }) => {
const fn = vi.fn();
fn.mockReturnValue("0x1");
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const count = await client.getMessageCount(
addHexPrefix(defaultAddress),
"latest",
);

expect(count).toBeDefined();
expect(count).toBe(1);
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getTransactionCount",
params: [addHexPrefix(defaultAddress), "latest"],
});
});

test("getBalance", async ({ expect }) => {
const balance = await client.getBalance(
addHexPrefix(defaultAddress),
"latest",
);
const fn = vi.fn();
fn.mockReturnValue("0x100");
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const balance = await client.getBalance(addHexPrefix(defaultAddress));

expect(balance).toBeDefined();
expect(balance).toBe(256n);
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getBalance",
params: [addHexPrefix(defaultAddress), "latest"],
});

await client.getBalance(addHexPrefix(defaultAddress), "pending");

expect(fn).toHaveBeenLastCalledWith({
method: "eth_getBalance",
params: [addHexPrefix(defaultAddress), "pending"],
});
});

test("getMessageByHash", async ({ expect }) => {
const fn = vi.fn();
fn.mockReturnValue({
value: "0x100",
gasLimit: 100,
gasUsed: "0x100",
seqno: "0x100",
index: "0x0",
});
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const message = await client.getMessageByHash(
"0x158c4be17b52b92dc03cef7e8cd9cec64c6413175df3cce9f6ae1fb0d12106fa",
);

expect(message).toBeDefined();
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getInMessageByHash",
params: [
1,
"0x158c4be17b52b92dc03cef7e8cd9cec64c6413175df3cce9f6ae1fb0d12106fa",
],
});
});

test("getMessageReceiptByHash", async ({ expect }) => {
const fn = vi.fn();
fn.mockReturnValue({});
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const receipt = await client.getMessageReceiptByHash(
"0x158c4be17b52b92dc03cef7e8cd9cec64c6413175df3cce9f6ae1fb0d12106fa",
);

expect(receipt).toBeDefined();
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getInMessageReceipt",
params: [
1,
"0x158c4be17b52b92dc03cef7e8cd9cec64c6413175df3cce9f6ae1fb0d12106fa",
],
});
});

test("getGasPrice", async ({ expect }) => {
const gasPrice = await client.getGasPrice();

const fn = vi.fn();
fn.mockReturnValue("0x100");
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const gasPrice = await client.getGasPrice(1);
expect(gasPrice).toBeDefined();
expect(gasPrice).toBe(256n);
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_gasPrice",
params: [1],
});
});

test("estimateGasLimit", async ({ expect }) => {
const fn = vi.fn();
fn.mockReturnValue("0x100");
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const gasLimit = await client.estimateGasLimit();

expect(gasLimit).toBeDefined();
});

test("chainId", async ({ expect }) => {
const fn = vi.fn();
fn.mockReturnValue("0x1");
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const chainId = await client.chainId();

expect(chainId).toBeDefined();
expect(chainId).toBe(1);
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_chainId",
params: [],
});
});

test("getCurrencies", async ({ expect }) => {
const fn = vi.fn();
fn.mockReturnValue({});
const client = new PublicClient({
transport: new MockTransport(fn),
shardId: 1,
});
const currencies = await client.getCurrencies(
addHexPrefix(defaultAddress),
"latest",
);

expect(currencies).toBeDefined();
expect(currencies).toBeInstanceOf(Object);
expect(fn).toHaveBeenCalledOnce();
expect(fn).toHaveBeenLastCalledWith({
method: "eth_getCurrencies",
params: [addHexPrefix(defaultAddress), "latest"],
});
});
Loading

0 comments on commit f707e16

Please sign in to comment.