Skip to content

Commit

Permalink
refactor: cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sarvalabs-harshrastogi committed Apr 27, 2024
1 parent f540894 commit 86c3a1d
Show file tree
Hide file tree
Showing 49 changed files with 901 additions and 5,375 deletions.
12 changes: 10 additions & 2 deletions docs/source/manifest-call-encoder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,21 @@ Methods
.. code-block:: javascript
// Example
const error = "0x0e4f0666ae03737472696e67536f6d657468696e672077656e742077726f6e673f06b60166756e6374696f6e31282966756e6374696f6e322829";
const error = "0x0e6f0666d104de04737472696e67696e73756666696369656e742062616c616e636520666f722073656e6465723f06e60172756e74696d652e726f6f742829726f7574696e652e5472616e736665722829205b3078635d202e2e2e205b307831623a205448524f57203078355d";
const exception = ManifestCoder.decodeException(error);
console.log(exception)
>> { class: 'string', data: 'Something went wrong', trace: [ 'function1()', 'function2()' ] }
>> {
Class: "string",
Error: "insufficient balance for sender",
Revert: false,
Trace: [
"runtime.root()",
"routine.Transfer() [0xc] ... [0x1b: THROW 0x5]"
],
}
.. autofunction:: decodeState

Expand Down
125 changes: 54 additions & 71 deletions packages/js-moi-logic/__tests__/logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { Wallet } from "js-moi-wallet";
import { LogicDriver, getLogicDriver } from "../src.ts/logic-driver";
import { LogicFactory } from "../src.ts/logic-factory";

import type { LogicManifest } from "js-moi-manifest";
import { loadManifestFromFile } from "js-moi-manifest/__tests__/utils/helper";
import { JsonRpcProvider } from "js-moi-providers";
import manifest from "../manifests/erc20.json";

