Skip to content

Commit

Permalink
[Issue-80] Resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
S2kael committed May 24, 2024
1 parent 138965d commit 2d4cd0f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import { _AssetType, _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types';
import { APIItemState } from '@subwallet/extension-base/background/KoniTypes';
import { COMMON_REFRESH_BALANCE_INTERVAL } from '@subwallet/extension-base/constants';
import { Brc20BalanceItem } from '@subwallet/extension-base/services/chain-service/handler/bitcoin/strategy/BlockStream/types';
import { _BitcoinApi } from '@subwallet/extension-base/services/chain-service/types';
import { _getChainNativeTokenSlug, _getRuneId, _isSupportRuneChain } from '@subwallet/extension-base/services/chain-service/utils';
import { _getChainNativeTokenSlug, _getRuneId } from '@subwallet/extension-base/services/chain-service/utils';
import { BalanceItem } from '@subwallet/extension-base/types';
import { filterAssetsByChainAndType, filteredOutTxsUtxos, filterOutPendingTxsUtxos, getInscriptionUtxos, getRuneTxsUtxos } from '@subwallet/extension-base/utils';
import BigN from 'bignumber.js';

// todo: update bitcoin params
function subscribeAddressesRuneInfo (bitcoinApi: _BitcoinApi, addresses: string[], assetMap: Record<string, _ChainAsset>, chainInfo: _ChainInfo, callback: (rs: BalanceItem[]) => void) {
// todo: currently set decimal of runes on chain list to zero because the amount api return is after decimal
function subscribeRuneBalance (bitcoinApi: _BitcoinApi, addresses: string[], assetMap: Record<string, _ChainAsset>, chainInfo: _ChainInfo, callback: (rs: BalanceItem[]) => void) {
const chain = chainInfo.slug;
const tokenList = filterAssetsByChainAndType(assetMap, chain, [_AssetType.LOCAL]);
const tokenList = filterAssetsByChainAndType(assetMap, chain, [_AssetType.RUNE]);

// todo: check await asset ready before subscribe
if (Object.keys(tokenList).length === 0) {
Expand All @@ -39,6 +39,10 @@ function subscribeAddressesRuneInfo (bitcoinApi: _BitcoinApi, addresses: string[
runes.forEach((rune) => {
const runeId = rune.rune_id;

if (!Object.keys(runeIdToSlugMap).includes(runeId)) {
return;
}

const item = {
address: address,
tokenSlug: runeIdToSlugMap[runeId],
Expand Down Expand Up @@ -76,6 +80,53 @@ function subscribeAddressesRuneInfo (bitcoinApi: _BitcoinApi, addresses: string[
};
}

function subscribeBRC20Balance (bitcoinApi: _BitcoinApi, addresses: string[], assetMap: Record<string, _ChainAsset>, chainInfo: _ChainInfo, callback: (rs: BalanceItem[]) => void) {
const chain = chainInfo.slug;
const tokenList = filterAssetsByChainAndType(assetMap, chain, [_AssetType.BRC20]);

const getBRC20Balance = () => {
Object.values(tokenList).map(async (token) => {
try {
const ticker = token.symbol;
const balances: Brc20BalanceItem[] = await Promise.all(addresses.map(async (address) => {
try {
return await bitcoinApi.api.getAddressBRC20FreeLockedBalance(address, ticker);
} catch (error) {
console.error(`Error on get BRC balance of account ${address} for token ${token.slug}`, error);

return {
free: '0',
locked: '0'
};
}
}));

const items: BalanceItem[] = balances.map((balance, index): BalanceItem => {
return {
address: addresses[index],
tokenSlug: token.slug,
free: balance.free || '0',
locked: balance.locked || '0',
state: APIItemState.READY
};
});

callback(items);
} catch (error) {
console.log(token.slug, error);
}
});
};

getBRC20Balance();

const interval = setInterval(getBRC20Balance, COMMON_REFRESH_BALANCE_INTERVAL);

return () => {
clearInterval(interval);
};
}

export const getTransferableBitcoinUtxos = async (bitcoinApi: _BitcoinApi, address: string) => {
try {
const [utxos, txs, runeTxsUtxos, inscriptionUtxos] = await Promise.all([
Expand Down Expand Up @@ -158,18 +209,15 @@ export function subscribeBitcoinBalance (addresses: string[], chainInfo: _ChainI
};

getBalance();

const interval = setInterval(getBalance, COMMON_REFRESH_BALANCE_INTERVAL);

if (_isSupportRuneChain(chainInfo.slug)) {
const unsub = subscribeAddressesRuneInfo(bitcoinApi, addresses, assetMap, chainInfo, callback);
const unsub = subscribeRuneBalance(bitcoinApi, addresses, assetMap, chainInfo, callback);
const unsub2 = subscribeBRC20Balance(bitcoinApi, addresses, assetMap, chainInfo, callback);

return () => {
clearInterval(interval);
unsub && unsub();
};
} else {
return () => {
clearInterval(interval);
};
}
return () => {
clearInterval(interval);
unsub && unsub();
unsub2 && unsub2();
};
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6615,7 +6615,7 @@ __metadata:

"@subwallet/keyring@file:./local-libs/keyring::locator=root-workspace-0b6124%40workspace%3A.":
version: 0.1.4
resolution: "@subwallet/keyring@file:./local-libs/keyring#./local-libs/keyring::hash=03559c&locator=root-workspace-0b6124%40workspace%3A."
resolution: "@subwallet/keyring@file:./local-libs/keyring#./local-libs/keyring::hash=6d1c5c&locator=root-workspace-0b6124%40workspace%3A."
dependencies:
"@ethereumjs/tx": ^5.0.0
"@polkadot/util": ^12.2.1
Expand All @@ -6630,7 +6630,7 @@ __metadata:
rxjs: ^7.5.6
tiny-secp256k1: ^2.2.3
tslib: ^2.6.2
checksum: b11f239a161cd67dea77d9af5d757d3fc55825d9b5572530368de58cae58860c79dfba0582258f1c5fc218ee0843f14b713075fdf5578c41ea0741c3a9ae0918
checksum: 9bfb78763418b24bcac0502513a95a9d82f7f16ddb8a7a35efd3cb0be5b64d19297f4e21e12c275db224afe7bc8ef5906827eca6634b0e1df0418bc5369f8469
languageName: node
linkType: hard

Expand Down

0 comments on commit 2d4cd0f

Please sign in to comment.