Skip to content

Commit 0803e08

Browse files
committed
refactor: cleanup
1 parent d793db5 commit 0803e08

File tree

7 files changed

+41
-18
lines changed

7 files changed

+41
-18
lines changed

advanced/dapps/react-dapp-v2/src/chains/bip122.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ export const BtcMetadata: NamespaceMetadata = {
1717
},
1818
};
1919

20-
// "Non conforming namespaces. approve() namespaces chains don't satisfy required namespaces.
21-
// Required: bip122:000000000019d6689c085ae165831e93
22-
// Approved: xrpl:1,eip155:114,eip155:16,eip155:11155111,eip155:97,eip155:80002,eip155:4002,eip155:421614,eip155:11155420,eip155:51,eip155:84532,bip122:000000000933ea01ad0ee984209779ba,bip122:4966625a4b2851d9fdee139e56211a0d,bip122:bb0a78264637406b6360aad926284d54"
23-
2420
export function getChainMetadata(chainId: string): ChainMetadata {
2521
const reference = chainId.split(":")[1];
2622
const metadata = BtcMetadata[reference];

advanced/dapps/react-dapp-v2/src/constants/default.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,6 @@ export enum DEFAULT_KADENA_EVENTS {}
277277
* BITCOIN
278278
*/
279279
export enum DEFAULT_BIP122_METHODS {
280-
// BIP122_SIGN_MESSAGE = "btc_signMessage",
281-
// BIP122_SEND_TRANSACTION = "btc_sendTransaction",
282280
BIP122_SEND_TRANSACTION = "sendTransfer",
283281
BIP122_GET_ACCOUNT_ADDRESSES = "getAccountAddresses",
284282
BIP122_SIGN_MESSAGE = "signMessage",

advanced/dapps/react-dapp-v2/src/helpers/bip122.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { schnorr } from "@noble/secp256k1";
22
import * as bitcoin from "bitcoinjs-lib";
33
import BitcoinMessage from "bitcoinjs-message";
44
import { convertHexToBase64 } from "./utilities";
5-
5+
import { IUTXO } from "./types";
66
export async function apiGetBip122AccountBalance(
77
address: string,
88
chainId: string
@@ -19,15 +19,15 @@ export async function apiGetAddressUtxos(address: string, chainId: string) {
1919
).json();
2020
}
2121

22-
export function getAvailableBalanceFromUtxos(utxos: any[]) {
22+
export function getAvailableBalanceFromUtxos(utxos: IUTXO[]) {
2323
if (!utxos || !utxos.length) {
2424
return 0;
2525
}
2626
return utxos.reduce((acc, { value }) => acc + value, 0);
2727
}
2828

2929
export function calculateChange(
30-
utxos: any[],
30+
utxos: IUTXO[],
3131
amount: number,
3232
feeRate: number
3333
): number {

advanced/dapps/react-dapp-v2/src/helpers/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,15 @@ export interface KadenaAccount {
163163
account: string; // Kadena account
164164
chainId: ChainId; // Kadena ChainId
165165
}
166+
167+
export interface IUTXO {
168+
txid: string;
169+
vout: number;
170+
value: number;
171+
status: {
172+
confirmed: boolean;
173+
block_height: number;
174+
block_hash: string;
175+
block_time: number;
176+
};
177+
}

advanced/dapps/react-dapp-v2/src/pages/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ import OriginSimulationDropdown from "../components/OriginSimulationDropdown";
5050
import LoaderModal from "../modals/LoaderModal";
5151
import { numberToHex } from "@walletconnect/encoding";
5252
import RequestLoaderModal from "../modals/RequestLoaderModal";
53-
import { cons } from "fp-ts/lib/ReadonlyNonEmptyArray";
5453

5554
// Normal import does not work here
5655
const { version } = require("@walletconnect/sign-client/package.json");

advanced/wallets/react-wallet-v2/src/data/Bip122Data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Chains
33
*/
44
export const BITCOIN_MAINNET = {
5-
'bip122:000000000933ea01ad0ee984209779ba4ff763ae46a2a6c172b3f1b60a8ce26f': {
6-
chainId: '000000000933ea01ad0ee984209779ba4ff763ae46a2a6c172b3f1b60a8ce26f',
5+
'bip122:000000000019d6689c085ae165831e93': {
6+
chainId: '000000000019d6689c085ae165831e93',
77
name: 'BTC Mainnet',
88
logo: '/chain-logos/btc-mainnet.png',
99
rgb: '107, 111, 147',

advanced/wallets/react-wallet-v2/src/lib/Bip122Lib.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@ interface IInitArguments {
1313
privateKey?: string
1414
}
1515

16+
interface IUTXO {
17+
txid: string
18+
vout: number
19+
value: number
20+
status: {
21+
confirmed: boolean
22+
block_height: number
23+
block_hash: string
24+
block_time: number
25+
}
26+
}
27+
1628
interface ICreateTransaction {
1729
network: bitcoin.Network
1830
recipientAddress: string
1931
amount: number
2032
changeAddress: string
2133
memo?: string
22-
utxos: any[]
34+
utxos: IUTXO[]
2335
privateKeyWIF: string
2436
feeRate: number
2537
}
@@ -146,13 +158,13 @@ export default class Bip122Lib {
146158
throw new Error(`Invalid amount: ${amount}`)
147159
}
148160

149-
const utxos = await this.getUtXOs(account)
161+
const utxos = (await this.getUTXOs(account)) as IUTXO[]
150162
if (!utxos || utxos.length === 0) {
151163
throw new Error(`No UTXOs found for address: ${account}`)
152164
}
153165

154166
let utxosValue = 0
155-
const utxosToSpend: any[] = []
167+
const utxosToSpend: IUTXO[] = []
156168
utxos.forEach(utxo => {
157169
utxosValue += utxo.value
158170
utxosToSpend.push(utxo)
@@ -175,7 +187,7 @@ export default class Bip122Lib {
175187
return await this.broadcastTransaction(transaction)
176188
}
177189

178-
async getUtXOs(address: string): Promise<any[]> {
190+
async getUTXOs(address: string): Promise<IUTXO[]> {
179191
// make chain dynamic
180192
return await (await fetch(`https://mempool.space/testnet/api/address/${address}/utxo`)).json()
181193
}
@@ -192,7 +204,7 @@ export default class Bip122Lib {
192204
throw new Error('Error broadcasting transaction: ' + (await result.text()))
193205
}
194206

195-
getAvailableBalance(utxos: any[]) {
207+
getAvailableBalance(utxos: IUTXO[]) {
196208
return utxos.reduce((acc, { value }) => acc + value, 0)
197209
}
198210

@@ -352,8 +364,14 @@ export default class Bip122Lib {
352364
}
353365

354366
// Helper function to calculate change
355-
private calculateChange(utxos: any[], amount: number, feeRate: number): number {
367+
private calculateChange(utxos: IUTXO[], amount: number, feeRate: number): number {
356368
const inputSum = utxos.reduce((sum, utxo) => sum + utxo.value, 0)
369+
/**
370+
* 10 bytes: This is an estimated fixed overhead for the transaction.
371+
* 148 bytes: This is the average size of each input (UTXO).
372+
* 34 bytes: This is the size of each output.
373+
* The multiplication by 2 indicates that there are usually two outputs in a typical transaction (one for the recipient and one for change)
374+
*/
357375
const estimatedSize = 10 + 148 * utxos.length + 34 * 2
358376
const fee = estimatedSize * feeRate
359377
const change = inputSum - amount - fee

0 commit comments

Comments
 (0)