Skip to content

A Node.js application for managing a TON Wallet, tracking blockchain transactions, and forwarding details to an API and RabbitMQ. Supports wallet creation for version v4r2.

Notifications You must be signed in to change notification settings

mohammaditor/Ton-wallet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ton Wallet Project

This project is a Node.js application designed for managing a TON Wallet. It tracks transactions on the TON blockchain for a specific wallet and forwards the transaction details to a predefined API and RabbitMQ queue. Since wallets on the TON network can have different versions, the service includes an endpoint for creating wallets according to version v4r2. It is recommended to use wallets generated by this service as the primary wallet for the pool.

Table of Contents

Project Structure

TonWallet/
├── config/
│   ├── db.js
│   └── contracts.json
├── models/
│   └── transaction.js
├── services/
│   └── tonService.js
├── utils/
│   └── api.js
├── .env
├── index.js
└── README.md

Config Files

  • config/db.js: Database connection configuration.
  • config/contracts.json: Stores token contract addresses.

Model Files

  • models/transaction.js: Mongoose model for transactions.

Service Files

  • services/tonService.js: Main service for interacting with the Ton blockchain.

Utility Files

  • utils/api.js: Utility for sending transactions to the API.

Main Files

  • .env: Environment variables configuration.
  • index.js: Main entry point for the application.

Classes and Methods

TonService

The TonService class is responsible for interacting with the Ton blockchain, handling transactions, and sending tokens.

Methods:

constructor()

Initializes the TonService instance and sets up the necessary configurations for interacting with the Ton blockchain.

init()

  • Initializes keys and wallet by retrieving keys from HashiCorp Vault.
  • Updates token addresses by calling updateTokenAddresses.
  • Starts monitoring the wallet by calling monitorWallet.
  • Connects to RabbitMQ by calling connectToRabbitMQ.

getKeysFromVault()

  • Retrieves keys (public and private) from HashiCorp Vault.
  • Throws an error if the keys cannot be retrieved.

updateTokenAddresses(accountId)

  • Fetches the token balances for a given account ID.
  • Updates the internal mapping of token addresses to token symbols.
  • Logs the loaded token addresses or exits the program if an error occurs.

monitorWallet(walletAddress)

  • Periodically retrieves transactions for the specified wallet address by calling getTransactions.
  • Processes each transaction using handleTransaction.

getTransactions(walletAddress)

  • Uses the TON API to fetch recent transactions for the specified wallet address.
  • Returns the list of transactions.

handleTransaction(tx)

  • Processes a single transaction.
  • Determines the type of transaction (TON or token) and extracts relevant details (sender, receiver, amount, memo).
  • Stores the transaction in the database if it is new.
  • Updates the status of existing transactions if necessary.
  • Sends the transaction details to the API using sendToAPI.

sendToAPI(transaction)

  • Sends the transaction details to the predefined API.
  • Also sends the transaction details to a RabbitMQ queue.
  • The data sent by this method can vary and does not have a fixed schema.
{
  "txId": "transaction-id",
  "from": "sender-address",
  "to": "receiver-address",
  "value": "transaction-value",
  "token": "token-type",
  "memo": "transaction-memo",
  "success": "transaction-status",
  "tx": "complete-transaction-object"
}
  • Marks the transaction as sent in the database upon successful API response.

sendPendingTransactionsToAPI()

  • Retrieves all pending transactions from the database.
  • Sends each pending transaction to the API by calling sendToAPI.

checkAndUpdateTransactions()

  • Periodically checks the status of transactions stored in the database.
  • Updates the status of transactions by fetching their latest state from the blockchain.
  • Ensures that each transaction is sent to the API if its status changes.

sendTon(toAddress, amount, memo)

  • Sends a specified amount of TON to a given address with an optional memo.
  • Uses the TonWeb library to create and send the transaction.
  • Logs the result of the transaction.

sendToken(toAddress, tokenContractAddress, tokenAmount, memo)

  • Sends a specified amount of tokens to a given address with an optional memo.
  • Uses the TonWeb library to create and send the token transfer transaction.
  • Logs the result of the transaction.

createWallet()

  • Creates a new wallet using the TonWeb library.
  • Returns the public and secret keys of the newly created wallet.
  • Logs the details of the created wallet.

getTransactionInfoByHash(transactionHash)

  • Fetches detailed information about a transaction using its hash.
  • Uses axios to make a request to the TON Center API.
  • Returns the transaction details if found.

