Skip to content

Commit 1f6b69f

Browse files
authored
fix: issues OK-22986 & fetch token balances with 'withBRC20Tokens=true' (#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
1 parent 62302bf commit 1f6b69f

File tree

3 files changed

+75
-54
lines changed

3 files changed

+75
-54
lines changed

packages/engine/src/apiProxyUtils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type TokenBalancesQuery = {
99
// eslint-disable-next-line camelcase
1010
contract_addresses?: string[];
1111
xpub?: string;
12+
withBRC20Tokens?: boolean;
1213
};
1314

1415
export type TokenBalancesResponse = {
@@ -38,6 +39,7 @@ export const getBalancesFromApi = async ({
3839
const query: TokenBalancesQuery = {
3940
network: networkId,
4041
address,
42+
withBRC20Tokens: true,
4143
};
4244
if (xpub) {
4345
Object.assign(query, { xpub });

packages/engine/src/vaults/utils/btcForkChain/VaultBtcFork.ts

Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,10 @@ export default class VaultBtcFork extends VaultBase {
518518
from: 'unknown',
519519
to: dbAccount.address,
520520
amount: '0',
521-
asset: output.inscriptions[0],
521+
asset: {
522+
...output.inscriptions[0],
523+
type: NFTAssetType.BTC,
524+
},
522525
}),
523526
);
524527
} else {
@@ -1161,6 +1164,10 @@ export default class VaultBtcFork extends VaultBase {
11611164
tokenAddress: tokenIdOnNetwork,
11621165
});
11631166

1167+
// several history items with same txid
1168+
// merge them into one
1169+
const txIdSet = new Set<string>();
1170+
11641171
const promises = history.map(async (tx) => {
11651172
try {
11661173
const historyTxToMerge = localHistory.find(
@@ -1171,62 +1178,77 @@ export default class VaultBtcFork extends VaultBase {
11711178
return null;
11721179
}
11731180

1174-
const {
1175-
fromAddress,
1176-
toAddress,
1177-
time,
1178-
token: tick,
1179-
actionType,
1180-
amount,
1181-
} = tx;
1181+
if (txIdSet.has(tx.txId)) {
1182+
return null;
1183+
}
11821184

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

1185-
const tokenInfo = await this.engine.findToken({
1186-
networkId: this.networkId,
1187-
tokenIdOnNetwork: tokenId,
1188-
});
1187+
const txsWithSameTxId = history.filter(
1188+
(item) => item.txId === tx.txId,
1189+
);
11891190

11901191
const actions: IDecodedTxAction[] = [];
1191-
const isInscribeTransfer = actionType === 'inscribeTransfer';
1192-
1193-
if (tokenInfo) {
1194-
const action = this.buildBRC20TokenAction({
1195-
nftInfo: {
1196-
from: isInscribeTransfer ? toAddress : fromAddress,
1197-
to: toAddress,
1198-
amount,
1199-
asset: {
1200-
type: NFTAssetType.BTC,
1201-
inscription_id: tx.inscriptionId,
1202-
inscription_number: new BigNumber(
1203-
tx.inscriptionNumber,
1204-
).toNumber(),
1205-
tx_hash: tx.txId,
1206-
content: '',
1207-
content_length: 0,
1208-
content_type: '',
1209-
timestamp: tx.time,
1210-
output: tx.index,
1211-
owner: '',
1212-
output_value_sat: 0,
1213-
genesis_transaction_hash: '',
1214-
location: tx.location,
1215-
contentUrl: '',
1216-
},
1217-
},
1218-
dbAccount,
1219-
token: tokenInfo,
1220-
brc20Content: {
1221-
p: 'brc20',
1222-
op: actionType,
1223-
tick,
1224-
amt: amount,
1225-
},
1192+
1193+
for (let i = 0, len = txsWithSameTxId.length; i < len; i += 1) {
1194+
const {
1195+
fromAddress,
1196+
toAddress,
1197+
time,
1198+
token: tick,
1199+
actionType,
1200+
amount,
1201+
} = txsWithSameTxId[i];
1202+
1203+
const tokenId = `brc-20--${tick}`;
1204+
1205+
const tokenInfo = await this.engine.findToken({
1206+
networkId: this.networkId,
1207+
tokenIdOnNetwork: tokenId,
12261208
});
1227-
actions.push(action);
1209+
1210+
const isInscribeTransfer = actionType === 'inscribeTransfer';
1211+
1212+
if (tokenInfo) {
1213+
const action = this.buildBRC20TokenAction({
1214+
nftInfo: {
1215+
from: isInscribeTransfer ? toAddress : fromAddress,
1216+
to: toAddress,
1217+
amount,
1218+
asset: {
1219+
type: NFTAssetType.BTC,
1220+
inscription_id: tx.inscriptionId,
1221+
inscription_number: new BigNumber(
1222+
tx.inscriptionNumber,
1223+
).toNumber(),
1224+
tx_hash: tx.txId,
1225+
content: '',
1226+
content_length: 0,
1227+
content_type: '',
1228+
timestamp: tx.time,
1229+
output: tx.index,
1230+
owner: '',
1231+
output_value_sat: 0,
1232+
genesis_transaction_hash: '',
1233+
location: tx.location,
1234+
contentUrl: '',
1235+
},
1236+
},
1237+
dbAccount,
1238+
token: tokenInfo,
1239+
brc20Content: {
1240+
p: 'brc20',
1241+
op: actionType,
1242+
tick,
1243+
amt: amount,
1244+
},
1245+
});
1246+
actions.push(action);
1247+
}
12281248
}
12291249

1250+
const { time } = txsWithSameTxId[0];
1251+
12301252
const decodedTx: IDecodedTx = {
12311253
txid: tx.txId,
12321254
owner: dbAccount.address,

packages/kit/src/views/Wallet/NFT/NFTDetail/Components/BTCAsset/BTCAssetDetailContent.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ function BTCAssetDetailContent({
9595

9696
const [asset, updateAsset] = useState(outerAsset);
9797
const isDisabled =
98-
wallet?.type === WALLET_TYPE_WATCHING ||
99-
asset.owner !== outerAsset.accountAddress ||
100-
!accountId ||
101-
!isOwner;
98+
wallet?.type === WALLET_TYPE_WATCHING || !accountId || !isOwner;
10299

103100
const sendAction = useCallback(() => {
104101
if (!networkId || !accountId) {

0 commit comments

Comments
 (0)