LogoLogo
  • Introduction
  • Core Concepts
  • Partner setup
  • Tutorials [WIP]
    • Create Identities
    • Create a Blockchain Address
    • Payout Service
  • Public Mint Widget
    • Getting started
    • Embed Widget
      • Deposit of funds
      • Withdraw of funds
  • Public Mint API
    • Getting started
    • Creating identities
    • Connecting to the blockchain
      • Running your own client node
    • Executing Transactions
      • Depositing
      • Wallet Transactions
      • Withdrawing
    • Sandbox
    • API Reference
      • Identities
      • Operations
      • Transfer Methods
      • Transactions
      • Withdrawals (deprecated)
      • Verifications
      • Identity Relations
      • Documents
      • Deposits (deprecated)
      • Authentication
      • Pagination
      • Errors
      • Core Resources
    • Webhooks
  • Public Mint Blockchain
    • Getting started
    • Transacting
    • Configuring Metamask for the Public Mint blockchain
    • Interoperability [WIP]
  • Other Information
    • Public Mint Wallet
    • Users and Accounts
Powered by GitBook
On this page
  • Creating a wallet
  • Checking your balance
  • Sending Funds
  • More transactions

Was this helpful?

  1. Public Mint API
  2. Executing Transactions

Wallet Transactions

How to transfer tokenized fiat to other accounts using publicmint-web3.js

PreviousDepositingNextWithdrawing

Last updated 5 years ago

Was this helpful?

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.

Read on to learn your way around the basics.

What you'll need

Important

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 and don't have your own, then the first thing you need is a wallet. Start by connecting to a node - check the 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';
const web3 = new PublicMint();

// Instantiate wallet
const {
	wallet
} = web3.pm

// Add your first account
// 0x4389Af2E0515dDFe3453B1bD748aDfD5e2598cFd
const walletAccountPrivateKey1 = "0xba6cdfcc795484a9774eb98e756983da822ef3481b37d4ba649864e2d1ab4e5e";

wallet.add(walletAccountPrivateKey1);

const getAccountsRes = 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.

After instantiating web3 and create a connection to a Public Mint node, instantiate a local version of the ERC20_USD contract:


// Instantiate publicmint-web3 module

const PublicMint = require('@publicmint/publicmint-web3').default;
const web3 = new PublicMint('testNet');

const {
	USD
} = web3.pm.contracts.token

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:
const myBalance = await web3.pm.wallet.accounts.getBalance("address in wallet");

// To get balances of all accounts in wallet:
const myBalances = await web3.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: web3
const PublicMint from '@publicmint/publicmint-web3';
const web3 = new PublicMint(2019); // 'testNet'

// Your wallet
const {
	wallet
} = web3.pm

// Method to convert USD to native token units
const {
    toToken
} =  web3.pm.utils;

// 0x4389Af2E0515dDFe3453B1bD748aDfD5e2598cFd
const walletAccountPrivateKey1 = "0xba6cdfcc795484a9774eb98e756983da822ef3481b37d4ba649864e2d1ab4e5e";
wallet.add(walletAccountPrivateKey1);

const destinationAccount = '0x994e6a8c5F4c15FD004B6DAcb305cC1213C5503B';

// 10 USD will be converted to small unit using toToken function
const transferReceipt = 
  await web3.pm.wallet.accounts.transfer(destinationAccount, toToken(10))
 
// 

More transactions

The ERC20 token balance is mapped directly against the native token - for all effects, they are the same. While you can use the balanceOf, for performance reasons, we strongly recommend you use the JSON-RPC getBalances to do it.

And that's it. To make sure it worked, you should check the balance on both accounts .

For a comprehensive list of transactions with examples, please check the 's list of methods.

web3 wallet method
web3.js documentation
as described above
Node.js
Web3.js
Web Wallet
previous section