[TOC]
# Bscscan api & Web3 筆記
## [Bscscan api](https://bscscan.com/apis)
### 1. 獲取熱錢包中的 Bnb(幣安幣) 數量
- Bscscan api/account/Get BNB Balance for a single Address
- https://api.bscscan.com/api?module=account&action=balance&address=0xD05Ebe8E7BB56450d34784996Db6D22909E035Af&tag=latest&apikey=apikey
```
{
"status": "1",
"message": "OK",
"result": "25335050000000000"
}
```

### 2. 獲取熱錢包中的 Cake(幣安幣) 數量
- Bscscan api/tokens/Get BEP20-Token Account Balance for TokenContractAddress
- https://api.bscscan.com/api?module=account&action=tokenbalance&contractaddress=0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82&address=0xD05Ebe8E7BB56450d34784996Db6D22909E035Af&tag=latest&apikey=YourApiKeyToken
```
{
"status": "1",
"message": "OK",
"result": "50000000000000"
}
```
## [Web3](https://web3js.readthedocs.io/en/v1.3.4/)
Version: 1.3.3
### 1. 獲取熱錢包中的 Bnb(幣安幣) 數量
- [Bsc JSON-RPC Endpoint](https://docs.binance.org/smart-chain/developer/rpc.html)

```=javascript
import Web3 from "web3";
var web3 = new Web3('https://bsc-dataseed.binance.org/'); // 輸入 RPC 地址
// 取得 Bnb 餘額
const getBnbBalance = async () => {
let bnbBalance = await web3.eth.getBalance('0xd05ebe8e7bb56450d34784996db6d22909e035af');
console.log("bnbBalance: ", bnbBalance);
bnbBalance = web3.utils.fromWei(bnbBalance, "ether");
console.log("bnbBalance_convert: ", bnbBalance);
};
```
log:
bnbBalance: 25335050000000000
bnbBalance_convert: 0.02533505
### 2. 獲取熱錢包中的 Cake(幣安幣) 數量
- [cake contract abi](https://bscscan.com/address/0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82#code)
- 所謂的 abi 就是合約中提供了哪些方法
```=javascript
import Web3 from "web3";
import cake from "./../config/abi/cake.json";
var cakeContractAddress = '0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82'
// 調用 Cake 合約
const cakeContract = new web3.eth.Contract(cake, cakeContractAddress);
// 合約中的方法
console.log(cakeContract.methods);
```
log:
DELEGATION_TYPEHASH: ƒ ()
DELEGATION_TYPEHASH(): ƒ ()
DOMAIN_TYPEHASH: ƒ ()
DOMAIN_TYPEHASH(): ƒ ()
allowance: ƒ ()
allowance(address,address): ƒ ()
approve: ƒ ()
approve(address,uint256): ƒ ()
balanceOf: ƒ ()
balanceOf(address): ƒ ()
checkpoints: ƒ ()
checkpoints(address,uint32): ƒ ()
decimals: ƒ ()
decimals(): ƒ ()
decreaseAllowance: ƒ ()
decreaseAllowance(address,uint256): ƒ ()
delegate: ƒ ()
delegate(address): ƒ ()
delegateBySig: ƒ ()
delegateBySig(address,uint256,uint256,uint8,bytes32,bytes32): ƒ ()
delegates: ƒ ()
delegates(address): ƒ ()
getCurrentVotes: ƒ ()
getCurrentVotes(address): ƒ ()
getPriorVotes: ƒ ()
getPriorVotes(address,uint256): ƒ ()
increaseAllowance: ƒ ()
increaseAllowance(address,uint256): ƒ ()
mint: ƒ ()
mint(address,uint256): ƒ ()
name: ƒ ()
name(): ƒ ()
nonces: ƒ ()
nonces(address): ƒ ()
numCheckpoints: ƒ ()
numCheckpoints(address): ƒ ()
owner: ƒ ()
owner(): ƒ ()
renounceOwnership: ƒ ()
renounceOwnership(): ƒ ()
symbol: ƒ ()
symbol(): ƒ ()
totalSupply: ƒ ()
totalSupply(): ƒ ()
transfer: ƒ ()
transfer(address,uint256): ƒ ()
transferFrom: ƒ ()
transferFrom(address,address,uint256): ƒ ()
transferOwnership: ƒ ()
transferOwnership(address): ƒ ()