From 63d29ba39ed228a95da777f41a365fb8e91bf911 Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Thu, 14 Dec 2023 16:00:34 +0100 Subject: [PATCH] feat: modified contracts and balance fetching --- package.json | 2 +- src/app/common/hooks/use-bitcoin-contracts.ts | 24 +++++++++++++------ .../bitcoin-contract-list.tsx | 4 ++-- yarn.lock | 10 ++++---- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 26db783b445..84d07f18823 100644 --- a/package.json +++ b/package.json @@ -122,7 +122,7 @@ "dependencies": { "@bitcoinerlab/secp256k1": "1.0.2", "@coinbase/cbpay-js": "1.0.2", - "@dlc-link/dlc-tools": "1.0.9", + "@dlc-link/dlc-tools": "1.1.0", "@fungible-systems/zone-file": "2.0.0", "@hirosystems/token-metadata-api-client": "1.1.0", "@ledgerhq/hw-transport-webusb": "6.27.19", diff --git a/src/app/common/hooks/use-bitcoin-contracts.ts b/src/app/common/hooks/use-bitcoin-contracts.ts index 1f434476aaa..88355b05eb5 100644 --- a/src/app/common/hooks/use-bitcoin-contracts.ts +++ b/src/app/common/hooks/use-bitcoin-contracts.ts @@ -207,17 +207,27 @@ export function useBitcoinContracts() { close(); } - async function getAllSignedBitcoinContracts(): Promise { + async function getAllActiveBitcoinContracts(): Promise { const bitcoinContractInterface = await getBitcoinContractInterface(); if (!bitcoinContractInterface) return; + await bitcoinContractInterface.periodic_check(); const bitcoinContracts = await bitcoinContractInterface.get_contracts(); - const signedBitcoinContracts = bitcoinContracts.filter( - (bitcoinContract: BitcoinContractListItem) => bitcoinContract.state === 'Signed' - ); - return signedBitcoinContracts; + const stateOrder = ['Signed', 'Confirmed']; + + const activeBitcoinContracts = bitcoinContracts + .filter( + (bitcoinContract: BitcoinContractListItem) => + bitcoinContract.state === 'Signed' || bitcoinContract.state === 'Confirmed' + ) + .sort( + (a: BitcoinContractListItem, b: BitcoinContractListItem) => + stateOrder.indexOf(a.state) - stateOrder.indexOf(b.state) + ); + + return activeBitcoinContracts; } function getTransactionDetails(txId: string, bitcoinCollateral: number) { @@ -239,7 +249,7 @@ export function useBitcoinContracts() { async function sumBitcoinContractCollateralAmounts(): Promise { let bitcoinContractsCollateralSum = 0; - const bitcoinContracts = await getAllSignedBitcoinContracts(); + const bitcoinContracts = await getAllActiveBitcoinContracts(); if (!bitcoinContracts) return createMoneyFromDecimal(0, 'BTC'); bitcoinContracts.forEach((bitcoinContract: BitcoinContractListItem) => { @@ -313,7 +323,7 @@ export function useBitcoinContracts() { handleOffer, handleAccept, handleReject, - getAllSignedBitcoinContracts, + getAllActiveBitcoinContracts, sumBitcoinContractCollateralAmounts, sendRpcResponse, }; diff --git a/src/app/pages/bitcoin-contract-list/bitcoin-contract-list.tsx b/src/app/pages/bitcoin-contract-list/bitcoin-contract-list.tsx index 069ff293fa5..5f8766e5a95 100644 --- a/src/app/pages/bitcoin-contract-list/bitcoin-contract-list.tsx +++ b/src/app/pages/bitcoin-contract-list/bitcoin-contract-list.tsx @@ -12,14 +12,14 @@ import { BitcoinContractListItemLayout } from './components/bitcoin-contract-lis import { BitcoinContractListLayout } from './components/bitcoin-contract-list-layout'; export function BitcoinContractList() { - const { getAllSignedBitcoinContracts } = useBitcoinContracts(); + const { getAllActiveBitcoinContracts } = useBitcoinContracts(); const [bitcoinContracts, setBitcoinContracts] = useState([]); const [isLoading, setLoading] = useState(true); const [isError, setError] = useState(false); useOnMount(() => { const fetchAndFormatBitcoinContracts = async () => { - const fetchedBitcoinContracts = await getAllSignedBitcoinContracts(); + const fetchedBitcoinContracts = await getAllActiveBitcoinContracts(); if (!fetchedBitcoinContracts) { setError(true); setLoading(false); diff --git a/yarn.lock b/yarn.lock index ac7dec45539..33454f12cf2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -899,10 +899,10 @@ 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.9": - version "1.0.9" - resolved "https://registry.yarnpkg.com/@dlc-link/dlc-tools/-/dlc-tools-1.0.9.tgz#3dfe1d743e9d5e309732e55c824ffbf77039f2bf" - integrity sha512-+lxe6nrZCbuUWcMR4l9X+axqR1yB7lgtjsrP6Pp23GRqSGL+wvts+L8wSCJxTi6rDhBKfGzvau3SLOPrha/3Rw== +"@dlc-link/dlc-tools@1.1.0": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@dlc-link/dlc-tools/-/dlc-tools-1.1.0.tgz#6d28f139ea971894c11ba182776cf97a35317eeb" + integrity sha512-P4APlS2PaA6pNEh2WFonGspMvO9HJXjmqQNYPeM9p+THUoQQDb8Uq9aTaiN8K/5oS8kqZCetkfdBijtgO9Vi+w== "@dnd-kit/accessibility@^3.1.0": version "3.1.0" @@ -12065,7 +12065,7 @@ is-typedarray@^1.0.0, is-typedarray@~1.0.0: resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a" integrity sha512-cyA56iCMHAh5CdzjJIa4aohJyeO1YbwLi3Jc35MmRU6poroFjIGZzUzupGiRPOjgHg9TLu43xbpwXk523fMxKA== -is-unicode-supported@^1.1.0, is-unicode-supported@^1.2.0, is-unicode-supported@^1.3.0: +is-unicode-supported@^1.1.0, is-unicode-supported@^1.2.0: version "1.3.0" resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-1.3.0.tgz#d824984b616c292a2e198207d4a609983842f714" integrity sha512-43r2mRvz+8JRIKnWJ+3j8JtjRKZ6GmjzfaE/qiBJnikNnYv/6bagRJ1kUhNk8R5EX/GkobD+r+sfxCPJsiKBLQ==