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

v4 refactor: dynex get unspent outputs #4803

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 2 additions & 42 deletions packages/engine/src/vaults/impl/dynex/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export default class Vault extends VaultBase {

const unspentOutputs = await this._collectUnspentOutputs();
const { inputs, finalAmount } = this._collectInputs({
unspentOutputs: Object.values(unspentOutputs),
unspentOutputs,
amount: new BigNumber(transferInfo.amount)
.shiftedBy(network.decimals)
.toFixed(),
Expand Down Expand Up @@ -470,48 +470,8 @@ export default class Vault extends VaultBase {
async _collectUnspentOutputs() {
const client = await this.getClient();
const accountAddress = await this.getAccountAddress();
const transactions = await client.getTransactionsByAddress(accountAddress);
const unspentOutputs: Record<
string,
{
prevIndex: number;
globalIndex: number;
txPubkey: string;
prevOutPubkey: string;
amount: number;
}
> = {};

for (let i = 0; i < transactions.length; i += 1) {
const tx = transactions[i];
const transaction = await client.getTransaction(tx.hash);

if (transaction.address_from === accountAddress) {
transaction.inputs.forEach((input) => {
const output =
unspentOutputs[
`${input.data.input.key_offsets[0]}_${input.data.input.amount}`
];
if (output && output.amount === input.data.input.amount) {
delete unspentOutputs[
`${input.data.input.key_offsets[0]}_${input.data.input.amount}`
];
}
});
}

transaction.outputs?.forEach((output, index) => {
if (output.address_to === accountAddress) {
unspentOutputs[`${output.globalIndex}_${output.output.amount}`] = {
prevIndex: index,
globalIndex: output.globalIndex,
txPubkey: transaction.extra.publicKey,
prevOutPubkey: output.output.target.data.key,
amount: output.output.amount,
};
}
});
}
const unspentOutputs = await client.getUnspentUTXOs(accountAddress);

return unspentOutputs;
}
Expand Down
28 changes: 28 additions & 0 deletions packages/engine/src/vaults/impl/dynex/helper/ClientDynex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import type {
IOnChainNodeInfo,
IOnChainTransaction,
IOnChainTransactionsItem,
IOnChainUnspentOutput,
IUnspentOutput,
} from '../types';

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -26,6 +28,7 @@ export enum RPC_METHODS {
GET_TRANSACTION = 'gettransaction',
GET_BLOCK_COUNT = 'getblockcount',
VALIDATE_ADDRESS = 'validateaddress',
GET_OUTPUT_BY_ADDRESS = 'getoutputsbyaddress',
}
// eslint-disable-next-line @typescript-eslint/naming-convention
export enum PARAMS_ENCODINGS {
Expand Down Expand Up @@ -142,6 +145,31 @@ export class ClientDynex extends BaseClient {
return resp.transactions;
}

async getUnspentUTXOs(address: string): Promise<IUnspentOutput[]> {
try {
const resp = await this.rpc.call<{
status: string;
outputs: IOnChainUnspentOutput[];
balance: number;
}>(RPC_METHODS.GET_OUTPUT_BY_ADDRESS, {
address,
});

this.checkDynexNodeResponse({ resp, method: 'getTransactionsByAddress' });

return resp.outputs.map((output) => ({
prevIndex: output.outputInTransaction,
globalIndex: output.globalOutputIndex,
txPubkey: output.transactionPublicKey,
prevOutPubkey: output.outputKey,
amount: output.amount,
}));
} catch (e) {
console.log(e);
return [];
}
}

override getInfo(): Promise<ClientInfo> {
throw new NotImplemented();
}
Expand Down
9 changes: 9 additions & 0 deletions packages/engine/src/vaults/impl/dynex/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,12 @@ export type ISignTxParams = {
fee: string;
paymentIdHex?: string;
};

export type IOnChainUnspentOutput = {
amount: number;
globalOutputIndex: number;
outputInTransaction: number;
outputKey: string;
transactionHash: string;
transactionPublicKey: string;
};
Loading