This repository contains a collection of examples demonstrating how to integrate with the Phantasma blockchain using the phantasma-ts
library in a Node.js / TypeScript environment. These examples are designed to help developers understand the basics of Phantasma blockchain operations including account balance retrieval, transaction creation, signing, and sending, as well as wallet generation and management.
Before you begin, ensure you have met the following requirements:
- You have installed Node.js and npm.
- You are familiar with TypeScript and Node.js environments.
Clone the repository and install the dependencies:
git clone https://github.com/phantasma-io/phantasma-node-examples.git
cd phantasma-node-examples
npm install # Or bun install
Each example is a standalone script that illustrates a specific functionality of the Phantasma blockchain.
This example demonstrates how to retrieve the balance of a specific account on the Phantasma blockchain.
// Usage
GetUserBalance("your-phantasma-address-here");
Shows how to decode a transaction event data for a TokenSend and a TokenReceive event.
// Usage
DecodeTransactionTransferEventData("data field from the event");
Illustrates how to create a transaction.
// Usage
MakeATransaction();
Shows how to sign a transaction with a given WIF.
// Usage
SignATransaction(yourTransactionObject);
Describes the process of sending a signed transaction to the Phantasma network.
// Usage
SendATransaction(yourSignedTransactionObject);
Explains how to retrieve the details of a transaction using its hash.
// Usage
GetATransaction("your-transaction-hash-here");
Converts a private key into a Wallet Import Format (WIF) string.
// Usage
GetWifFromPrivateKey("your-private-key-here");
Generates wallet details from a WIF string.
// Usage
GenerateAWalletFromWIF("your-wif-here");
Creates a wallet using a given private key.
// Usage
GenerateAWalletFromPrivateKey("your-private-key-here");
This example demonstrates how to retrieve the Block by the height on the Phantasma Blockchain.
// Usage
GetBlockHeight(10);
This function continuously monitors the blockchain for new blocks. When it detects that the height of the chain has increased, it fetches the latest block and processes each transaction within it, specifically looking for "TokenReceive" events. When such an event is found, it triggers a predefined action.
// Usage
CheckForNewBlocks();
Contributions to the phantasma-node-examples
repository are welcome. To contribute, please follow the instructions in CONTRIBUTING.md.
This project is licensed under the MIT License. See the LICENSE file for details.