mainnet.cash - Getting started

23 435
Avatar for pat
Written by
3 years ago

mainnet.cash is a library which allows you to interact with the BitcoinCash network. It aims to simplify the BCH development substantially, allowing new business ideas to be implemented swiftly. Our core features are available in the browser or in the node.js environment. Our REST server allows for an easy integration with other programming languages.

We’ve implemented the most convenient functionality of a crypto wallet like working with network transactions, sending and receiving funds. However, you can treat mainnet.cash as an “umbrella” library which aims to support the most popular and emerging BCH technologies. Among these are: SLP tokens, HD and multi-signature wallets, encrypted messaging, smart contracts in bitcoin script and cashscript, decentralized exchanges, anyhedge and much more!

So let’s get started using mainnet.cash with Javascript and build a simple wallet application, shall we?

For this clone the following repository https://github.com/mainnet-cash/mainnet-tutorial.git and open the 1.tutorial.html file.

Or simply paste this text in a new file with .html extension:

<html>
  <head>
    <meta charset="utf-8">
    <script src="https://rest-unstable.mainnet.cash/scripts/mainnet.js"></script>
  </head>
  <body>
  </body>

  <script>
    async function run() {
    }

    run();
  </script>
</html>

This is a basic html page with mainnet.cash library imported.

Now lets add the UI elements to the webpage and paste the following text inside the <body> tag:

<div style="display: flex; flex-direction: row;">
  <div style="margin: 30px; flex: 50%;">
    <h1>Bitcoin cash</h1>
    <div>Wallet balance: <span id="balance"></span></div>
    <div>Wallet balance USD: <span id="balanceUSD"></span></div>
    <div>Deposit address: <span id="depositAddr"></span></div>
    <div>Deposit QR: <img id="depositQr"/></div>
    <div>
      <input type="button" id="getSats" value="Get testnet satoshi"></input>
    </div>
    <div>
      Send to address
      <input id="sendAddr"></input>
      <input type="button" id="send" value="Send"></input>
      <input type="button" id="sendMax" value="Send All Funds"></input>
    </div>
  </div>
</div>

This markup will render the basic UI elements of the wallet: balance, balance in USD, deposit address, deposit address QR code, some buttons for getting the testnet satoshi and sending them around.

Let's animate our webpage and make our wallet functional.

First things first - we should create the wallet object.

const wallet = await TestNetWallet.newRandom();

The wallet variable will now provide the wallet functionality on the test network, where BCH is worthless.

Now we can access the full functionality of the mainnet.cash library. Let's get the wallet balance in satoshi and USD and update the webpage UI.

const balance = await wallet.getBalance();
document.querySelector('#balance').innerText = `${balance.sat} testnet satoshi`;
document.querySelector('#balanceUSD').innerText = `$${balance.usd}`;

Easy, isn't it? We hide the complexity of interacting with the network, counting the unspent transaction outputs (UTXO), calculating the fee amount and converting the sats to USD from you. We want to enable you to concentrate on the business-logic.

To watch for balance updates we set up a callback:

wallet.watchBalance((balance) => {
 document.querySelector('#balance').innerText = `${balance.sat} testnet satoshi`;
  document.querySelector('#balanceUSD').innerText = `$${balance.usd}`;
});

The code within curly brackets is identical to one shown above. It will be evaluated each time our wallet receives or sends funds.

Now let's get our wallet's deposit address and QR code image containing it and update the UI:

const addr = await wallet.getDepositAddress();
document.querySelector('#depositAddr').innerText = addr;

const qr = await wallet.getDepositQr();
document.querySelector('#depositQr').src = qr.src; 

mainnet.cash hosts a testnet faucet which will refill our wallet's address up to 10000 satoshi.

document.querySelector('#getSats').addEventListener("click", async () => {
  await wallet.getTestnetSatoshis();
});

Let's claim them by clicking on the Get testnet satoshi button.

Pay attention to the wallet's real-time balance update.

Now we have 10000 satoshi which should worth $0.05 USD on the BCH main network.

Lastly let's program the Send and Send All Funds buttons:

document.querySelector('#send').addEventListener("click", async () => {
  const addr = document.querySelector('#sendAddr').value;
  await wallet.send([{cashaddr: addr, value: 1000, unit: "sat"}]);
  alert('Sent 1000 sats to ' + addr);
});

document.querySelector('#sendMax').addEventListener("click", async () => {
  const addr = document.querySelector('#sendAddr').value;
  await wallet.sendMax(addr);
  alert('Sent all funds to ' + addr);
});

Try sending the funds to yourself, or open a new browser window and send them to another random wallet.

This concludes the first chapter in our tutorial series. Next time we will add the SLP token support to our wallet. Be sure to check out our web site and library documentation. You can ask any questions in our telegram group we have created recently.

Cheers,

pat for mainnet.cash team.

73
$ 5.09
$ 2.73 from @TheRandomRewarder
$ 2.00 from @Mr-Zwets
$ 0.25 from @lerkfrend
+ 2
Avatar for pat
Written by
3 years ago

Comments

Nice

$ 0.00
3 years ago

Are you a developer? We're looking for devs to hire

$ 0.00
3 years ago

Thanks for this. I'm gonna try this later. 😊

$ 0.00
3 years ago

I have already tried it and it works very well.

$ 0.00
3 years ago

Yeah, I think, you are our earliest adopter. Thanks!

$ 0.00
User's avatar pat
3 years ago

Unfortunately, I found that through my outgoing transaction, from an address where I had SLP token, I burned my tokens. ☹️

$ 0.00
3 years ago

I'd like to remind you of my question on the github to provide as much info as you can about this.

$ 0.00
User's avatar pat
3 years ago

I will try to do better in the future. I am not used to using GitHub.

$ 0.00
3 years ago

Hi

$ 0.00
3 years ago

Great

$ 0.00
3 years ago

This is the best Article Today that i ever read 📖

$ 0.00
3 years ago

its very heplfull and thankfull article for everyone.

$ 0.00
3 years ago

🤚

$ 0.00
3 years ago

Hold on, what happened to the other account about mainnet when it was getting Flipstarters?

$ 0.00
3 years ago

This account is still around. Maybe I should've used it for these publications.

$ 0.00
User's avatar pat
3 years ago

Nice Article

$ 0.00
3 years ago

i love you article

$ 0.00
3 years ago

Looking forward to another learnings my friend! Have a great Day!

$ 0.00
3 years ago

Me too I'm just new here and interested to many articles

$ 0.00
3 years ago

How

$ 0.00
3 years ago

I look forward to your next tutorial friend, good article

$ 0.00
3 years ago

This is really interesting and hoping many take advantage of this and implement some cool and amazing features for all of us to enjoy :)

$ 0.00
3 years ago

Tnx

$ 0.00
3 years ago