Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phantasma-io/phantasma-node-examples
Phantasma Node / JS / TS code example to interact with the Phantasma Blockchain.
https://github.com/phantasma-io/phantasma-node-examples
Last synced: about 2 months ago
JSON representation
Phantasma Node / JS / TS code example to interact with the Phantasma Blockchain.
- Host: GitHub
- URL: https://github.com/phantasma-io/phantasma-node-examples
- Owner: phantasma-io
- License: mit
- Created: 2024-03-11T11:12:49.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-03-11T11:13:03.000Z (10 months ago)
- Last Synced: 2024-05-01T00:44:45.333Z (8 months ago)
- Language: TypeScript
- Homepage:
- Size: 34.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](#prerequisites)
- [Setup](#setup)
- [Examples](#examples)
- [Get User Balances](#get-user-balances)
- [Decode a Transaction Event Data](#decode-a-transaction-event-data)
- [Make a Transaction](#make-a-transaction)
- [Sign a Transaction](#sign-a-transaction)
- [Send a Transaction](#send-a-transaction)
- [Get a Transaction](#get-a-transaction)
- [Get WIF from Private Key](#get-wif-from-private-key)
- [Generate a Wallet from WIF](#generate-a-wallet-from-wif)
- [Generate a Wallet from Private Key](#generate-a-wallet-from-private-key)
- [Contributing](#contributing)
- [License](#license)## 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:
```sh
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.
```js
// 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.
```js
// Usage
DecodeTransactionTransferEventData("data field from the event");
```### Make a Transaction
Illustrates how to create a transaction.
```js
// Usage
MakeATransaction();
```### Sign a Transaction
Shows how to sign a transaction with a given WIF.
```js
// Usage
SignATransaction(yourTransactionObject);
```### Send a Transaction
Describes the process of sending a signed transaction to the Phantasma network.
```js
// Usage
SendATransaction(yourSignedTransactionObject);
```### Get a Transaction
Explains how to retrieve the details of a transaction using its hash.
```js
// Usage
GetATransaction("your-transaction-hash-here");
```### Get WIF from Private Key
Converts a private key into a Wallet Import Format (WIF) string.
```js
// Usage
GetWifFromPrivateKey("your-private-key-here");
```### Generate a Wallet from WIF
Generates wallet details from a WIF string.
```js
// Usage
GenerateAWalletFromWIF("your-wif-here");
```### Generate a Wallet from Private Key
Creates a wallet using a given private key.
```js
// 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.
```js
// 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.
```js
// Usage
CheckForNewBlocks();
```## Contributing
Contributions to the `phantasma-node-examples` repository are welcome. To contribute, please follow the instructions in [CONTRIBUTING.md](CONTRIBUTING.md).
## License
This project is licensed under the [MIT License](LICENSE). See the [LICENSE](LICENSE) file for details.