Skip to content

Commit

Permalink
feat(sdk-coin-sui): add more validations for custom tx
Browse files Browse the repository at this point in the history
  • Loading branch information
evanzbitgo committed Oct 7, 2023
1 parent b89bae4 commit 6809bfb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
9 changes: 5 additions & 4 deletions modules/sdk-coin-sui/src/lib/customTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {
import { Transaction } from './transaction';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import utils from './utils';
import { BaseKey, InvalidTransactionError, TransactionRecipient, TransactionType } from '@bitgo/sdk-core';
import { BaseKey, InvalidTransactionError, Recipient, TransactionRecipient, TransactionType } from '@bitgo/sdk-core';
import { UNAVAILABLE_TEXT } from './constants';

export class CustomTransaction extends Transaction<CustomProgrammableTransaction> {
private _rawTransaction: string;
private _recipients: Recipient[];

constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
Expand Down Expand Up @@ -70,13 +71,13 @@ export class CustomTransaction extends Transaction<CustomProgrammableTransaction
return;
}

const recipients = utils.getRecipients(this._suiTransaction);
this._outputs = recipients.map((recipient, index) => ({
this._recipients = utils.getRecipients(this._suiTransaction);
this._outputs = this._recipients.map((recipient, index) => ({
address: recipient.address,
value: recipient.amount,
coin: this._coinConfig.name,
}));
const totalAmount = recipients.reduce((accumulator, current) => accumulator + Number(current.amount), 0);
const totalAmount = this._recipients.reduce((accumulator, current) => accumulator + Number(current.amount), 0);

this._inputs = [
{
Expand Down
21 changes: 15 additions & 6 deletions modules/sdk-coin-sui/src/lib/customTransactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TransactionBuilder } from './transactionBuilder';
import { CustomProgrammableTransaction, SuiTransaction, SuiTransactionType } from './iface';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { CustomTransaction } from './customTransaction';
import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core';
import { BuildTransactionError, InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
import { Transaction } from './transaction';
import assert from 'assert';
import utils from './utils';
Expand Down Expand Up @@ -63,18 +63,27 @@ export class CustomTransactionBuilder extends TransactionBuilder<CustomProgramma
}

/**
* Currently custom tx only allows a combination of 3 types of SUI transactions:
* 1. SplitCoins
* 2. TransferObjects
* 3. MoveCall
* @inheritdoc
*/
validateTransaction(transaction: CustomTransaction): void {
if (!transaction.suiTransaction) {
return;
}
this.validateTransactionFields();

// Check all the transaction types are supported
this.transaction.suiTransaction.tx.transactions.forEach((tx) => {
utils.getSuiTransactionType(tx);
});
try {
this.transaction.suiTransaction.tx.transactions.forEach((tx) => {
utils.getSuiTransactionType(tx);
});
} catch (e) {
if (e instanceof InvalidTransactionError) {
throw new BuildTransactionError(e.message);
}
throw e;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions modules/sdk-coin-sui/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ export class Utils implements BaseUtils {
} else if (command.target.endsWith(MethodNames.StakingPoolSplit)) {
return SuiTransactionType.CustomTx;
} else {
throw new InvalidTransactionError(`unsupported target method`);
throw new InvalidTransactionError(`unsupported target method ${command.target}`);
}
default:
throw new InvalidTransactionError(`unsupported transaction kind`);
throw new InvalidTransactionError(`unsupported transaction kind ${command.kind}`);
}
}

Expand Down

0 comments on commit 6809bfb

Please sign in to comment.