hear-verse
v0.0.20
Published
HearVerse is a Javascript library that provides an easy-to-use interface for interacting with a HearVerse Chain
Downloads
29
Maintainers
Readme
HearVerse
HearVerse is a Javascript library that provides an easy-to-use interface for interacting with a HearVerse Chain
Installation
npm i hear-verse
Usage
const HearVerse = require('hear-verse'); // adjust path to HearVerse file if necessary
const fs = require('fs');
const path = require('path');
const addressFilePath = path.join(__dirname, 'address.txt');
// replace this with your actual endpoint
const endpoint = 'ws://127.0.0.1:9944';
const api = new HearVerse(endpoint);
(async () => {
await api.connect();
async function getAccount() {
if (fs.existsSync(addressFilePath)) {
// If the file exists, read the address from it
const address = fs.readFileSync(addressFilePath, 'utf8');
return JSON.parse(address);
} else {
// If the file does not exist, create a new account
const newAccount = await api.createAccount();
// Write the address of the new account to the file
fs.writeFileSync(addressFilePath, JSON.stringify(newAccount, null, 2));
return newAccount;
}
}
// Use the function like this:
const account = await getAccount();
// get balance
const balance = await api.getBalance(account.address);
console.log(`Balance of new account: ${balance}`);
// get latest block
const latestBlock = await api.getLatestBlock();
// handling events
const eventCallback = (eventData) => {
console.log('Event data:', eventData);
};
const collectionDetails = "kianoush"; // provide a valid collectionDetails object
let crate_collection = await api.createCollection(account, collectionDetails);
const newAccounta = await api.createAccount();
const newAccountb = await api.createAccount();
const newAccountc = await api.createAccount();
let set_config = await api.setConfig(account, 5, 5);
console.log(set_config)
const nftDetails = {
collectionId: crate_collection.result,
metadata: "kianoush",
royalty: 10,
share_profits: [
{ percentage: 50, owner_address: newAccounta.address },
{ percentage: 30, owner_address: newAccountb.address },
{ percentage: 20, owner_address: newAccountc.address },
],
price: 1000000000000000n,
end_date: 1000000000000000n,
};
let mint_nft = await api.mintNft(account, nftDetails);
console.log(mint_nft)
const albumDetails = {
collectionId: crate_collection.result,
metadata: "My Album",
royalty: 10,
tracks: [
{
track_id: null,
metadata: "Track 1",
owners: [],
share_profits: [
{ percentage: 50, owner_address: newAccounta.address },
{ percentage: 25, owner_address: newAccountb.address },
{ percentage: 25, owner_address: newAccountc.address },
],
price: 1,
},
{
track_id: null,
metadata: "Track 2",
owners: [],
share_profits: [
{ percentage: 50, owner_address: newAccounta.address },
{ percentage: 25, owner_address: newAccountb.address },
{ percentage: 25, owner_address: newAccountc.address },
],
price: 1,
},
],
endDate: 1,
};
let mint_album = await api.mintAlbum(account, albumDetails);
console.log(mint_album)
let buy_album = await api.buyAlbum(account, Number(mint_album.result.collectionId), Number(mint_album.result.albumId), 1);
console.log(buy_album)
const updatedShareProfits = [
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 95, owner_address: newAccounta.address },
];
let update_share_nft = await api.updateShareProfitNft(account, Number(mint_nft.result.collectionId), Number(mint_nft.result.nft_id), updatedShareProfits);
console.log('up', update_share_nft)
const updatedShareProfitsAlbum = [
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 1, owner_address: newAccounta.address },
{ percentage: 95, owner_address: newAccounta.address },
];
let update_share_album = await api.updateShareProfitAlbum(account, Number(mint_album.result.collectionId), Number(mint_album.result.albumId), Number(2), updatedShareProfitsAlbum);
console.log('up', update_share_album)
let buy_nft = await api.buyNFT(account, Number(mint_nft.result.collectionId), Number(mint_nft.result.nft_id), 1);
console.log(buy_nft)
})().catch(console.error);
Transfer Token
const hearVerse = require("hear-verse");
async function main() {
const hv = new hearVerse('ws://server-address');
await hv.connect();
const newAccount = hv.createAccount();
console.log('New account:', newAccount);
// Transfer native tokens example
const toAddress = '5GJ1p...'; // Replace with the recipient's address
const amount = 1000000000000; // Replace with the desired transfer amount
try {
const result = await hv.transfer(newAccount, toAddress, amount);
console.log('Transaction successful, included in block hash:', result.blockHash);
} catch (error) {
console.error('Transaction failed:', error.message);
}
}
main().catch(console.error);
In the example above, we added the usage of the transfer function to transfer native tokens. Replace the sample toAddress and amount with your desired values.