Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix too many vins on nexa(OK-27699) #4559

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
originalix marked this conversation as resolved.
Show resolved Hide resolved
(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
Loading