Skip to content

Commit

Permalink
Merge pull request #12 from formysister/update-class-structure
Browse files Browse the repository at this point in the history
bitcoin- getBalance() added, testcase- getBalance() added
  • Loading branch information
formysister authored Oct 30, 2023
2 parents f36e290 + 5814ef7 commit ff07014
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/wallet_class/bitcoin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as bip32 from 'bip32'
import * as bitcoin from 'bitcoinjs-lib'
import { PrivateKey } from 'bitcore-lib';

import axios from 'axios';

import { BtcNetwork, BtcWallet, BtcAccount } from "../../type/type"
import { BITCOIN_DEFAULT, BTC_MAINNET, BTC_REGTEST, BTC_TESTNET } from "../../constant";

Expand Down Expand Up @@ -167,6 +169,21 @@ class BitCoinWallet {
privateKey: privateKey,
}
}

/**
*
* @param address
* @returns {Number}
*/
getBalance = async (address: string): Promise<number> => {
try {
const response = await axios.get(`https://blockchain.info/q/addressbalance/${address}/`)

return response.data
} catch (error) {
throw error
}
}
}

export default BitCoinWallet
3 changes: 3 additions & 0 deletions test/sample_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const SAMPLE_DATA = {
SAMPLE_TOKEN_ADDRESS: "0xa8De46D005919aa6a8851C85E60f09884869c50a",
SAMPLE_721_NFT_ADDRESS: "0x7EC3f3CE817F5A5CA3e285022573Df1b1B4DeF7d",
SAMPLE_1155_NFT_ADDRESS: "0xA8bf99a3aDA507bCD0a8ce8982d3d94a4896440B"
},
BITCOIN: {
SAMPLE_ADDRESS: 'bc1pt4da3t0n347v7y95cpnqfw7el7u6uk47d9uvsxvntnkr7nsjwj7qyx8avw'
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,11 @@ describe("Wallet Test", () => {
expect(typeof wallet.privateKey).toBe('string')
expect(typeof wallet.address).toBe('string')
})

it("getBalance", async () => {
const balance = await bitcoinWallet.getBalance(SAMPLE_DATA.BITCOIN.SAMPLE_ADDRESS)

console.log(typeof balance)
})
})
})

0 comments on commit ff07014

Please sign in to comment.