Skip to content
Draft
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
4 changes: 4 additions & 0 deletions packages/snap/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Add `rawAmount` to balance responses and events for precision in balance calculations ([#573](https://github.com/MetaMask/snap-bitcoin-wallet/pull/573))

## [1.8.0]

### Changed
Expand Down
1 change: 1 addition & 0 deletions packages/snap/integration-test/keyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ describe('Keyring', () => {
[Caip19Asset.Regtest]: {
amount: '500',
unit: CurrencyUnit.Regtest,
rawAmount: '50000000000',
},
});
});
Expand Down
5 changes: 4 additions & 1 deletion packages/snap/src/handlers/KeyringHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ describe('KeyringHandler', () => {
const mockAccount = mock<BitcoinAccount>({
id: 'some-id',
addressType: 'p2wpkh',
balance: { trusted_spendable: { to_btc: () => 1 } },
balance: {
trusted_spendable: { to_btc: () => 1, to_sat: () => BigInt(100000000) },
},
network: 'bitcoin',
derivationPath: ['myEntropy', "84'", "0'", "0'"],
entropySource: 'myEntropy',
Expand Down Expand Up @@ -500,6 +502,7 @@ describe('KeyringHandler', () => {
[Caip19Asset.Bitcoin]: {
amount: '1',
unit: 'BTC',
rawAmount: '100000000',
},
};

Expand Down
5 changes: 4 additions & 1 deletion packages/snap/src/handlers/KeyringHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,15 @@ export class KeyringHandler implements Keyring {
id: string,
): Promise<Record<CaipAssetType, Balance>> {
const account = await this.#accountsUseCases.get(id);
const balance = account.balance.trusted_spendable.to_btc().toString();
const trustedSpendable = account.balance.trusted_spendable;
const balance = trustedSpendable.to_btc().toString();
const rawBalance = trustedSpendable.to_sat().toString();

return {
[networkToCaip19[account.network]]: {
amount: balance,
unit: networkToCurrencyUnit[account.network],
rawAmount: rawBalance,
},
};
}
Expand Down
5 changes: 4 additions & 1 deletion packages/snap/src/infra/SnapClientAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ export class SnapClientAdapter implements SnapClient {
async emitAccountBalancesUpdatedEvent(
account: BitcoinAccount,
): Promise<void> {
const balance = account.balance.trusted_spendable.to_btc().toString();
const trustedSpendable = account.balance.trusted_spendable;
const balance = trustedSpendable.to_btc().toString();
const rawBalance = trustedSpendable.to_sat().toString();
return emitSnapKeyringEvent(snap, KeyringEvent.AccountBalancesUpdated, {
balances: {
[account.id]: {
[networkToCaip19[account.network]]: {
amount: balance,
unit: networkToCurrencyUnit[account.network],
rawAmount: rawBalance,
},
},
},
Expand Down
Loading