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:
Install
To get started with the Ola JavaScript SDK, install it via npm:
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
Initializing @sin7y/ola-crypto
@sin7y/ola-crypto
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:
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:
Sending Transactions
To send transactions, use the invoke
method:
The invoke
method is used to send transactions that interact with smart contracts.
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:
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.
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:
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