const HOST = "http://localhost:1600/";
const MNEMONIC = "visa security tobacco hood forget rate exhibit habit deny good sister slender";
const MNEMONIC = "laptop hybrid ripple unaware entire cover flag rally deliver adjust nerve ready";
const INITIAL_SUPPLY = 100000000;
const SYMBOL = "MOI";
const RECEIVER = "0x4cdc9a1430ca00cbaaab5dcd858236ba75e64b863d69fa799d31854e103ddf72";
Expand All @@ -28,33 +29,33 @@ it("should initialize the wallet", async () => {

describe("Logic", () => {
let logicId: string | undefined;
let manifest: LogicManifest.Manifest;

beforeAll(async () => {
manifest = await loadManifestFromFile("../../manifests/tokenledger.json");
});

describe("deploy logic", () => {
it("should deploy logic without options", async () => {
const factory = new LogicFactory(manifest, wallet);

const symbol = SYMBOL;
const supply = INITIAL_SUPPLY;
const ix = await factory.deploy("Seeder", SYMBOL, INITIAL_SUPPLY);

const ix = await factory.deploy("Seed!", symbol, supply);

const receipt = await ix.wait();
const result = await ix.result();
logicId = result.logic_id;

expect(ix.hash).toBeDefined();
expect(receipt).toBeDefined();

});


it("should deploy logic with options", async () => {
const factory = new LogicFactory(manifest, wallet);
const symbol = "MOI";
const supply = 100000000;
const option = { fuelPrice: 1, fuelLimit: 3000 + Math.floor(Math.random() * 3000) }
const ix = await factory.deploy("Seed!", symbol, supply, option);
const option = { fuelPrice: 1, fuelLimit: 3000 + Math.floor(Math.random() * 3000) };
const ix = await factory.deploy("Seeder", symbol, supply, option);

const receipt = await ix.wait();
const result = await ix.result();
logicId = result.logic_id;
Expand All @@ -64,64 +65,51 @@ describe("Logic", () => {
});
});


describe("logic driver initialized using signer", () => {
let logic: LogicDriver;

beforeAll(async () => {
if(logicId == null) {
expect(logicId).toBeDefined();
return;
};
if (logicId == null) {
const factory = new LogicFactory(manifest, wallet);
const ix = await factory.deploy("Seeder", SYMBOL, INITIAL_SUPPLY);
await ix.wait();
const result = await ix.result();
logicId = result.logic_id;
}

logic = await getLogicDriver(logicId, wallet);
logic = await getLogicDriver(logicId!, wallet);
});

it("should able to retrieve balance of the account", async () => {
const { balance } = await logic.routines.BalanceOf(wallet.getAddress());

expect(balance).toBe(INITIAL_SUPPLY);
});

it("should return object when multiple values are returned", async () => {
const values = await logic.routines.DoubleReturnValue(wallet.getAddress());
const { symbol, supply } = values;

expect(values).toBeDefined();
expect(typeof symbol).toBe('string');
expect(typeof supply).toBe('number');
expect(balance).toBe(INITIAL_SUPPLY);
});

it("should able to transfer without option", async () => {
const amount = Math.floor(Math.random() * 1000);
const ix = await logic.routines.Transfer(RECEIVER, amount);
const receipt = await ix.wait();
const { success } = await ix.result();

expect(ix.hash).toBeDefined();
expect(receipt).toBeDefined();
expect(typeof success).toBe('boolean');
});

it("should able to transfer with option", async () => {
const amount = Math.floor(Math.random() * 1000);
const option = { fuelPrice: 1, fuelLimit: 1000 + Math.floor(Math.random() * 1000) }
const option = { fuelPrice: 1, fuelLimit: 1000 + Math.floor(Math.random() * 1000) };
const ix = await logic.routines.Transfer(RECEIVER, amount, option);
const receipt = await ix.wait();
const result = await ix.result();
const { balance } = await logic.routines.BalanceOf(RECEIVER);

const { success } = result;

expect(balance).toBeGreaterThanOrEqual(amount);
expect(ix.hash).toBeDefined();
expect(receipt).toBeDefined();
expect(typeof success).toBe('boolean');
});

it("should throw error when logic execution throw error using `result()`", async () => {
const { balance } = await logic.routines.BalanceOf(wallet.getAddress());
const amount = balance + 1
const amount = balance + 1;
const ix = await logic.routines.Transfer(RECEIVER, amount);

try {
Expand All @@ -134,7 +122,7 @@ describe("Logic", () => {

it("should throw error when logic execution throw error using `wait()`", async () => {
const { balance } = await logic.routines.BalanceOf(wallet.getAddress());
const amount = balance + 1
const amount = balance + 1;
const ix = await logic.routines.Transfer(RECEIVER, amount);

try {
Expand All @@ -146,86 +134,81 @@ describe("Logic", () => {
});

it("should be able to read from persistent storage", async () => {
const symbol = await logic.persistentState.get(b => b.entity("symbol"));
const symbol = await logic.persistentState.get((b) => b.entity("symbol"));

expect(symbol).toBe(SYMBOL);
});

it("should throw error when reading from persistent storage with invalid key", async () => {
const invalidKey = "invalid-key";

expect(async () => {
await logic.persistentState.get(b => b.entity(invalidKey));
}).rejects.toThrow(`The provided slot "${invalidKey}" does not exist.`);
await logic.persistentState.get((b) => b.entity(invalidKey));
}).rejects.toThrow(`'${invalidKey}' is not member of persistent state`);
});
});

describe("logic driver initialized using provider", () => {
let logic: LogicDriver;

beforeAll(async () => {
if(logicId == null) {
expect(logicId).toBeDefined();
return;
};
if (logicId == null) {
const factory = new LogicFactory(manifest, wallet);
const ix = await factory.deploy("Seeder", SYMBOL, INITIAL_SUPPLY);
await ix.wait();
const result = await ix.result();
logicId = result.logic_id;
}

logic = await getLogicDriver(logicId, PROVIDER);
logic = await getLogicDriver(logicId!, PROVIDER);
});

it("should able to retrieve balance of the account", async () => {
const { balance } = await logic.routines.BalanceOf(wallet.getAddress());

expect(balance).toBeGreaterThan(0);
});

it("should return object when multiple values are returned", async () => {
const values = await logic.routines.DoubleReturnValue(wallet.getAddress());

expect(values).toBeDefined();

const { symbol, supply } = values;

expect(typeof symbol).toBe('string');
expect(typeof supply).toBe('number');
expect(balance).toBeGreaterThan(0);
});

it("should throw an exception in mutating routine call", async () => {
const amount = Math.floor(Math.random() * 1000);

expect(async () => {
await logic.routines.Transfer(RECEIVER, amount);
}).rejects.toThrow("Mutating routine calls require a signer to be initialized.");
});

it("should be able to read from persistent storage", async () => {
const symbol = await logic.persistentState.get(b => b.entity("symbol"));
const symbol = await logic.persistentState.get((b) => b.entity("symbol"));

expect(symbol).toBe(SYMBOL);
});

it("should throw an exception when reading from persistent storage with invalid key", async () => {
const invalidKey = "invalid-key";

expect(async () => {
await logic.persistentState.get(b => b.entity(invalidKey));
}).rejects.toThrow(`The provided slot "${invalidKey}" does not exist.`);
await logic.persistentState.get((b) => b.entity(invalidKey));
}).rejects.toThrow(`'${invalidKey}' is not member of persistent state`);
});
});

let logic: LogicDriver;

beforeAll(async () => {
logic = new LogicDriver("0x", manifest, wallet)
logic = new LogicDriver("0x", manifest, wallet);
});

it("should be able return is routine mutable or not", () => {
const routine = [{ name: "Transfer", mutable: false }, { name: "BalanceOf", mutable: false }];

for(const { name, mutable } of routine) {
const routine = [
{ name: "Transfer", mutable: true },
{ name: "BalanceOf", mutable: false },
];

for (const { name, mutable } of routine) {
if (name in logic.routines) {
const isMutable = logic.routines[name].isMutable();
expect(isMutable).toBe(mutable);
}
}
})
})
});
});
Loading

0 comments on commit 86c3a1d

Please sign in to comment.