Skip to content

Commit 2e581d8

Browse files
authored
feat: use BigInt to make consistency (#3)
* feat: use bigint to make consistency * feat: refactor transaction amount conversion to use sdk methods
1 parent 7043617 commit 2e581d8

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

examples/react-playground/src/components/DemoDetails.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import useBalance from "../hooks/useBalance";
55
import useSend from "../hooks/useSend";
66
import CommitRevealDemo from "./CommitRevealDemo";
77
import SignPSKTDemo from "./SignPSKTDemo";
8+
import * as sdk from "@forbole/kastle-sdk";
89

910
export default function KaspaWalletDemoDetails() {
1011
const { address, publicKey } = useAccount();
@@ -23,10 +24,9 @@ export default function KaspaWalletDemoDetails() {
2324
}
2425
try {
2526
// Convert amount from KAS to sompi (1 KAS = 100,000,000 sompi)
26-
const amountSompi = Math.floor(parseFloat(amount) * 100000000);
27-
const priorityFeeSompi = priorityFee
28-
? Math.floor(parseFloat(priorityFee) * 100000000)
29-
: 0;
27+
const amountSompi = sdk.kaspaWasm.kaspaToSompi(amount) ?? BigInt(0);
28+
const priorityFeeSompi =
29+
sdk.kaspaWasm.kaspaToSompi(priorityFee) ?? BigInt(0);
3030

3131
await sendTransaction(recipient, amountSompi, priorityFeeSompi);
3232
} catch (err: any) {
@@ -35,8 +35,8 @@ export default function KaspaWalletDemoDetails() {
3535
};
3636

3737
// Format balance from sompi to KAS (1 KAS = 100,000,000 sompi)
38-
const formatBalance = (sompiAmount: number) => {
39-
return (sompiAmount / 100000000).toFixed(8);
38+
const formatBalance = (sompiAmount: bigint) => {
39+
return sdk.kaspaWasm.sompiToKaspaString(sompiAmount);
4040
};
4141

4242
return (

examples/react-playground/src/hooks/useBalance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { useEffect, useState } from "react";
77

88
export default function useBalance() {
9-
const [balance, setBalance] = useState(0);
9+
const [balance, setBalance] = useState(BigInt(0));
1010
const [error, setError] = useState<string | null>(null);
1111

1212
const refreshBalance = async () => {
@@ -24,7 +24,7 @@ export default function useBalance() {
2424
}, []);
2525

2626
useEffect(() => {
27-
const handler = async (balance: number) => {
27+
const handler = async (balance: bigint) => {
2828
setBalance(balance);
2929
};
3030

examples/react-playground/src/hooks/useSend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export default function useSend() {
88

99
const sendTransaction = async (
1010
address: string,
11-
amount: number,
12-
priorityFee?: number,
11+
amount: bigint,
12+
priorityFee?: bigint,
1313
) => {
1414
try {
1515
if (isSending) {

packages/sdk/src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ export const switchNetwork = async (
9797
/**
9898
* Fetches the current balance of the wallet
9999
*/
100-
export const getBalance = async (): Promise<number> => {
100+
export const getBalance = async (): Promise<bigint> => {
101101
const address = await getWalletAddress();
102102
const response = await rpcClient?.getBalanceByAddress({ address });
103103

104-
return parseInt(response?.balance?.toString() ?? "0", 10);
104+
return response?.balance ?? BigInt(0);
105105
};
106106

107107
/**
@@ -112,8 +112,8 @@ export const getBalance = async (): Promise<number> => {
112112
*/
113113
export const sendKaspa = async (
114114
toAddress: string,
115-
amountSompi: number,
116-
options?: { priorityFee?: number },
115+
amountSompi: bigint,
116+
options?: { priorityFee?: bigint },
117117
): Promise<string> => {
118118
if (!rpcClient) throw new Error("Unable to reach RPC");
119119

@@ -128,10 +128,10 @@ export const sendKaspa = async (
128128
outputs: [
129129
{
130130
address: toAddress,
131-
amount: BigInt(amountSompi),
131+
amount: amountSompi,
132132
},
133133
],
134-
priorityFee: BigInt(options?.priorityFee ?? 0),
134+
priorityFee: options?.priorityFee ?? BigInt(0),
135135
networkId: await getNetwork(),
136136
});
137137

0 commit comments

Comments
 (0)