tbc
Javascript Turing BC library.
To get started, just npm install tbc-lib-js
.
Please see ./doc for documents details.
A transaction construct demo program:
const Transaction = require('tbc-lib-js/lib/transaction/transaction');
const PrivateKey = require('tbc-lib-js/lib/privatekey');
const Address = require('tbc-lib-js/lib/address');
const Script = require('tbc-lib-js/lib/script');
const Signature = require('tbc-lib-js/lib/crypto/signature'); // Import the Signature module
const Mnemonic = require('tbc-lib-js/lib/mnemonic/mnemonic');
const PublicKey = require('tbc-lib-js/lib/publickey');
// generate mnemonic
//const mnemonic = new Mnemonic();
const mnemonic = Mnemonic.fromString(
"word word word word word word word word word word word word",
Mnemonic.Words.ENGLISH
);
// get HDPrivateKey from mnemonic
const HDPrivateKey = mnemonic.toHDPrivateKey('','mainnet');
// create private key from seed with compressed format
// will sign the transaction with this private key
const DerivationPath = "m/44'/236'/0'/1/0";
const derivedHDPrivateKey =HDPrivateKey.deriveChild(DerivationPath);
const privateKey = derivedHDPrivateKey.privateKey;
// get public key from private key
const publicKey = privateKey.toPublicKey();
// get WIF private key
const wif = privateKey.toWIF();
// get address from private key
const address = privateKey.toAddress();
// print results
console.log('private key:', privateKey.toString());
console.log('public key:', publicKey.toString());
console.log('WIF private key (compressed):', wif);
console.log('mnemonic:', mnemonic.phrase);
console.log('address:', address.toString());
// Create a new transaction
const transaction = new Transaction();
// Add the first input
const utxo1 = {
txId: '<txid>',
outputIndex: 0,
script: '<lockingScript>',
satoshis: 50000000
};
transaction.from(utxo1);
//Add the second input
// const utxo2 = {
// txId: '<txid>',
// outputIndex: 1,
// script: '<lockingScript>',
// satoshis: 50000000
// };
// transaction.from(utxo2);
// Set the output
const toAddress = new Address('1Jb...');
const amount = 40000000;
transaction.to(toAddress, amount);
// Set the change address
const changeAddress = new Address('1B2...');
transaction.change(changeAddress);
// Set the fee
const fee = 1000;
transaction.fee(fee);
// Explicitly sign each input
transaction.sign(privatekey, Signature.SIGHASH_ALL | Signature.SIGHASH_FORKID);
//transaction.sign(privateKey2, Signature.SIGHASH_ALL | Signature.SIGHASH_FORKID);
// Serialize the transaction
const serializedTransaction = transaction.serialize();
console.log('Serialized Transaction:', serializedTransaction);
Create FT
FT Issuance on Turing Blockchain using tbc-lib-js
This document provides a step-by-step guide for issuing an FT (Fungible Token) on the Turing blockchain using the tbc-lib-js
library. The guide is beginner-friendly and covers key concepts such as setting up a wallet, creating a transaction, and issuing an FT.
To get started, install the library using the following command:(Note:Download the @types/node package corresponding to your Node.js)
npm i tbc-lib-js
npm i --save-dev @types/node
FT Issuance Program
The following program demonstrates how to issue an FT using a UTXO (Unspent Transaction Output) on the Turing blockchain. It includes creating a new transaction, adding inputs and outputs, signing the transaction, and retrieving the serialized transaction hex.
import * as tbc from 'tbc-lib-js';
import { FT } from 'tbc-lib-js'
const privateKeyA = tbc.PrivateKey.fromString('L1u2TmR7hMMMSV9Bx2Lyt3sujbboqEFqnKygnPRnQERhKB4qptuK');
const publicKeyA = tbc.PublicKey.fromPrivateKey(privateKeyA);
const addressA = tbc.Address.fromPrivateKey(privateKeyA).toString();
const addressB = '1FhSD1YezTXbdRGWzNbNvUj6qeKQ6gZDMq'
const ftName = 'test_usdt';
const ftSymbol = 'test_usdt';
const ftDecimal = 6;
const ftAmount = 210000000;
async function main() {
try {
const newToken = new FT({
name: ftName,
symbol: ftSymbol,
amount: ftAmount,
decimal: ftDecimal
});
const mintTX = await newToken.MintFT(privateKeyA, addressA);
await newToken.broadcastTXraw(mintTX);
const Token = new FT('ee8d97e5953a6843c3269a7ce3ae4c5264b7af8539fa07764a7f0cf260bf5eb5');
await Token.initialize();
const transferTX = await Token.transfer(privateKeyA, addressB, 0.02);
await Token.broadcastTXraw(transferTX);
} catch (error) {
console.error('Error:', error);
}
}
main();
Explanation: UTXO refers to the output of P2PKH, which provides fees for transactions. Fttxo refers to the output of storing ft contract code.
The FT SDK only provides basic UTXO retrieval, which means adding only one UTXO and FTTXO for transactions. To better build transactions, developers are advised to learn how to manage UTXO locally. If there is insufficient transaction fee or FT amount, please try checking the balance from the API. If the balance is sufficient, you can manually add multiple UTXO or FTTXO.
Note:When manually adding, ensure that the utxo input is after the fttxo input.
poolNFT
import * as tbc from 'tbc-lib-js';
import { poolNFT } from 'tbc-lib-js';
const privateKeyA = tbc.PrivateKey.fromString('L1u2TmR7hMMMSV9Bx2Lyt3sujbboqEFqnKygnPRnQERhKB4qptuK');
const publicKeyA = tbc.PublicKey.fromPrivateKey(privateKeyA);
const addressA = tbc.Address.fromPrivateKey(privateKeyA).toString();
async function main() {
try {
const pool = new poolNFT();
await pool.initCreate('80e056fe24e90ff4ef849eca33047243e27ebada13eea695db0d660726fec2ed');
const tx1 = await pool.createPoolNFT(privateKeyA);
await pool.broadcastTXraw(tx1);
const poolNftContractId = 'a17cfb4c11560c38d54e4ffaa24b94c7bee39bbc929f13e46db9bc69403846ce';
const poolUse = new poolNFT(poolNftContractId);
await poolUse.initfromContractId();
let tbc = 30;
let fta = 1000;
let tx2 = await poolUse.initPoolNFT(privateKeyA, addressA, lp, tbc, fta);
await poolUse.broadcastTXraw(tx2);
let tbc = 0.1;
const tx3 = await poolUse.increaseLP(privateKeyA, addressA, tbc);
await poolUse.broadcastTXraw(tx3);
let lp = 2;
const tx4 = await poolUse.consumLP(privateKeyA, addressA, lp);
await poolUse.broadcastTXraw(tx4);
let fta = 100;
const tx5 = await poolUse.swaptoToken(privateKeyA, addressA, fta);
await poolUse.broadcastTXraw(tx5);
let tbc = 0.1;
const tx6 = await poolUse.swaptoTBC(privateKeyA, addressA, tbc);
await poolUse.broadcastTXraw(tx6);
} catch (error) {
console.error('Error:', error);
}
}
main();