Skip to content

Commit

Permalink
fix: removed attestor checking and argument from the bitcoin contract…
Browse files Browse the repository at this point in the history
… interface, better error handling
  • Loading branch information
Polybius93 committed Nov 1, 2023
1 parent e349e0c commit 7a0a2e8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 75 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
"dependencies": {
"@bitcoinerlab/secp256k1": "1.0.2",
"@coinbase/cbpay-js": "1.0.2",
"@dlc-link/dlc-tools": "1.0.2",
"@fungible-systems/zone-file": "2.0.0",
"@hirosystems/token-metadata-api-client": "1.1.0",
"@ledgerhq/hw-transport-webusb": "6.27.19",
Expand Down Expand Up @@ -185,6 +184,7 @@
"compare-versions": "4.1.3",
"css-loader": "6.8.1",
"dayjs": "1.11.8",
"dlc-tools": "file:../new-dlc-stack/wasm-wallet/pkg",
"dompurify": "3.0.6",
"dotenv": "16.3.1",
"ecdsa-sig-formatter": "1.0.11",
Expand Down
64 changes: 11 additions & 53 deletions src/app/common/hooks/use-bitcoin-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useNavigate } from 'react-router-dom';

import { RpcErrorCode } from '@btckit/types';
import { JsDLCInterface } from '@dlc-link/dlc-tools';
import { bytesToHex } from '@stacks/common';
import { JsDLCInterface } from 'dlc-tools';

