Deploy & Invoke Logics
Pre-requisites
Before you begin, you'll required to know how to setup a JSON-RPC provider, a wallet and initialize a wallet.
Incase you are not familiar with the above, please refer to the Getting Started tutorial.
Deploying Logic
To facilitate the deployment of a logic, it is imperative to have a corresponding manifest file. This JSON-formatted file encapsulates essential metadata associated with the logic. Deployment necessitates the presence of both the logic's manifest file and a wallet, the latter requiring initialization through a mnemonic.
import { LogicFactory } from 'js-moi-sdk';
import manifest from './path/to/manifest.json';
import { wallet } from './wallet';
const deploy = () => {
const logicFactory = new LogicFactory(manifest, wallet);
const name = "LOG-FAC";
const description = "LOG";
const amount = 100000000;
const code = "0xffcd8ee6a29ec442dbbf9c6124dd3aeb833ef58052237d521654740857716b34";
const ix = await logicFactory.deploy("Seeder!", name, description, amount, code);
console.log(ix); // OUTPUT: { hash: '0x...', wait: [Function: bound waitForInteraction] AsyncFunction, result: [Function: bound processResult] AsyncFunction }
const receipt = await ix.wait();
console.log(receipt);
/**
Receipt Response:
{
"ix_type": "0x8",
"ix_hash": "0x...",
"status": 0,
"fuel_used": "0x2b..",
"hashes": [
...
],
"extra_data": {
"logic_id": "0x...",
"error": "0x"
},
"from": "0x...",
"to": "0x...",
"ix_index": "0x0",
"parts": [
...
]
}
*/
const result = await ix.result();
/**
Result Response
{
logic_id: "0x...",
error: null
}
*/
}
Invoking Logic
To invoke a logic, it required a wallet and the logic's ID.
The corresponding example below demonstrates how to invoke a logic and retrieve the result.
import fs from 'node:fs/promise';
import { wallet } from "./wallet";
const logicId = "0x...";
const logic = await getLogicDriver(logicId, wallet);
const { allTodos } = await this.logic.routines.GetTodos();
console.log(allTodos); // OUTPUT: [ { id: 1, text: 'Hello World!', completed: false } ]
Reading From Persistent Storage
To read from persistent storage, it required a wallet and the logic's ID.
The corresponding example below demonstrates how to read from persistent storage.
import fs from 'node:fs/promise';
const logic = await getLogicDriver(logicId, wallet);
const name = await logicDriver.persistentState.get("name");
console.log("name - ", name); // OUTPUT: "name - LOG-FAC"