Skip to content

Latest commit

 

History

History
148 lines (100 loc) · 3.78 KB

README.md

File metadata and controls

148 lines (100 loc) · 3.78 KB

Phantasma Node Examples

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.

Table of Contents

Prerequisites

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.

Setup

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

Examples

Each example is a standalone script that illustrates a specific functionality of the Phantasma blockchain.

Get User Balances

This example demonstrates how to retrieve the balance of a specific account on the Phantasma blockchain.

// Usage
GetUserBalance("your-phantasma-address-here");

Decode a Transaction Event Data

Shows how to decode a transaction event data for a TokenSend and a TokenReceive event.

// Usage
DecodeTransactionTransferEventData("data field from the event");

Make a Transaction

Illustrates how to create a transaction.

// Usage
MakeATransaction();

Sign a Transaction

Shows how to sign a transaction with a given WIF.

// Usage
SignATransaction(yourTransactionObject);

Send a Transaction

Describes the process of sending a signed transaction to the Phantasma network.

// Usage
SendATransaction(yourSignedTransactionObject);

Get a Transaction

Explains how to retrieve the details of a transaction using its hash.

// Usage
GetATransaction("your-transaction-hash-here");

Get WIF from Private Key

Converts a private key into a Wallet Import Format (WIF) string.

// Usage
GetWifFromPrivateKey("your-private-key-here");

Generate a Wallet from WIF

Generates wallet details from a WIF string.

// Usage
GenerateAWalletFromWIF("your-wif-here");

Generate a Wallet from Private Key

Creates a wallet using a given private key.

// Usage
GenerateAWalletFromPrivateKey("your-private-key-here");

Get Block by Height

This example demonstrates how to retrieve the Block by the height on the Phantasma Blockchain.

// Usage
GetBlockHeight(10);

Check for new Blocks

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();

Contributing

Contributions to the phantasma-node-examples repository are welcome. To contribute, please follow the instructions in CONTRIBUTING.md.

License

This project is licensed under the MIT License. See the LICENSE file for details.