import {
deriveAddressIndexKeychainFromAccount,
Expand All @@ -14,7 +14,6 @@ import { BitcoinContractResponseStatus } from '@shared/rpc/methods/accept-bitcoi
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { makeRpcErrorResponse } from '@shared/rpc/rpc-methods';

import { checkDlcLinkAttestorHealth } from '@app/query/bitcoin/contract/check-dlc-link-attestor-health';
import { sendAcceptedBitcoinContractOfferToProtocolWallet } from '@app/query/bitcoin/contract/send-accepted-bitcoin-contract-offer';
import {
useCalculateBitcoinFiatValue,
Expand Down Expand Up @@ -66,9 +65,7 @@ export function useBitcoinContracts() {
const nativeSegwitPrivateKeychain = useNativeSegwitAccountBuilder()?.(currentIndex);
const currentBitcoinNetwork = useCurrentNetwork();

async function getBitcoinContractInterface(
attestorURLs: string[]
): Promise<JsDLCInterface | undefined> {
async function getBitcoinContractInterface(): Promise<JsDLCInterface | undefined> {
if (!nativeSegwitPrivateKeychain || !bitcoinAccountDetails) return;

const currentAddress = bitcoinAccountDetails.address;
Expand All @@ -84,8 +81,7 @@ export function useBitcoinContracts() {
bytesToHex(currentAddressPrivateKey),
currentAddress,
currentBitcoinNetwork.chain.bitcoin.network,
currentBitcoinNetwork.chain.bitcoin.url,
JSON.stringify(attestorURLs)
currentBitcoinNetwork.chain.bitcoin.url
);

return bitcoinContractInterface;
Expand Down Expand Up @@ -121,12 +117,11 @@ export function useBitcoinContracts() {

async function handleAccept(
bitcoinContractJSON: string,
counterpartyWalletDetails: CounterpartyWalletDetails,
attestorURLs: string[]
counterpartyWalletDetails: CounterpartyWalletDetails
) {
let bitcoinContractInterface: JsDLCInterface | undefined;
try {
bitcoinContractInterface = await getBitcoinContractInterface(attestorURLs);
bitcoinContractInterface = await getBitcoinContractInterface();
} catch (error) {
navigate(RouteUrls.BitcoinContractLockError, {
state: {
Expand All @@ -145,9 +140,9 @@ export function useBitcoinContracts() {
const bitcoinContractCollateralAmount =
bitcoinContractOffer.contractInfo.singleContractInfo.totalCollateral;

await bitcoinContractInterface.get_wallet_balance();

try {
await bitcoinContractInterface.get_wallet_balance();

const acceptedBitcoinContract =
await bitcoinContractInterface.accept_offer(bitcoinContractJSON);

Expand Down Expand Up @@ -196,46 +191,8 @@ export function useBitcoinContracts() {
close();
}

async function getHealthyDlcLinkAttestor(): Promise<string> {
const dlcLinkAttestorUrls = [
'https://devnet.dlc.link/attestor-1/',
'https://devnet.dlc.link/attestor-2/',
'https://devnet.dlc.link/attestor-3/',
];

let currentAttestorUrl: string | undefined;

for (const attestorURL of dlcLinkAttestorUrls) {
const isAttestorHealthy = await checkDlcLinkAttestorHealth(attestorURL);
if (isAttestorHealthy) {
currentAttestorUrl = attestorURL;
break;
}
}

if (!currentAttestorUrl) {
throw new Error('Unable to find a healthy DLC.Link attestor');
}

return currentAttestorUrl;
}

async function getAllSignedBitcoinContracts() {
let bitcoinContractInterface: JsDLCInterface | undefined;

try {
const currentAttestorUrl = await getHealthyDlcLinkAttestor();
bitcoinContractInterface = await getBitcoinContractInterface([currentAttestorUrl]);
} catch (error) {
navigate(RouteUrls.BitcoinContractLockError, {
state: {
error,
title: 'There was an error with getting the Bitcoin Contract Interface',
body: 'Unable to setup Bitcoin Contract Interface',
},
});
sendRpcResponse(BitcoinContractResponseStatus.INTERFACE_ERROR);
}
async function getAllSignedBitcoinContracts(): Promise<BitcoinContractListItem[] | undefined> {
const bitcoinContractInterface = await getBitcoinContractInterface();

if (!bitcoinContractInterface) return;

Expand Down Expand Up @@ -264,9 +221,10 @@ export function useBitcoinContracts() {
};
}

async function sumBitcoinContractCollateralAmounts(): Promise<Money> {
async function sumBitcoinContractCollateralAmounts(): Promise<Money | undefined> {
let bitcoinContractsCollateralSum = 0;
const bitcoinContracts = await getAllSignedBitcoinContracts();
if (!bitcoinContracts) return;
bitcoinContracts.forEach((bitcoinContract: BitcoinContractListItem) => {
bitcoinContractsCollateralSum += parseInt(bitcoinContract.acceptorCollateral);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function BitcoinContractEntryPoint({ btcAddress }: BitcoinContractEntryPo
const getBitcoinContractDataAndSetState = async () => {
setIsLoading(true);
const currentBitcoinContractSum = await sumBitcoinContractCollateralAmounts();
if (!currentBitcoinContractSum) return;
setBitcoinContractSum(currentBitcoinContractSum);
setIsLoading(false);
};
Expand Down
14 changes: 12 additions & 2 deletions src/app/pages/bitcoin-contract-list/bitcoin-contract-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ export function BitcoinContractList() {
const { getAllSignedBitcoinContracts } = useBitcoinContracts();
const [bitcoinContracts, setBitcoinContracts] = useState<BitcoinContractListItem[]>([]);
const [isLoading, setLoading] = useState(true);
const [isError, setError] = useState(false);

useOnMount(() => {
const fetchAndFormatBitcoinContracts = async () => {
const fetchedBitcoinContracts = await getAllSignedBitcoinContracts();
if (!fetchedBitcoinContracts) {
setError(true);
setLoading(false);
return;
}
setBitcoinContracts(fetchedBitcoinContracts);
setLoading(false);
};
Expand All @@ -30,9 +36,13 @@ export function BitcoinContractList() {

return (
<BitcoinContractListLayout>
{bitcoinContracts.length === 0 ? (
{bitcoinContracts.length === 0 || isError ? (
<Flex width="100%" height="100vh" alignItems="center" justifyContent="center">
<Text textAlign="center">You don't have any open Bitcoin Contracts.</Text>
<Text textAlign="center">
{isError
? 'Bitcoin Contracts are not available currently'
: "You don't have any open Bitcoin Contracts."}
</Text>
</Flex>
) : (
bitcoinContracts.map(bitcoinContract => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,14 @@ export function BitcoinContractRequest() {
const [bitcoinContractOfferDetails, setBitcoinContractOfferDetails] =
useState<BitcoinContractOfferDetails>();
const [bitcoinAddress, setBitcoinAddress] = useState<string>();
const [attestorURLs, setAttestorURLs] = useState<string[]>([]);

const [isLoading, setLoading] = useState(true);
const [isProcessing, setProcessing] = useState(false);

const handleAcceptClick = async () => {
if (!bitcoinContractJSON || !bitcoinContractOfferDetails) return;
setProcessing(true);
await handleAccept(
bitcoinContractJSON,
bitcoinContractOfferDetails.counterpartyWalletDetails,
attestorURLs
);
await handleAccept(bitcoinContractJSON, bitcoinContractOfferDetails.counterpartyWalletDetails);
setProcessing(false);
};

Expand All @@ -52,7 +47,6 @@ export function BitcoinContractRequest() {
useOnMount(() => {
const bitcoinContractOfferJSON = initialSearchParams.get('bitcoinContractOffer');
const counterpartyWalletDetailsJSON = initialSearchParams.get('counterpartyWalletDetails');
const attestorURLs = initialSearchParams.get('attestorURLs');

const bitcoinAccountDetails = getNativeSegwitSigner?.(0);

Expand All @@ -71,12 +65,7 @@ export function BitcoinContractRequest() {
sendRpcResponse(BitcoinContractResponseStatus.NETWORK_ERROR);
}

if (
!getNativeSegwitSigner ||
!bitcoinContractOfferJSON ||
!counterpartyWalletDetailsJSON ||
!attestorURLs
)
if (!getNativeSegwitSigner || !bitcoinContractOfferJSON || !counterpartyWalletDetailsJSON)
return;

const currentBitcoinContractOfferDetails = handleOffer(
Expand All @@ -89,7 +78,6 @@ export function BitcoinContractRequest() {
setBitcoinContractJSON(bitcoinContractOfferJSON);
setBitcoinContractOfferDetails(currentBitcoinContractOfferDetails);
setBitcoinAddress(currentAddress);
setAttestorURLs(JSON.parse(attestorURLs));
setLoading(false);
});

Expand Down
8 changes: 3 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1065,11 +1065,6 @@
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz#1d572bfbbe14b7704e0ba0f39b74815b84870d70"
integrity sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==

"@dlc-link/dlc-tools@1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@dlc-link/dlc-tools/-/dlc-tools-1.0.2.tgz#fe7531b735a8f7e4bfee6ca9224c9bde99783027"
integrity sha512-EAp5Z/bhUzTdxLSOB9PeeSCfdNyNXsi+NJ25NiQcmczJXwczmShjqRIKpZt/WWs2Ahgi0zpIyFmcvFn7IWo/6Q==

"@dnd-kit/accessibility@^3.0.0":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@dnd-kit/accessibility/-/accessibility-3.0.1.tgz#3ccbefdfca595b0a23a5dc57d3de96bc6935641c"
Expand Down Expand Up @@ -10907,6 +10902,9 @@ dir-glob@^3.0.1:
dependencies:
path-type "^4.0.0"

"dlc-tools@file:../new-dlc-stack/wasm-wallet/pkg":
version "1.0.3"

dlv@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/dlv/-/dlv-1.1.3.tgz#5c198a8a11453596e751494d49874bc7732f2e79"
Expand Down

0 comments on commit 7a0a2e8

Please sign in to comment.