Code Monkey home page Code Monkey logo

firma-js_old's Introduction

firma-js

Javascript SDK for FirmaChain

npm version

Install

You can install firma-js using NPM

yarn add @firmachain/firma-js

or

npm i @firmachain/firma-js

Usage

Initializing SDK

const {Firma} = require('@firmachain/firma-js');

const chainId = 'imperium-0001';
const lcdUrl = 'http://localhost:1317';

const firma = new Firma(chainId, lcdUrl);

Create account

const {Wallet} = require('@firmachain/firma-js');

const mnemonic = Wallet.generateMnemonic();
const index = 0;

const wallet = Wallet.fromMnemonic(mnemonic, index);

console.log(wallet.accAddress);

Import account by private key

const {Wallet} = require('@firmachain/firma-js');

const wallet = Wallet.fromPrivateKey(privateKey);

console.log(wallet.accAddress);

Get current block number

firma.blockchain.getBlockNumber().then((blockNumber) => {
    console.log(blockNumber);
}).catch((e) => {
    console.error(e);
};

Get FIRMA balance of specific account

firma.account.getBalance(address).then((balance) => {
    console.log(balance);
}).catch((e) => {
    console.error(e);
};

Get transaction by hash

firma.tx.getTransactionByHash(txHash).then((tx) => {
    console.log(tx)
}).catch((e) => {
    console.error(e);
});

Create Message (MsgSend) & StdTx

const {MsgSend, StdTx} = require('@firmachain/firma-js');
const msg = new MsgSend(fromAddress, toAddress, (1 * 10 ** 6)); //send 1 FIRMA to toAddress
const stdTx = new StdTx(msg);

Calculate gas

const gasAdjustment = 2;
firma.tx.estimateGas(stdTx, gasAdjustment).then((fee) => { 
    // return Fee object
    stdTx.setFee(fee);
}).catch((e) => {
    console.log(e);
});

Sign Transaction & Broadcast Transaction

const {Wallet} = require('@firmachain/firma-js');
const wallet = Wallet.fromPrivateKey(privateKey);

firma.signStdTx(stdTx, wallet).then((tx) => {
    firma.tx.broadcast(tx).then((result) => {
      console.log('result', result);
    }).catch((e) => {
      console.log(e);
    });
}).catch((e) => {
    console.log(e);
});

Shortcut of sign transaction

firma.createAndSign(wallet, msg).then((signedTx) => {
    firma.tx.broadcast(signedTx).then(console.log)
})

Mint NFT

// MintNFT msg
// image, tokenURI, description are metadata
const msg = new MsgMintNFT("f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b", testAccount.accAddress, 'https://ipfs.infura.io:5001/api/v0/cat?arg=QmfWVHyTiVXscS4kHENQRXKD5ug1qd2UQJAE6aCX5ch2Eq', "test description", "https://ipfs.infura.io:5001/api/v0/cat?arg=QmTF7NerdGZhnDPJj3Yj51gqH18o8kLtgkgtVjMLk1V9tx");

firma.createAndSign(wallet, msg).then((signedTx) => {
    firma.tx.broadcast(signedTx).then(console.log)
}).catch(console.error)

Transfer NFT

// TransferNFT msg
const msg = new MsgTransferNFT("f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b", testAccount.accAddress, 'firma1rhfahdeh2f644f8mjeclyzp2jn9cshs0md8z5a');

firma.createAndSign(wallet, msg).then((signedTx) => {
    firma.tx.broadcast(signedTx).then(console.log)
}).catch(console.error);

Burn NFT

// BurnNFT msg
const msg = new MsgBurnNFT("f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b", testAccount.accAddress);

firma.createAndSign(wallet, msg).then((signedTx) => {
    firma.tx.broadcast(signedTx).then(console.log)
}).catch(console.error);

License

MIT See LICENSE

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.