Skip to content

Commit 8824d2a

Browse files
authored
Merge pull request #252 from bitcoin-computer/feat/unconfirmedBalance2
Block reorgs and confirmed/unconfirmed balance
2 parents dbaf81d + 10d4bf1 commit 8824d2a

File tree

28 files changed

+110
-114
lines changed

28 files changed

+110
-114
lines changed

packages/chat/src/StartChat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function StartChat({ computer }) {
1212
console.log('creating chat')
1313
let chat
1414
try {
15-
if (await computer.getBalance() < 100) {
15+
if ((await computer.getBalance()).balance < 100) {
1616
await computer.faucet(1e7)
1717
}
1818
chat = await computer.new(ChatSc, [publicKey])

packages/chat/src/Wallet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function Wallet({ computer, chain }) {
77

88
useInterval(() => {
99
const getBalance = async () => {
10-
if (computer) setBalance(await computer.getBalance())
10+
if (computer) setBalance((await computer.getBalance()).balance)
1111
}
1212
getBalance()
1313
}, 3000)

packages/components/built/Wallet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var Balance = function (_a) {
8686
return [4 /*yield*/, computer.getBalance()];
8787
case 5:
8888
availableWalletBalance = _b.sent();
89-
setBalance(availableWalletBalance + amountsInPaymentToken);
89+
setBalance(availableWalletBalance.balance + amountsInPaymentToken);
9090
setChain(computer.getChain());
9191
_b.label = 6;
9292
case 6:
@@ -111,7 +111,7 @@ var Balance = function (_a) {
111111
_a = setBalance;
112112
return [4 /*yield*/, computer.getBalance()];
113113
case 2:
114-
_a.apply(void 0, [_b.sent()]);
114+
_a.apply(void 0, [(_b.sent()).balance]);
115115
return [2 /*return*/];
116116
}
117117
});

packages/components/src/Wallet.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const Balance = ({
3030
: 0
3131

3232
const availableWalletBalance = await computer.getBalance()
33-
setBalance(availableWalletBalance + amountsInPaymentToken)
33+
setBalance(availableWalletBalance.balance + amountsInPaymentToken)
3434
setChain(computer.getChain())
3535
}
3636
showLoader(false)
@@ -42,7 +42,7 @@ const Balance = ({
4242

4343
const fund = async () => {
4444
await computer.faucet(1e8)
45-
setBalance(await computer.getBalance())
45+
setBalance((await computer.getBalance()).balance)
4646
}
4747

4848
useEffect(() => {

packages/docs/Lib/getBalance.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# getBalance
22

3-
Returns the current balance in satoshi.
3+
Returns an object with the current balance in Satoshi, the confirmed balance in Satoshi (at least 1 confirmation), and the unconfirmed balance in Satoshi.
44

55
### Type
66
```ts
7-
() => Promise<string>
7+
() => Promise<{balance: number, confirmed: number, unconfirmed: number}>
88
```
99

1010
### Syntax

packages/docs/Lib/lib.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ The wallet functionality within a `Computer` instance can be accessed using the
5757
| [send](./send.md) | Sends satoshis to an address |
5858
| [rpcCall](./rpcCall.md) | Access Bitcoin's RPC interface |
5959
| [getAddress](./getAddress.md) | Returns the Bitcoin address of the computer wallet |
60-
| [getBalance](./getBalance.md) | Returns the balance in satoshi |
60+
| [getBalance](./getBalance.md) | Confirmed, unconfirmed and total balance in sats |
6161
| [getChain](./getChain.md) | Returns the blockchain |
6262
| [getNetwork](./getNetwork.md) | Returns the network |
6363
| [getMnemonic](./getMnemonic.md) | Returns a BIP39 mnemonic sentence |

packages/docs/node.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const { txId, vout } = await computer.faucet(1e4)
133133
expect(await new Computer(conf).getUtxos(address)).deep.eq([`${txId}:${vout}`])
134134

135135
// Return the balance
136-
expect(await new Computer(conf).getBalance(address)).eq(1e4)
136+
expect(await new Computer(conf).getBalance(address).balance).eq(1e4)
137137

138138
// Return the transactions
139139
expect(await new Computer(conf).listTxs(address)).deep.eq({

packages/ft/src/MintToken.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const MintToken: React.FC<IMintTokenProps> = ({ computer }) => {
3636

3737
const clicked = () => {
3838
setVisible(true)
39-
if (computer.getBalance() === 0) {
39+
if (computer.getBalance().balance === 0) {
4040
alert('Wallet has zero balance, please fund the wallet first.')
4141
}
4242
}

packages/ft/src/Wallet.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const Wallet: React.FC<IWalletProps> = ({ computer, chain }) => {
1616

1717
const refresh = async () => {
1818
try {
19-
if (computer) setBalance(await computer.getBalance())
19+
if (computer) setBalance((await computer.getBalance()).balance)
2020
} catch (err) {
2121
console.log(err)
2222
console.log("error occurred while fetching wallet details: ", err)

packages/lib/computer.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ declare class Wallet {
4646
readonly restClient: RestClient;
4747
constructor(params?: ComputerOptions);
4848
derive(subpath?: string): Wallet;
49-
getBalance(address?: string): Promise<number>;
49+
getBalance(address?: string): Promise<{balance: number, confirmed: number, unconfirmed: number}>;
5050
getUtxos(address?: string): Promise<_Unspent[]>;
5151
getDustThreshold(isWitnessProgram: boolean, script?: Buffer): number;
5252
getAmountThreshold(isWitnessProgram: boolean, script: Buffer): number;
@@ -109,7 +109,7 @@ declare class RestClient {
109109
constructor({ chain, network, mnemonic, path, passphrase, addressType, url, satPerByte, dustRelayFee }?: ComputerOptions);
110110
rpc(method: string, params: string): Promise<any>;
111111
broadcast(txHex: string): Promise<string>;
112-
getBalance(address: string): Promise<number>;
112+
getBalance(address: string): Promise<{balance: number, confirmed: number, unconfirmed: number}>;
113113
listTxs(address: string): Promise<_Transaction>;
114114
getUtxos(address: string): Promise<_Unspent[]>;
115115
getFormattedUtxos(address: string): Promise<_Unspent[]>;
@@ -332,7 +332,7 @@ declare class Computer {
332332
load(rev: string): Promise<ModuleExportsNamespace>;
333333
listTxs(address?: string): Promise<import("./types")._Transaction>;
334334
getUtxos(address?: string): Promise<string[]>;
335-
getBalance(address?: string): Promise<number>;
335+
getBalance(address?: string): Promise<{balance: number, confirmed: number, unconfirmed: number}>;
336336
sign(transaction: Transaction, opts?: SigOptions): Promise<void>;
337337
fund(tx: Transaction, opts?: Fee & FundOptions): Promise<void>;
338338
send(satoshis: number, address: string): Promise<string>;

0 commit comments

Comments
 (0)