@bethelnet/utils v1.0.3
How to Use the @bethel/utils Package
The @bethel/utils package simplifies interactions with Ink! nodes, making tasks like wallet creation, connecting to smart contracts, and more seamless.
Installation
Install the package via npm:
npm install @bethel/utilsFeatures
The @bethel/utils package provides multiple functionalities to interact with Ink! nodes, including:
- Wallet creation
- Connecting to smart contracts
- Fetching wallet balances
- Transferring funds between wallets
Usage
1. Connect to an Ink! Node
To connect to an Ink! node, you need to establish a WebSocket connection. Follow these steps:
Import the Bethel library:
import { bethel } from "@bethel/utils";Create an instance for API connection:
const bethelNode = new bethel("your-websocket-url");Connect to the API:
const connectApi = bethelNode.connect();This method creates an API connection to the Ink! node and returns a success or failure message.
2. Create a Wallet
You can create a wallet using the following method:
Syntax:
const wallet = bethelNode.createWallet("Wallet Name");- Parameters: Pass the wallet name as a string.
- Returns: The method returns the wallet's public key, private key, and additional wallet information.
3. Get Wallet Balance
Retrieve the balance of your wallet:
Syntax:
const balance = bethelNode.getBalance();- Returns: The method fetches and returns the wallet balance.
4. Wallet Transfers
Transfer funds between wallets using the following method:
Syntax:
const txHash = bethelNode.makeTransfer(recipientAddress, amount);- Parameters:
recipientAddress: The address of the recipient wallet.amount: The amount to transfer.
- Returns: The method returns the transaction hash.
5. Connect to Smart Contracts
To interact with a smart contract, you need the contract's metadata and address:
Syntax:
const contract = bethelNode.connectToContract(metaData, address);- Parameters:
metaData: The metadata of the smart contract.address: The address of the smart contract.
- Returns: The method returns the contract instance for further interactions.
Example Workflow
Below is an example workflow demonstrating key functionalities:
import { bethel } from "@bethel/utils";
// Step 1: Create an instance and connect to the node
const bethelNode = new bethel("wss://example-node-url");
const connectApi = bethelNode.connect();
if (connectApi.success) {
console.log("Connected to the node successfully!");
// Step 2: Create a wallet
const wallet = bethelNode.createWallet("MyWallet");
console.log("Wallet created:", wallet);
// Step 3: Get wallet balance
const balance = bethelNode.getBalance();
console.log("Wallet balance:", balance);
// Step 4: Transfer funds
const txHash = bethelNode.makeTransfer("recipient-address", 1000);
console.log("Transaction hash:", txHash);
// Step 5: Connect to a smart contract
const contract = bethelNode.connectToContract(metaData, "contract-address");
console.log("Connected to contract:", contract);
} else {
console.error("Failed to connect to the node:", connectApi.error);
}Notes
- Ensure you use a valid WebSocket URL for the Ink! node.
- Handle errors gracefully by checking the return values of each method.
- For more detailed documentation, refer to the official library documentation.
Start leveraging the power of the @bethel/utils package to simplify your blockchain interactio