a few things about Web3.JS

web3.js will:
We can create new accounts, we can send Ether, we can add signatures to transfers, get balances from accounts, get transactions, and transaction IDs. We can clone and we can interact with contracts, all kinds of stuff.

where will it go?
load it from metamask

OR

add it as a script to your HTML DAPP.

<head>
//load BootStrap CSS
</head>
<body>
//content
//load web3.js
//load ABI.js
//load jQuery
//load Bootstrap js

<script>
//web3 provider
</script>

</body>

The Web3.js Script part can use the following:
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    // Use Mist/MetaMask's provider
    web3js = new Web3(web3.currentProvider);
web3js.version.getNetwork((err, netId) => {
  switch (netId) {
    case "1":
      console.log('This is mainnet')
      break
    case "2":
      console.log('This is the deprecated Morden test network.')
      break
    case "3":
      console.log('This is the ropsten test network.')
      break
    case "4":
      console.log('This is the Rinkeby test network.')
      break
    case "42":
      console.log('This is the Kovan test network.')
      break
    default:
      console.log('This is an unknown network.')
  }
})
} else { console.log('No web3? You should consider trying MetaMask!') // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail) web3js = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); }


a few methods usually used

check for a valid wallet or contract Hex
var isAddress = web3js.isAddress("0x8888f1f195afa192cfee860698584c030f4c9db1");
console.log(isAddress); // true

NOTE:The above version is for web3.js version 0.2x.x. If you’re using web3.js 1.0 this is what you need to use.
web3js.utils.isAddress('0x8888f1f195afa192cfee860698584c030f4c9db1');> true

Check the balance based on a network connected
var balance = web3js.eth.getBalance("0x8888f1f195afa192cfee860698584c030f4c9db1");
console.log(balance); // instanceof BigNumber
console.log(balance.toString(10)); // '1000000000000'
console.log(balance.toNumber()); // 1000000000000

Gives a contracts balance in Wei
web3js.utils.fromWei('1', 'ether');
> "0.000000000000000001"

Comments

Popular posts from this blog

Solidity Hash of Structs (Testing hash uniqueness)

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Parity installation on Ubuntu Virtual Machine