connectToRabbitMQ()

  • Connects to RabbitMQ using the connection URL provided in the environment variables.
  • Asserts a queue to ensure it exists.
  • Logs the successful connection or an error if the connection fails.

sendToRabbitMQ(transaction)

  • Sends the transaction details to the RabbitMQ queue.
  • Converts the transaction object to a JSON string and sends it as a message to the queue.
  • Logs the result or an error if the message could not be sent.

API Endpoints

POST /send-ton

Sends a specified amount of TON to a given address with an optional memo.

Request Body:

{
  "toAddress": "EQCHIYHft0_Cww0jtiXqOSUVYwfmfD0pFayozSBrSQRSi02B",
  "amount": "10",
  "memo": "Transaction memo"
}

Responses:

  • 200 OK: Transaction successful.
  • 400 Bad Request: Missing required parameters.
  • 500 Internal Server Error: An error occurred on the server.

POST /send-token

Sends a specified amount of tokens to a given address with an optional memo.

Request Body:

{
  "toAddress": "EQCHIYHft0_Cww0jtiXqOSUVYwfmfD0pFayozSBrSQRSi02B",
  "tokenContractAddress": "EQD8F9y5Tk9F_S5V4iZb6VW5tW7ZR8v8ZjL4jaOi6SX_MPTA",
  "amount": "100",
  "memo": "Token transfer memo"
}

Responses:

  • 200 OK: Token transfer successful.
  • 400 Bad Request: Missing required parameters.
  • 500 Internal Server Error: An error occurred on the server.

POST /create-wallet

Creates a new wallet and returns its public and secret keys.

Responses:

  • 200 OK: Wallet created successfully.
  • 500 Internal Server Error: An error occurred on the server.

Environment Variables

The following environment variables are required for the application to run:

MONGO_URI=<your-mongodb-connection-url>
TONCENTER_API_URL=https://toncenter.com/api/v2/jsonRPC
TONCENTER_API_KEY=<your-toncenter-api-key>
TON_API_URL=https://tonapi.io
TON_API_KEY=<your-tonapi-io-api-key>
WALLET_ADDRESS=<your-wallet-address>
VAULT_ENDPOINT=<your-hashicorp-vault-endpoint>
VAULT_TOKEN=<your-hashicorp-vault-token>
VAULT_PATH=<your-hashicorp-vault-path>
API_URL=<your-endpoint> // Your endpoint URL for receiving transactions
CHECK_INTERVAL=10000 // Timer for checking the wallet
UPDATE_INTERVAL=2000 // Timer for syncing transactions with your server
LIMIT=500 // Number of transactions to fetch per wallet check
PORT=3000 // Port on which the application runs
RABBITMQ_URL=<your-rabbitmq-url>
RABBITMQ_QUEUE=<your-rabbitmq-queue-name>

To get the API key from TONCENTER, you need to visit their Telegram bot at https://t.me/tonapibot.

To get the API key from tonapi.io, you need to visit tonconsole.com.

Setup and Installation

  1. Clone the repository:

    git clone https://github.com/mohammaditor/Ton-wallet
    cd Ton-wallet
  2. Install the dependencies:

    npm install
  3. Create a .env file in the root directory with the required environment variables:

    MONGO_URI=<your-mongodb-connection-url>
    TONCENTER_API_URL=https://toncenter.com/api/v2/jsonRPC
    TONCENTER_API_KEY=<your-toncenter-api-key>
    TON_API_URL=https://tonapi.io
    TON_API_KEY=<your-tonapi-io-api-key>
    WALLET_ADDRESS=<your-wallet-address>
    VAULT_ENDPOINT=<your-hashicorp-vault-endpoint>
    VAULT_TOKEN=<your-hashicorp-vault-token>
    VAULT_PATH=<your-hashicorp-vault-path>
    API_URL=<your-endpoint>
    CHECK_INTERVAL=10000
    UPDATE_INTERVAL=2000
    LIMIT=500
    PORT=3000
    RABBITMQ_URL=<your-rabbitmq-url>
    RABBITMQ_QUEUE=<your-rabbitmq-queue-name>
    
  4. Start the application:

    node index.js
  5. Access the API endpoints using tools like Postman or curl to interact with the service.

About

A Node.js application for managing a TON Wallet, tracking blockchain transactions, and forwarding details to an API and RabbitMQ. Supports wallet creation for version v4r2.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published