After trying to recuparate money from cold wallet21

This commit is contained in:
Your Name 2026-08-29 18:56:51 +02:00
parent fec413a94b
commit 3bf1a28042
2 changed files with 122 additions and 6 deletions

View File

@ -120,6 +120,24 @@ export async function getTransactionReceipt(txHash: string): Promise<TxReceiptSu
};
}
/** Native BNB transfer details (for cold↔hot gas funding confirm). */
export async function getNativeTransfer(txHash: string): Promise<{
status: number;
from: string;
to: string | null;
value: bigint;
} | null> {
const p = getProvider();
const [tx, receipt] = await Promise.all([p.getTransaction(txHash), p.getTransactionReceipt(txHash)]);
if (!tx || !receipt) return null;
return {
status: receipt.status === null ? 0 : Number(receipt.status),
from: tx.from,
to: tx.to,
value: tx.value,
};
}
type TransferHit = {
txHash: string;
from: string;

View File

@ -409,12 +409,17 @@ export async function confirmColdToHotFunding(
fundingId: string,
blockchainTxId: string
) {
if (!blockchainTxId?.trim()) throw new Error('blockchainTxId is required');
const row = await exchangeFundingRepository.confirmFunding(
fundingId,
blockchainTxId.trim(),
adminUserId
);
const txId = blockchainTxId?.trim();
if (!txId) throw new Error('blockchainTxId is required');
const funding = await exchangeFundingRepository.findFundingById(fundingId);
if (!funding || funding.status !== 'pending') {
throw new Error('Funding request not found or not pending');
}
await assertFundingTxMatchesRequest(funding, txId);
const row = await exchangeFundingRepository.confirmFunding(fundingId, txId, adminUserId);
if (!row) throw new Error('Funding request not found or not pending');
return {
id: row.id,
@ -423,6 +428,99 @@ export async function confirmColdToHotFunding(
};
}
function addrEq(a: string | null | undefined, b: string | null | undefined): boolean {
if (!a || !b) return false;
const na = a.trim();
const nb = b.trim();
if (na.startsWith('0x') || nb.startsWith('0x')) return na.toLowerCase() === nb.toLowerCase();
return na === nb;
}
/**
* Reject Confirm when the pasted tx does not match the pending funding row
* (wrong amount / addresses / asset) so history stays consistent with chain.
*/
async function assertFundingTxMatchesRequest(
funding: exchangeFundingRepository.ExchangeFundingRow,
txId: string
): Promise<void> {
const walletType = await walletRepository.getWalletTypeById(funding.wallet_type_id);
if (!walletType) throw new Error('Unknown wallet type for funding request');
const expectedAmount = BigInt(funding.amount);
const isGas =
funding.direction === 'cold_to_hot_gas' || funding.direction === 'hot_to_cold_gas';
if (walletType.network === 'BSC' || walletType.code === USDT_BEP20_CODE) {
if (!/^0x[0-9a-fA-F]{64}$/.test(txId)) {
throw new Error('Invalid BSC transaction hash (expected 0x + 64 hex chars)');
}
const receipt = await bscService.getTransactionReceipt(txId);
if (!receipt) throw new Error('Transaction not found on BSC (wait for confirmation, then retry)');
if (receipt.status !== 1) throw new Error('Transaction failed on-chain');
if (isGas) {
const native = await bscService.getNativeTransfer(txId);
if (!native) throw new Error('Transaction not found on BSC (wait for confirmation, then retry)');
if (native.status !== 1) throw new Error('Transaction failed on-chain');
if (!addrEq(native.from, funding.from_address)) {
throw new Error(
`Tx from ${native.from} does not match funding from ${funding.from_address}`
);
}
if (!addrEq(native.to, funding.to_address)) {
throw new Error(`Tx to ${native.to} does not match funding to ${funding.to_address}`);
}
if (native.value !== expectedAmount) {
throw new Error(
`On-chain amount ${native.value.toString()} wei does not match funding request ${expectedAmount.toString()} wei. Cancel and recreate the request with the real amount, or send exactly the requested amount.`
);
}
return;
}
const contract = bscService.resolveUsdtContract(walletType.contract_address).toLowerCase();
const match = receipt.transfers.find(
(t) =>
t.contract.toLowerCase() === contract &&
addrEq(t.from, funding.from_address) &&
addrEq(t.to, funding.to_address) &&
t.value === expectedAmount
);
if (!match) {
const usdtToHot = receipt.transfers.filter(
(t) =>
t.contract.toLowerCase() === contract &&
addrEq(t.to, funding.to_address)
);
if (usdtToHot.length === 0) {
throw new Error(
'No USDT BEP20 Transfer to the hot wallet found in this tx. Check the hash and destination.'
);
}
const got = usdtToHot.map((t) => t.value.toString()).join(', ');
throw new Error(
`On-chain USDT amount(s) [${got}] do not match funding request ${expectedAmount.toString()} (raw). ` +
`Send exactly the requested amount, or Cancel this request and create a new one for the amount you actually sent.`
);
}
return;
}
if (walletType.network === 'TRON') {
// TRON receipt verification is limited here; still require a plausible tx id length.
if (!/^[0-9a-fA-F]{64}$/.test(txId)) {
throw new Error('Invalid TRON transaction id (expected 64 hex characters)');
}
console.warn(
`[treasury] confirm ${funding.id}: TRON tx ${txId} accepted without full amount verify yet`
);
return;
}
throw new Error(`Unsupported network for funding confirm: ${walletType.network}`);
}
export async function listFunding(opts: { status?: string; limit?: number; offset?: number }) {
const result = await exchangeFundingRepository.listFunding(opts);
return {