Skip to content

Commit

Permalink
feat: modified contracts and balance fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Dec 15, 2023
1 parent 75f4998 commit 63d29ba
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 17 additions & 7 deletions src/app/common/hooks/use-bitcoin-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,27 @@ export function useBitcoinContracts() {
close();
}

async function getAllSignedBitcoinContracts(): Promise<BitcoinContractListItem[] | undefined> {
async function getAllActiveBitcoinContracts(): Promise<BitcoinContractListItem[] | undefined> {
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) {
Expand All @@ -239,7 +249,7 @@ export function useBitcoinContracts() {

async function sumBitcoinContractCollateralAmounts(): Promise<Money> {
let bitcoinContractsCollateralSum = 0;
const bitcoinContracts = await getAllSignedBitcoinContracts();
const bitcoinContracts = await getAllActiveBitcoinContracts();
if (!bitcoinContracts) return createMoneyFromDecimal(0, 'BTC');

bitcoinContracts.forEach((bitcoinContract: BitcoinContractListItem) => {
Expand Down Expand Up @@ -313,7 +323,7 @@ export function useBitcoinContracts() {
handleOffer,
handleAccept,
handleReject,
getAllSignedBitcoinContracts,
getAllActiveBitcoinContracts,
sumBitcoinContractCollateralAmounts,
sendRpcResponse,
};
Expand Down
4 changes: 2 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 @@ -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<BitcoinContractListItem[]>([]);
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);
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 63d29ba

Please sign in to comment.