How to transfer tokenized fiat to other accounts using publicmint-web3.js
Transactions in the Public Mint blockchain are quite similar to Ethereum. All transactions take place through ERC-20-like smart contracts that handle the tokenised fiat. This means that checking balances, sending or receiving fiat require calls to that fiat currency's smart contract, with the default being USD.
Within the code, Public Mint uses the same monetary units as Ethereum when referring to its native tokens - Wei and Ether. Even though the name is the same, for all effects these units represent tokenized fiat in the Public Mint ecosystem.
Warning
Please DO NOT send funds into any of the addresses shown in this documentation as they are published on the internet, taken from the web3.js documentation. Doing so may result in the loss of your funds!
Creating a wallet
If you haven't created a Public Mint Web Wallet and don't have your own, then the first thing you need is a wallet. Start by connecting to a node - check the previous section to get the code.
After that, it's simple - instantiate a wallet and add the private key of your funded account:
import PublicMint from'@publicmint/publicmint-web3';constweb3=newPublicMint();// Instantiate walletconst {wallet} =web3.pm// Add your first account// 0x4389Af2E0515dDFe3453B1bD748aDfD5e2598cFdconstwalletAccountPrivateKey1="0xba6cdfcc795484a9774eb98e756983da822ef3481b37d4ba649864e2d1ab4e5e";wallet.add(walletAccountPrivateKey1);constgetAccountsRes=pm.wallet.accounts.getAccounts;// [// '0x4389Af2E0515dDFe3453B1bD748aDfD5e2598cFd'// ]
And that's it - keep your keys safe and don't show anyone.
Checking your balance
Let's use web3 instance to check the balance of a Public Mint address.
Please note
Just like Ethereum, transactions in the Public Mint blockchain can either read or write to the blockchain. While read-type transactions are free, writing (i.e. changing the state) entails fees. Ethereum fees are paid in ETH. In contrast, all fees on Public Mint are paid in fiat.
The ERC20 token balance is mapped directly against the native token - for all effects, they are the same. While you can use the web3 wallet methodbalanceOf, for performance reasons, we strongly recommend you use the JSON-RPC getBalances to do it.
After instantiating web3 and create a connection to a Public Mint node, instantiate a local version of the ERC20_USD contract:
Then all you need to do is call the contract's getBalances method for your wallet's address.
// Making web3 JSON-RPC getBalance method:web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log);>"1000000000000"// Alternatively, you can also use:constmyBalance=awaitweb3.pm.wallet.accounts.getBalance("address in wallet");// To get balances of all accounts in wallet:constmyBalances=awaitweb3.pm.wallet.accounts.getBalances;/* { '0x4389Af2E0515dDFe3453B1bD748aDfD5e2598cFd': '0', '0xd2FA48924906e00069E81aaf778c4f586b5CA58B': '0' } */
Sending Funds
Here is what you need to do to send funds to another Public Mint address.
After getting a wallet and checking your balance to make sure everything is working, you can start transacting.
Here's the full code snippet to get you through all the steps:
// Instantiate: web3constPublicMint from '@publicmint/publicmint-web3';constweb3=newPublicMint(2019); // 'testNet'// Your walletconst {wallet} =web3.pm// Method to convert USD to native token unitsconst {toToken} =web3.pm.utils;// 0x4389Af2E0515dDFe3453B1bD748aDfD5e2598cFdconstwalletAccountPrivateKey1="0xba6cdfcc795484a9774eb98e756983da822ef3481b37d4ba649864e2d1ab4e5e";wallet.add(walletAccountPrivateKey1);constdestinationAccount='0x994e6a8c5F4c15FD004B6DAcb305cC1213C5503B';// 10 USD will be converted to small unit using toToken functionconsttransferReceipt=awaitweb3.pm.wallet.accounts.transfer(destinationAccount,toToken(10))//
And that's it. To make sure it worked, you should check the balance on both accounts as described above.
More transactions
For a comprehensive list of transactions with examples, please check the web3.js documentation's list of methods.