Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion step21_web3_node_getbalance/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Node packages
node_modules
*.js

# Build files created by TypeScript Compiler
*.js
48 changes: 31 additions & 17 deletions step21_web3_node_getbalance/EthereumAccount.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import Web3 from 'web3';
import Web3 from "web3";

// Create a new project on Infura and replace PROJECT_ID
// in the following line with your Infura project id.
const RPC_ENDPOINT = "https://mainnet.infura.io/v3/PROJECT_ID";

//https://www.typescriptlang.org/docs/handbook/2/classes.html
export class EthereumAccount {
private web3: Web3;
private address: string;

private web3: Web3;
private address: string;

// Got an Default Account Address from Etherscan: https://etherscan.io/accounts
public constructor(address: string = "0xDeE6238780f98c0ca2c2C28453149bEA49a3Abc9") {
this.address = address;
this.web3 = new Web3("https://mainnet.infura.io/v3/b56ef5c27bca4880844888e6a12a16ef");

}
/**
* Instantiate an Ethereum Account object.
* @param address Public address of the account on Ethereum Network.
*/
public constructor(address: string) {
this.address = address;
this.web3 = new Web3(RPC_ENDPOINT);
}

public getAddress(): string {
return this.address;
}
/**
* Get the public address of the Ethereum account.
* @returns public address of the Ethereum account.
*/
public getAddress(): string {
return this.address;
}

public async getBalance() {
return await this.web3.eth.getBalance(this.address);
}
}
/**
* Get the number of Ethers in the Ethereum account.
* @returns Ethers (ETH) in the Ethereum account.
*/
public async getBalance() {
const balance = await this.web3.eth.getBalance(this.address);
return this.web3.utils.fromWei(balance, "ether");
}
}
18 changes: 14 additions & 4 deletions step21_web3_node_getbalance/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { EthereumAccount } from './EthereumAccount';
import { EthereumAccount } from "./EthereumAccount";

const account : EthereumAccount = new EthereumAccount();

account.getBalance().then(response => console.log(response));
const numberFormator = new Intl.NumberFormat("us-EN", {
style: "currency",
currency: "ETH",
});

// Get any Account Address from Etherscan: https://etherscan.io/accounts
const accountAddress = "0x53d284357ec70ce289d6d64134dfac8e511c8a3d";

(async () => {
// Instantiate an Ethereum account instance.
const account = new EthereumAccount(accountAddress);

console.log("Account Address:", account.getAddress());
const balance = await account.getBalance();
console.log("Account Balance:", numberFormator.format(+balance));
})();
Loading