Javascript SDK
Discover the JavaScript Software Development Kit (SDK) for Ola, allowing you to build applications using JavaScript.
Prerequisites
Before using the Ola JavaScript SDK, ensure that your project includes the following packages:
ethers
: A comprehensive Ethereum wallet implementation and utilities in JavaScript (and TypeScript).@sin7y/ola-abi-wasm
: A WebAssembly package for handling ABI encoding and decoding within the Ola ecosystem.@sin7y/ola-crypto
: A WebAssembly package providing cryptographic functionalities for the Ola ecosystem.
These packages are necessary for the SDK to function correctly, offering vital utilities and cryptographic operations needed for blockchain interactions.
Install the packages via npm:
npm i ethers@6.10.0 @sin7y/ola-abi-wasm @sin7y/ola-crypto
Install
To get started with the Ola JavaScript SDK, install it via npm:
npm i @sin7y/ola-js-sdk
This command adds the Ola JS SDK to your project, making it ready for use in your applications.
Initialization
After installing the required packages, you need to initialize the WebAssembly packages at the entry point of your project. This step is crucial for preparing the ABI and cryptographic functionalities for use.
Initializing @sin7y/ola-abi-wasm
@sin7y/ola-abi-wasm
import { init as initOlaAbiWasm } from '@sin7y/ola-abi-wasm';
// Initialize the ABI WASM module
initOlaAbiWasm().then(() => {
console.log('@sin7y/ola-abi-wasm initialized successfully.');
}).catch(error => {
console.error('Failed to initialize @sin7y/ola-abi-wasm:', error);
});
Initializing @sin7y/ola-crypto
@sin7y/ola-crypto
import { init as initOlaCrypto } from '@sin7y/ola-crypto';
// Initialize the Crypto WASM module
initOlaCrypto().then(() => {
console.log('@sin7y/ola-crypto initialized successfully.');
}).catch(error => {
console.error('Failed to initialize @sin7y/ola-crypto:', error);
});
Make sure these initialization steps are performed before you use any functionality provided by the Ola JavaScript SDK. This ensures that all necessary modules are loaded and ready to execute their respective operations.
Features
The Ola JavaScript SDK supports the following key functionalities:
setPubkey: This method is used for the initial registration of an account on the Ola blockchain. It is a crucial step for new users to start interacting with the blockchain.
invoke: Use this method to send transactions on the Ola blockchain. It enables the execution of various operations, such as transferring assets or interacting with smart contracts.
call: This method is designed for querying contract data. It allows read-only access to the state of smart contracts, making it possible to retrieve information without submitting a transaction.
Poseidon Hash: The Ola JavaScript SDK includes support for the Poseidon hash function, a highly efficient and secure hash function designed for use in zero-knowledge proofs and other blockchain-related cryptographic operations. This addition makes it easy for developers to compute hashes directly within their JavaScript or Node.js applications.
Here's a quick overview of how to use the Ola JS SDK in your project:
Generating an OlaWallet
You can generate an OlaWallet
using an Ethereum private key as follows:
async function generateAccount() {
const ethPrivateKey = "your_ethereum_private_key";
const ethWallet = new ethers.Wallet(ethPrivateKey);
const chainId = 1027;
const olaWallet = await OlaWallet.fromETHSignature(ethWallet);
olaWallet.connect("https://pre-alpha-api.olavm.com:443", chainId);
return olaWallet;
}
Replace your_ethereum_private_key
with your actual Ethereum private key.
Using OlaWallet for Blockchain Operations
The OlaWallet
instance can be used to perform various operations on the Ola blockchain, including account registration, transaction sending, and contract data querying.
Registering an Account
Use the setPubKey
method to register a new account on the Ola blockchain:
// Example
const olaWallet = await generateAccount();
const txHash = await olaWallet.setPubKey();
console.log("Transaction Hash:", txHash);
Sending Transactions
To send transactions, use the invoke
method:
// Example
const contracAddress = "your_contract_address";
const abi = [{ name: "set", type: "function", inputs: [{ name: "d", type: "u32" }], outputs: [] }];
const params = [{ U32: 2000 }];
const txHash = await olaWallet.invoke(abi, "set(u32)", contracAddress, params);
console.log("Invoke Transaction Hash:", txHash);
The invoke
method is used to send transactions that interact with smart contracts.
async invoke(
abi: Array<any>,
method: string,
to: string,
params: Array<any>,
options?: { nonce: number }
): string
Here's a breakdown of its parameters:
abi
: This represents the Application Binary Interface (ABI) of the contract. The ABI is a JSON-formatted array defining how to serialize and deserialize data to and from the blockchain. It allows the SDK to understand the contract's structure, including its methods and types.method
: This is the function signature within the contract you wish to call. It specifies which function of the smart contract will be invoked.to: This is the destnation contract address.
params
: These are the parameters to pass to the function being called. The parameters should match the expected types defined in the contract's ABI.options: Developers can use custom nonce on their purpose, if
null
, OlaWallet would usegetNonce
api to query the nonce of the address.Return Value: The
invoke
method returns a transaction hash (txHash
). This hash can be used to track the transaction's status on the blockchain.
Querying Contract Data
For querying contract data, the call
method is used:
// Example
const abi = [{ name: "get", type: "function", inputs: [], outputs: [{ name: "", type: "u32" }] }];
let result = await olaWallet.call<number>(abi, "get()", contracAddress, []);
console.log("Query Result:", result);
The call
method is used for querying data from smart contracts without creating a transaction. This is particularly useful for reading the current state or variables within a contract.
async call<T>(
abi: Array<any>,
method: string,
to: string,
params: Array<any>
): Promise<T>
The abi
, method
, to
, params
are same as invoke
's. Only the result value is different.
Return Value: Unlike
invoke
andsetPubKey
, the return value ofcall
is the actual data retrieved from the smart contract function call. The data type of the return value will match the expected return type defined in the contract's ABI.##
Utilizing Poseidon Hash
The Poseidon hash function can be directly accessed through the SDK. Here's a simple guide on how to use it for hashing data:
import { poseidonHash } from '@sin7y/ola-js-sdk';
async function computeHash(data) {
// Assuming `data` is an Uint8Array
const hash = poseidonHash(data);
return hash;
}
// Example usage
const dataToHash = Uint8Array.from([1, 2]); // Example data
computeHash(dataToHash)
.then(hash => console.log("Computed Hash:", hash))
.catch(error => console.error("Error computing hash:", error));
In this example, poseidonHash
is a function that takes an Uint8Array
as its input. This array represents the data you wish to hash. The function then computes and returns the hash as a Uint8Array
of 32 length. This makes it suitable for cryptographic applications where security and efficiency are paramount.
Last updated