Skip to content

Commit

Permalink
v4 feat: add kaspa test case OK-25164 (#4045)
Browse files Browse the repository at this point in the history
* chore: add kaspa tx his test case

* chore: update inpage provider version
  • Loading branch information
ByteZhang1024 authored Jan 15, 2024
1 parent 7b0ffdb commit 2762d10
Show file tree
Hide file tree
Showing 5 changed files with 1,068 additions and 309 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@
"@cmdcode/crypto-utils": "1.9.5",
"@cmdcode/tapscript": "1.2.9",
"@legendapp/state": "^1.2.9",
"@onekeyfe/cross-inpage-provider-core": "1.1.52",
"@onekeyfe/cross-inpage-provider-errors": "1.1.52",
"@onekeyfe/cross-inpage-provider-injected": "1.1.52",
"@onekeyfe/cross-inpage-provider-types": "1.1.52",
"@onekeyfe/extension-bridge-hosted": "1.1.52",
"@onekeyfe/cross-inpage-provider-core": "1.1.53",
"@onekeyfe/cross-inpage-provider-errors": "1.1.53",
"@onekeyfe/cross-inpage-provider-injected": "1.1.53",
"@onekeyfe/cross-inpage-provider-types": "1.1.53",
"@onekeyfe/extension-bridge-hosted": "1.1.53",
"@onekeyfe/hd-ble-sdk": "0.3.37",
"@onekeyfe/hd-core": "0.3.37",
"@onekeyfe/hd-shared": "0.3.37",
"@onekeyfe/hd-transport": "0.3.37",
"@onekeyfe/hd-web-sdk": "0.3.37",
"@onekeyfe/onekey-cross-webview": "1.1.52",
"@onekeyfe/onekey-cross-webview": "1.1.53",
"@starcoin/starcoin": "2.1.5",
"@web3-react/core": "8.0.35-beta.0",
"@web3-react/empty": "8.0.20-beta.0",
Expand Down
127 changes: 22 additions & 105 deletions packages/engine/src/vaults/impl/kaspa/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ import {
selectUTXOs,
} from './sdk';
import { toTransaction } from './sdk/transaction';
import {
calculateTxFee,
convertInputUtxos,
convertOutputUtxos,
determineFromAddress,
determineToAddresses,
} from './sdk/transactionUtils';
import settings from './settings';

import type { Network } from '../../../types/network';
import type { Token } from '../../../types/token';
import type {
GetTransactionInput,
GetTransactionOutput,
GetTransactionResponse,
UnspentOutputInfo,
} from './sdk';
import type { UnspentOutputInfo } from './sdk';
import type { IEncodedTxKaspa } from './types';

// @ts-ignore
Expand Down Expand Up @@ -548,70 +549,6 @@ export default class Vault extends VaultBase {
return txids.map((txid) => txStatuses.get(txid));
}

convertInputUtxos(
utxos: GetTransactionInput[],
mineAddress: string,
tokenInfo: Token,
) {
return utxos.map((utxo) => ({
address: utxo.previous_outpoint_address,
balance: new BigNumber(utxo.previous_outpoint_amount?.toString())
.shiftedBy(-tokenInfo.decimals)
.toFixed(),
balanceValue: utxo.previous_outpoint_amount?.toString(),
symbol: tokenInfo.symbol,
isMine: utxo.previous_outpoint_address === mineAddress,
}));
}

convertOutputUtxos(
utxos: GetTransactionOutput[],
mineAddress: string,
tokenInfo: Token,
) {
return utxos.map((utxo) => ({
address: utxo.script_public_key_address,
balance: new BigNumber(utxo.amount?.toString())
.shiftedBy(-tokenInfo.decimals)
.toFixed(),
balanceValue: utxo.amount?.toString(),
symbol: tokenInfo.symbol,
isMine: utxo.script_public_key_address === mineAddress,
}));
}

determineFromAddress(
mineAddress: string,
hasSenderIncludeMine: boolean,
hasReceiverIncludeMine: boolean,
inputs: GetTransactionInput[],
) {
if (hasSenderIncludeMine && !hasReceiverIncludeMine) {
return mineAddress;
}
if (!hasSenderIncludeMine && hasReceiverIncludeMine) {
return (
inputs.find((input) => input.previous_outpoint_address !== mineAddress)
?.previous_outpoint_address || 'kaspa:00000000'
);
}
return mineAddress;
}

determineToAddresses(
mineAddress: string,
hasSenderIncludeMine: boolean,
hasReceiverIncludeMine: boolean,
outputs: GetTransactionOutput[],
) {
if (hasSenderIncludeMine && !hasReceiverIncludeMine) {
return outputs
.filter((output) => output.script_public_key_address !== mineAddress)
.map((output) => output.script_public_key_address);
}
return [mineAddress];
}

override async fetchOnChainHistory(options: {
tokenIdOnNetwork?: string;
localHistory?: IHistoryTx[];
Expand Down Expand Up @@ -646,29 +583,31 @@ export default class Vault extends VaultBase {
const mineAddress = dbAccount.address;
try {
const { inputs, outputs } = tx;

const actions: IDecodedTxAction[] = [];

const hasSenderIncludeMine = inputs.some(
const hasSenderIncludeMine = inputs?.some(
(input) => input.previous_outpoint_address === mineAddress,
);
const hasReceiverIncludeMine = outputs.some(
const hasReceiverIncludeMine = outputs?.some(
(output) => output.script_public_key_address === mineAddress,
);

const utxoFrom = this.convertInputUtxos(inputs, mineAddress, token);
const utxoTo = this.convertOutputUtxos(outputs, mineAddress, token);
const utxoFrom = convertInputUtxos(inputs, mineAddress, token);
const utxoTo = convertOutputUtxos(outputs, mineAddress, token);

const from = this.determineFromAddress(
const from = determineFromAddress(
mineAddress,
hasSenderIncludeMine,
hasReceiverIncludeMine,
inputs,
);
const toAddresses = this.determineToAddresses(
const toAddresses = determineToAddresses(
mineAddress,
hasSenderIncludeMine,
hasReceiverIncludeMine,
outputs,
inputs,
);

toAddresses.forEach((to) => {
Expand Down Expand Up @@ -698,34 +637,12 @@ export default class Vault extends VaultBase {
});
});

let nativeFee = '';
try {
const inputAmount = tx.inputs.reduce(
(acc, input) => acc.plus(input.previous_outpoint_amount.toString()),
new BigNumber(0),
);

const outputAmount = tx.outputs
.filter(
(output) =>
output.script_public_key_address === dbAccount.address,
)
.reduce(
(acc, output) => acc.plus(output.amount.toString()),
new BigNumber(0),
);

if (inputAmount.isLessThanOrEqualTo(0)) {
nativeFee = '0';
} else {
nativeFee = inputAmount
.minus(outputAmount)
.shiftedBy(-decimals)
.toFixed();
}
} catch {
nativeFee = new BigNumber(tx.mass).shiftedBy(-decimals).toFixed();
}
const nativeFee = calculateTxFee({
mass: tx.mass,
inputs,
outputs,
decimals: token.decimals,
});

const decodedTx: IDecodedTx = {
txid: tx.transaction_id,
Expand Down
Loading

0 comments on commit 2762d10

Please sign in to comment.