Cleaned up
This commit is contained in:
parent
df9612c28c
commit
81f4c457d9
@ -1,4 +1,3 @@
|
|||||||
import { type UtxoWithTx } from 'sbtc';
|
|
||||||
import { type Transaction } from '@scure/btc-signer';
|
import { type Transaction } from '@scure/btc-signer';
|
||||||
import { NETWORK, MEMPOOLTXAPIURL, STACKSAPIURL, MEMPOOLURLADDRESS, STACKSURLADDRESS, STACKSURLADDRESSPOST, MEMPOOLTXURL } from './netconfig';
|
import { NETWORK, MEMPOOLTXAPIURL, STACKSAPIURL, MEMPOOLURLADDRESS, STACKSURLADDRESS, STACKSURLADDRESSPOST, MEMPOOLTXURL } from './netconfig';
|
||||||
|
|
||||||
@ -48,15 +47,14 @@ export type walletType = {
|
|||||||
publicKeyBTC: string | undefined,
|
publicKeyBTC: string | undefined,
|
||||||
balanceSBTC: number | undefined,
|
balanceSBTC: number | undefined,
|
||||||
addressSTX: string | undefined,
|
addressSTX: string | undefined,
|
||||||
utxos: UtxoWithTx[] | undefined,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type txInfoType = {
|
export type txInfoType = {
|
||||||
addressPeg: string | undefined,
|
addressPeg: string | undefined,
|
||||||
feeRate: number | undefined,
|
|
||||||
tx: Transaction | undefined,
|
tx: Transaction | undefined,
|
||||||
finalTx: string | undefined,
|
finalTx: string | undefined,
|
||||||
signature: string | undefined,
|
signature: string | undefined,
|
||||||
|
feeRate: number | undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const emptyWallet: walletType = {
|
export const emptyWallet: walletType = {
|
||||||
@ -66,15 +64,14 @@ export const emptyWallet: walletType = {
|
|||||||
publicKeyBTC: undefined,
|
publicKeyBTC: undefined,
|
||||||
balanceSBTC: undefined,
|
balanceSBTC: undefined,
|
||||||
addressSTX: undefined,
|
addressSTX: undefined,
|
||||||
utxos: undefined,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const emptyTxInfo: txInfoType = {
|
export const emptyTxInfo: txInfoType = {
|
||||||
addressPeg: undefined,
|
addressPeg: undefined,
|
||||||
feeRate: undefined,
|
|
||||||
tx: undefined,
|
tx: undefined,
|
||||||
finalTx: undefined,
|
finalTx: undefined,
|
||||||
signature: undefined,
|
signature: undefined,
|
||||||
|
feeRate: undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const humanReadableNumber = (number: number): string => {
|
export const humanReadableNumber = (number: number): string => {
|
||||||
|
131
src/app/page.tsx
131
src/app/page.tsx
@ -4,8 +4,7 @@ import type { UserData } from '@stacks/connect';
|
|||||||
import { AppConfig, UserSession, showConnect, openSignatureRequestPopup } from "@stacks/connect";
|
import { AppConfig, UserSession, showConnect, openSignatureRequestPopup } from "@stacks/connect";
|
||||||
import { StacksTestnet } from "@stacks/network";
|
import { StacksTestnet } from "@stacks/network";
|
||||||
import { bytesToHex, hexToBytes } from '@stacks/common';
|
import { bytesToHex, hexToBytes } from '@stacks/common';
|
||||||
import { sbtcDepositHelper, sbtcWithdrawHelper, sbtcWithdrawMessage, WALLET_00 } from 'sbtc';
|
import { sbtcWithdrawHelper, sbtcWithdrawMessage, WALLET_00 } from 'sbtc';
|
||||||
import type { UtxoWithTx } from 'sbtc';
|
|
||||||
import * as btc from '@scure/btc-signer';
|
import * as btc from '@scure/btc-signer';
|
||||||
|
|
||||||
// Network configuration
|
// Network configuration
|
||||||
@ -23,7 +22,6 @@ import { LogWindow } from './logwindow';
|
|||||||
import { Alert, Badge, Banner, Button, Card, Spinner } from 'flowbite-react';
|
import { Alert, Badge, Banner, Button, Card, Spinner } from 'flowbite-react';
|
||||||
|
|
||||||
// Setting: How much to deposit / withdraw
|
// Setting: How much to deposit / withdraw
|
||||||
const DEPOSITAMOUNT : number = 10_000;
|
|
||||||
const WITHDRAWAMOUNT : number = 10_000;
|
const WITHDRAWAMOUNT : number = 10_000;
|
||||||
|
|
||||||
// Main component
|
// Main component
|
||||||
@ -34,13 +32,6 @@ export default function Home() {
|
|||||||
const [txInfo, setTxInfo] = useState<txInfoType>(emptyTxInfo);
|
const [txInfo, setTxInfo] = useState<txInfoType>(emptyTxInfo);
|
||||||
const [userData, setUserData] = useState<UserData | null>(null);
|
const [userData, setUserData] = useState<UserData | null>(null);
|
||||||
|
|
||||||
// Log current state for debug purposes
|
|
||||||
// useEffect(() => {
|
|
||||||
// console.log("NEW STATE: ", state);
|
|
||||||
// console.log("WALLET: ", wallet);
|
|
||||||
// console.log("DEPOSIT INFO: ", depositInfo);
|
|
||||||
// }, [state]);
|
|
||||||
|
|
||||||
// Reset application
|
// Reset application
|
||||||
const reset = () : void => {
|
const reset = () : void => {
|
||||||
setState("DISCONNECTED");
|
setState("DISCONNECTED");
|
||||||
@ -68,19 +59,7 @@ export default function Home() {
|
|||||||
|
|
||||||
// Retrieve necessary information from the wallet and from the network
|
// Retrieve necessary information from the wallet and from the network
|
||||||
// This method depends on the network we are on. For now, it is implemented
|
// This method depends on the network we are on. For now, it is implemented
|
||||||
// for the local Development Network. Also, there are a few issues:
|
// for the local Development Network.
|
||||||
//
|
|
||||||
// setBtcAddress(userData.profile.btcAddress.p2wpkh.testnet);
|
|
||||||
// setBtcPublicKey(userData.profile.btcPublicKey.p2wpkh);
|
|
||||||
// Because of some quirks with Leather, we need to pull our BTC wallet using
|
|
||||||
// the helper if we are on devnet
|
|
||||||
// The following, as noted in the documentation, fails.
|
|
||||||
// According to Leather, the STX Address is the same as on the testnet.
|
|
||||||
// In fact, it coincides with the SBTC_FT_ADDRESS_DEVENV in the constants
|
|
||||||
// file (sbc).
|
|
||||||
// setStxAddress(bitcoinAccountA.tr.address);
|
|
||||||
// setFeeRate(await network.estimateFeeRate('low'));
|
|
||||||
// setSbtcPegAddress(await network.getSbtcPegAddress());
|
|
||||||
const getWalletAndTxDetails = async (userData:UserData) => {
|
const getWalletAndTxDetails = async (userData:UserData) => {
|
||||||
const bitcoinAccountA = await NETWORK.getBitcoinAccount(WALLET_00);
|
const bitcoinAccountA = await NETWORK.getBitcoinAccount(WALLET_00);
|
||||||
const addressBTC = bitcoinAccountA.wpkh.address;
|
const addressBTC = bitcoinAccountA.wpkh.address;
|
||||||
@ -94,14 +73,9 @@ export default function Home() {
|
|||||||
publicKeyBTC: bitcoinAccountA.publicKey.buffer.toString(),
|
publicKeyBTC: bitcoinAccountA.publicKey.buffer.toString(),
|
||||||
balanceBTC: balanceBTC,
|
balanceBTC: balanceBTC,
|
||||||
balanceSBTC: balanceSBTC,
|
balanceSBTC: balanceSBTC,
|
||||||
utxos: await NETWORK.fetchUtxos(addressBTC),
|
|
||||||
});
|
});
|
||||||
// Deposit Information
|
// Withdraw Information
|
||||||
// const feeRate = 1;
|
|
||||||
const feeRate = await getFeeRate();
|
const feeRate = await getFeeRate();
|
||||||
// const feeRates = await NETWORK.estimateFeeRates();
|
|
||||||
// console.log(feeRates);
|
|
||||||
// const feeRate = await NETWORK.estimateFeeRate('low');
|
|
||||||
setTxInfo({ ...txInfo,
|
setTxInfo({ ...txInfo,
|
||||||
addressPeg: await NETWORK.getSbtcPegAddress(),
|
addressPeg: await NETWORK.getSbtcPegAddress(),
|
||||||
feeRate: feeRate,
|
feeRate: feeRate,
|
||||||
@ -160,95 +134,23 @@ export default function Home() {
|
|||||||
const waitForConfirmation = (txid : string) => {
|
const waitForConfirmation = (txid : string) => {
|
||||||
const intervalId = setInterval(() => {
|
const intervalId = setInterval(() => {
|
||||||
waitUntilConfirmed(txid, intervalId);
|
waitUntilConfirmed(txid, intervalId);
|
||||||
// fetch(`${mempoolTxAPIUrl}${txid}/status`,{mode: 'no-cors'})
|
|
||||||
// .then((response) => response.json())
|
|
||||||
// .then((status) => {
|
|
||||||
// if (status.confirmed) {
|
|
||||||
// console.log("checkTX: CONFIRMED!");
|
|
||||||
// setConfirmed(true);
|
|
||||||
// clearInterval(intervalId);
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// .catch((err) => console.error(err));
|
|
||||||
}, 10000);
|
}, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hook to start deposit
|
|
||||||
const deposit = async () => {
|
|
||||||
const tx = await sbtcDepositHelper({
|
|
||||||
// network: TESTNET,
|
|
||||||
// pegAddress: sbtcPegAddress,
|
|
||||||
stacksAddress: wallet.addressSTX as string,
|
|
||||||
amountSats: DEPOSITAMOUNT,
|
|
||||||
feeRate: txInfo.feeRate as number,
|
|
||||||
utxos: wallet.utxos as UtxoWithTx[],
|
|
||||||
bitcoinChangeAddress: wallet.addressBTC as string,
|
|
||||||
});
|
|
||||||
setTxInfo({ ...txInfo, tx: tx });
|
|
||||||
// Sign and broadcast
|
|
||||||
const psbt = tx.toPSBT();
|
|
||||||
const requestParams = {
|
|
||||||
publicKey: wallet.publicKeyBTC as string,
|
|
||||||
hex: bytesToHex(psbt),
|
|
||||||
};
|
|
||||||
const txResponse = await window.btc.request("signPsbt", requestParams);
|
|
||||||
const formattedTx = btc.Transaction.fromPSBT(
|
|
||||||
hexToBytes(txResponse.result.hex)
|
|
||||||
);
|
|
||||||
formattedTx.finalize();
|
|
||||||
const finalTx : string = await NETWORK.broadcastTx(formattedTx);
|
|
||||||
setTxInfo({ ...txInfo, finalTx: finalTx });
|
|
||||||
// Wait for confirmatins
|
|
||||||
setState("REQUEST_SENT");
|
|
||||||
waitForConfirmation(finalTx);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hook to start withdraw
|
// Hook to start withdraw
|
||||||
const withdraw = async () => {
|
const withdraw = async () => {
|
||||||
const sbtcWalletAddress = wallet.addressSTX;
|
const feeRate = await getFeeRate();
|
||||||
const btcAddress = wallet.addressBTC;
|
|
||||||
const satoshis = WITHDRAWAMOUNT;
|
|
||||||
const signature = txInfo.signature;
|
|
||||||
const utxos = wallet.utxos;
|
|
||||||
const tx = await sbtcWithdrawHelper({
|
const tx = await sbtcWithdrawHelper({
|
||||||
// paymentPublicKey: sbtcWalletAddress,
|
// sbtcWalletAddress: wallet.addressSTX as string,
|
||||||
sbtcWalletAddress,
|
bitcoinAddress: wallet.addressBTC as string,
|
||||||
bitcoinAddress: btcAddress,
|
amountSats: WITHDRAWAMOUNT,
|
||||||
amountSats: satoshis,
|
signature: txInfo.signature as string,
|
||||||
signature,
|
feeRate: feeRate,
|
||||||
feeRate: await NETWORK.estimateFeeRate("low"),
|
|
||||||
fulfillmentFeeSats: 2000,
|
fulfillmentFeeSats: 2000,
|
||||||
utxos,
|
utxos: await NETWORK.fetchUtxos(wallet.addressBTC as string),
|
||||||
bitcoinChangeAddress: btcAddress,
|
bitcoinChangeAddress: wallet.addressBTC as string,
|
||||||
});
|
});
|
||||||
// const tx = await sbtcWithdrawHelper({
|
setTxInfo({ ...txInfo, tx: tx, feeRate: feeRate});
|
||||||
// // network: TESTNET,
|
|
||||||
// wallet.addressSTX,
|
|
||||||
// bitcoinAddress: wallet.addressBTC as string,
|
|
||||||
// amountSats: WITHDRAWAMOUNT,
|
|
||||||
// txInfo.signature,
|
|
||||||
// fulfillmentFeeSats: 1, // TODO: What is this?
|
|
||||||
// bitcoinChangeAddress: wallet.addressBTC as string,
|
|
||||||
// // pegAddress:
|
|
||||||
// feeRate: txInfo.feeRate as number,
|
|
||||||
// utxos: wallet.utxos as UtxoWithTx[],
|
|
||||||
// // utxoToSpendabe:
|
|
||||||
// // paymentPublicKey:
|
|
||||||
// // network?: BitcoinNetwork;
|
|
||||||
// // amountSats: number;
|
|
||||||
// // signature: string;
|
|
||||||
// // fulfillmentFeeSats: number;
|
|
||||||
// // bitcoinAddress: string;
|
|
||||||
// // bitcoinChangeAddress: string;
|
|
||||||
// // pegAddress?: string;
|
|
||||||
// // feeRate: number;
|
|
||||||
// // utxos: UtxoWithTx[];
|
|
||||||
// // utxoToSpendable?: Partial<SpendableByScriptTypes>;
|
|
||||||
// // paymentPublicKey?: string;
|
|
||||||
// });
|
|
||||||
console.log("TX PREPARED");
|
|
||||||
console.log(tx);
|
|
||||||
setTxInfo({ ...txInfo, tx: tx });
|
|
||||||
// Sign and broadcast
|
// Sign and broadcast
|
||||||
const psbt = tx.toPSBT();
|
const psbt = tx.toPSBT();
|
||||||
const requestParams = {
|
const requestParams = {
|
||||||
@ -260,18 +162,15 @@ export default function Home() {
|
|||||||
hexToBytes(txResponse.result.hex)
|
hexToBytes(txResponse.result.hex)
|
||||||
);
|
);
|
||||||
formattedTx.finalize();
|
formattedTx.finalize();
|
||||||
console.log("FormattedTx: ", formattedTx);
|
|
||||||
const finalTx : string = await NETWORK.broadcastTx(formattedTx);
|
const finalTx : string = await NETWORK.broadcastTx(formattedTx);
|
||||||
console.log("FinalTX: ", finalTx);
|
|
||||||
setTxInfo({ ...txInfo, finalTx: finalTx });
|
setTxInfo({ ...txInfo, finalTx: finalTx });
|
||||||
// Wait for confirmatins
|
// Wait for confirmations
|
||||||
setState("REQUEST_SENT");
|
setState("REQUEST_SENT");
|
||||||
waitForConfirmation(finalTx);
|
waitForConfirmation(finalTx);
|
||||||
}
|
}
|
||||||
|
|
||||||
const sign = async () => {
|
const sign = async () => {
|
||||||
const message = sbtcWithdrawMessage({
|
const message = sbtcWithdrawMessage({
|
||||||
// network: TESTNET,
|
|
||||||
amountSats: WITHDRAWAMOUNT,
|
amountSats: WITHDRAWAMOUNT,
|
||||||
bitcoinAddress: wallet.addressBTC as string
|
bitcoinAddress: wallet.addressBTC as string
|
||||||
});
|
});
|
||||||
@ -281,8 +180,6 @@ export default function Home() {
|
|||||||
userSession,
|
userSession,
|
||||||
network: new StacksTestnet(),
|
network: new StacksTestnet(),
|
||||||
onFinish: (data) => {
|
onFinish: (data) => {
|
||||||
console.log("SIGNATURE: ");
|
|
||||||
console.log(data);
|
|
||||||
setTxInfo({ ...txInfo, signature: data.signature });
|
setTxInfo({ ...txInfo, signature: data.signature });
|
||||||
setState("READY");
|
setState("READY");
|
||||||
},
|
},
|
||||||
@ -392,7 +289,7 @@ export default function Home() {
|
|||||||
<Badge
|
<Badge
|
||||||
color="warning"
|
color="warning"
|
||||||
>
|
>
|
||||||
{txInfo.feeRate as number} sat/byte fee
|
{txInfo.feeRate as number} sat/vB fee
|
||||||
</Badge>
|
</Badge>
|
||||||
</span>
|
</span>
|
||||||
</Alert>
|
</Alert>
|
||||||
|
Loading…
Reference in New Issue
Block a user