Skip to content

Commit

Permalink
fix: fix too many vins on nexa (#4559)
Browse files Browse the repository at this point in the history
  • Loading branch information
huhuanming authored May 10, 2024
1 parent 2926be5 commit 66d3e67
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
21 changes: 21 additions & 0 deletions packages/engine/src/vaults/VaultBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,27 @@ export abstract class VaultBase extends VaultBaseChainOnly {
return nextNonce;
}

getConfirmedUTXOs<T extends { value: string | number }>(
utxos: T[],
amount: string,
): T[] {
const confirmedUTXOs = utxos.sort((a, b) =>
new BigNumber(b.value).gt(a.value) ? 1 : -1,
);
let sum = new BigNumber(0);
let i = 0;
for (i = 0; i < confirmedUTXOs.length; i += 1) {
sum = sum.plus(confirmedUTXOs[i].value);
if (sum.gt(amount)) {
break;
}
}
if (sum.lt(amount)) {
return [];
}
return confirmedUTXOs.slice(0, i + 1);
}

validateSendAmount(amount: string, tokenBalance: string, to: string) {
return Promise.resolve(true);
}
Expand Down
48 changes: 39 additions & 9 deletions packages/engine/src/vaults/impl/nexa/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ import type {
} from '../../../types/provider';
import type { Token } from '../../../types/token';
import type { KeyringSoftwareBase } from '../../keyring/KeyringSoftwareBase';
import type { IDecodedTxLegacy, IHistoryTx, ISignedTxPro } from '../../types';
import type {
IDecodedTxLegacy,
IHistoryTx,
ISignCredentialOptions,
ISignedTxPro,
} from '../../types';
import type { EVMDecodedItem } from '../evm/decoder/types';
import type { IEncodedTxNexa, INexaTransaction } from './types';
import type { IEncodedTxNexa, IListUTXO, INexaTransaction } from './types';

export default class Vault extends VaultBase {
keyringMap = {
Expand Down Expand Up @@ -243,13 +248,12 @@ export default class Vault extends VaultBase {
): Promise<IEncodedTxNexa> {
const client = await this.getSDKClient();
const fromNexaAddress = transferInfo.from;
const utxos = (await client.getNexaUTXOs(fromNexaAddress)).filter(
const uxtos = (await client.getNexaUTXOs(fromNexaAddress)).filter(
(value) => !value.has_token,
);

const network = await this.getNetwork();
return {
inputs: utxos.map((utxo) => ({
inputs: uxtos.map((utxo) => ({
txId: utxo.outpoint_hash,
outputIndex: utxo.tx_pos,
satoshis: new BigNumber(utxo.value).toFixed(),
Expand Down Expand Up @@ -284,15 +288,41 @@ export default class Vault extends VaultBase {
return Promise.resolve(encodedTx);
}

override buildUnsignedTxFromEncodedTx(
override async buildUnsignedTxFromEncodedTx(
encodedTx: IEncodedTxNexa,
): Promise<IUnsignedTxPro> {
return Promise.resolve({
const client = await this.getSDKClient();
const network = await this.getNetwork();
const confirmedUTXOs = this.getConfirmedUTXOs(
encodedTx.inputs.map((input) => ({
...input,
value: input.satoshis,
})),
new BigNumber(encodedTx.transferInfo?.amount || 0)
.shiftedBy(network.decimals)
.plus(encodedTx?.gas || 0)
.toFixed(),
);

if (confirmedUTXOs.length > client.MAX_TX_NUM_VIN) {
const maximumAmount = confirmedUTXOs
.slice(0, client.MAX_TX_NUM_VIN)
.reduce((acc, cur) => acc.plus(cur.value), new BigNumber(0));
throw new OneKeyInternalError(
`Too many vins, The maximum amount for this transfer is ${maximumAmount
.shiftedBy(-network.decimals)
.toFixed()} ${network.symbol}.`,
);
}
encodedTx.inputs = confirmedUTXOs;
return {
inputs: [],
outputs: [],
payload: { encodedTx },
payload: {
encodedTx,
},
encodedTx,
});
};
}

override async getTransactionStatuses(
Expand Down
2 changes: 2 additions & 0 deletions packages/engine/src/vaults/impl/nexa/sdk/nexa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type { IListUTXO, INexaHistoryItem, INexaTransaction } from '../types';
export class Nexa extends SimpleClient {
readonly rpc: WebSocketRequest;

readonly MAX_TX_NUM_VIN = 256;

constructor(
url: string,
readonly defaultFinality: 'optimistic' | 'final' = 'optimistic',
Expand Down

0 comments on commit 66d3e67

Please sign in to comment.