Skip to content

Commit

Permalink
fix: issues OK-22986 & fetch token balances with 'withBRC20Tokens=tru…
Browse files Browse the repository at this point in the history
…e' (#3480)

* fix: nft detail did not show on send confirm

* fix: nft asset did not update

* fix: brc20 history error

* feat: request token balance with withBRC20Tokens=true
  • Loading branch information
weatherstar authored Aug 31, 2023
1 parent 62302bf commit 1f6b69f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 54 deletions.
2 changes: 2 additions & 0 deletions packages/engine/src/apiProxyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type TokenBalancesQuery = {
// eslint-disable-next-line camelcase
contract_addresses?: string[];
xpub?: string;
withBRC20Tokens?: boolean;
};

export type TokenBalancesResponse = {
Expand Down Expand Up @@ -38,6 +39,7 @@ export const getBalancesFromApi = async ({
const query: TokenBalancesQuery = {
network: networkId,
address,
withBRC20Tokens: true,
};
if (xpub) {
Object.assign(query, { xpub });
Expand Down
122 changes: 72 additions & 50 deletions packages/engine/src/vaults/utils/btcForkChain/VaultBtcFork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,10 @@ export default class VaultBtcFork extends VaultBase {
from: 'unknown',
to: dbAccount.address,
amount: '0',
asset: output.inscriptions[0],
asset: {
...output.inscriptions[0],
type: NFTAssetType.BTC,
},
}),
);
} else {
Expand Down Expand Up @@ -1161,6 +1164,10 @@ export default class VaultBtcFork extends VaultBase {
tokenAddress: tokenIdOnNetwork,
});

// several history items with same txid
// merge them into one
const txIdSet = new Set<string>();

const promises = history.map(async (tx) => {
try {
const historyTxToMerge = localHistory.find(
Expand All @@ -1171,62 +1178,77 @@ export default class VaultBtcFork extends VaultBase {
return null;
}

const {
fromAddress,
toAddress,
time,
token: tick,
actionType,
amount,
} = tx;
if (txIdSet.has(tx.txId)) {
return null;
}

const tokenId = `brc-20--${tick}`;
txIdSet.add(tx.txId);

const tokenInfo = await this.engine.findToken({
networkId: this.networkId,
tokenIdOnNetwork: tokenId,
});
const txsWithSameTxId = history.filter(
(item) => item.txId === tx.txId,
);

const actions: IDecodedTxAction[] = [];
const isInscribeTransfer = actionType === 'inscribeTransfer';

if (tokenInfo) {
const action = this.buildBRC20TokenAction({
nftInfo: {
from: isInscribeTransfer ? toAddress : fromAddress,
to: toAddress,
amount,
asset: {
type: NFTAssetType.BTC,
inscription_id: tx.inscriptionId,
inscription_number: new BigNumber(
tx.inscriptionNumber,
).toNumber(),
tx_hash: tx.txId,
content: '',
content_length: 0,
content_type: '',
timestamp: tx.time,
output: tx.index,
owner: '',
output_value_sat: 0,
genesis_transaction_hash: '',
location: tx.location,
contentUrl: '',
},
},
dbAccount,
token: tokenInfo,
brc20Content: {
p: 'brc20',
op: actionType,
tick,
amt: amount,
},

for (let i = 0, len = txsWithSameTxId.length; i < len; i += 1) {
const {
fromAddress,
toAddress,
time,
token: tick,
actionType,
amount,
} = txsWithSameTxId[i];

const tokenId = `brc-20--${tick}`;

const tokenInfo = await this.engine.findToken({
networkId: this.networkId,
tokenIdOnNetwork: tokenId,
});
actions.push(action);

const isInscribeTransfer = actionType === 'inscribeTransfer';

if (tokenInfo) {
const action = this.buildBRC20TokenAction({
nftInfo: {
from: isInscribeTransfer ? toAddress : fromAddress,
to: toAddress,
amount,
asset: {
type: NFTAssetType.BTC,
inscription_id: tx.inscriptionId,
inscription_number: new BigNumber(
tx.inscriptionNumber,
).toNumber(),
tx_hash: tx.txId,
content: '',
content_length: 0,
content_type: '',
timestamp: tx.time,
output: tx.index,
owner: '',
output_value_sat: 0,
genesis_transaction_hash: '',
location: tx.location,
contentUrl: '',
},
},
dbAccount,
token: tokenInfo,
brc20Content: {
p: 'brc20',
op: actionType,
tick,
amt: amount,
},
});
actions.push(action);
}
}

const { time } = txsWithSameTxId[0];

const decodedTx: IDecodedTx = {
txid: tx.txId,
owner: dbAccount.address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ function BTCAssetDetailContent({

const [asset, updateAsset] = useState(outerAsset);
const isDisabled =
wallet?.type === WALLET_TYPE_WATCHING ||
asset.owner !== outerAsset.accountAddress ||
!accountId ||
!isOwner;
wallet?.type === WALLET_TYPE_WATCHING || !accountId || !isOwner;

const sendAction = useCallback(() => {
if (!networkId || !accountId) {
Expand Down

0 comments on commit 1f6b69f

Please sign in to comment.