Skip to content

Commit

Permalink
Typescript SDK fix (MystenLabs#4864)
Browse files Browse the repository at this point in the history
  • Loading branch information
gegaowp authored Sep 30, 2022
1 parent 67e2325 commit c012117
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function TxAddressHeader({ data }: { data: TxResultState }) {
// TODO: use a different icon
ChangeEpoch: CallTypeIcon,
TransferSui: TransferObjectTypeIcon,
Pay: TransferObjectTypeIcon,
};
const TxKindName = data.txKindName;
const Icon = TxTransferTypeIcon[TxKindName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const txKindToTxt: Record<TransactionKindName, string> = {
Publish: 'Publish',
TransferSui: 'Sui transfer',
ChangeEpoch: 'Change epoch',
Pay: 'Pay',
};

function TransactionDetailsPage() {
Expand Down
7 changes: 4 additions & 3 deletions apps/wallet/src/ui/app/redux/slices/sui-objects/Coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ export class Coin {
): Promise<SuiTransactionResponse> {
await signer.syncAccountState();
const coin = await Coin.selectCoin(signer, coins, amount);
return await signer.transferObject({
objectId: coin,
return await signer.pay({
inputCoins: [coin],
recipients: [recipient],
amounts: [Number(amount)],
gasBudget: DEFAULT_GAS_BUDGET_FOR_TRANSFER,
recipient: recipient,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ export class LocalTxnDataSerializer implements TxnDataSerializer {
t: PayTransaction
): Promise<Base64DataBuffer> {
try {
const inputCoinRefs = (
await Promise.all(
t.inputCoins.map((coin) => this.provider.getObjectRef(coin))
)
).map((ref) => ref!);
const tx = {
Pay: {
input_coins: t.inputCoins,
coins: inputCoinRefs,
recipients: t.recipients,
amounts: t.amounts,
},
Expand Down
37 changes: 31 additions & 6 deletions sdk/typescript/src/types/index.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Generated type guards for "index.ts".
* WARNING: Do not manually change this file.
*/
import { TransactionDigest, SuiAddress, ObjectOwner, SuiObjectRef, SuiObjectInfo, ObjectContentFields, MovePackageContent, SuiData, SuiMoveObject, SuiMovePackage, SuiMoveFunctionArgTypesResponse, SuiMoveFunctionArgType, SuiMoveFunctionArgTypes, SuiMoveNormalizedModules, SuiMoveNormalizedModule, SuiMoveModuleId, SuiMoveNormalizedStruct, SuiMoveStructTypeParameter, SuiMoveNormalizedField, SuiMoveNormalizedFunction, SuiMoveVisibility, SuiMoveTypeParameterIndex, SuiMoveAbilitySet, SuiMoveNormalizedType, SuiMoveNormalizedTypeParameterType, SuiMoveNormalizedStructType, SuiObject, ObjectStatus, ObjectType, GetOwnedObjectsResponse, GetObjectDataResponse, ObjectDigest, ObjectId, SequenceNumber, MoveEvent, PublishEvent, TransferObjectEvent, DeleteObjectEvent, NewObjectEvent, SuiEvent, MoveEventField, EventType, SuiEventFilter, SuiEventEnvelope, SuiEvents, SubscriptionId, SubscriptionEvent, TransferObject, SuiTransferSui, SuiChangeEpoch, ExecuteTransactionRequestType, TransactionKindName, SuiTransactionKind, SuiTransactionData, EpochId, GenericAuthoritySignature, AuthorityQuorumSignInfo, CertifiedTransaction, GasCostSummary, ExecutionStatusType, ExecutionStatus, OwnedObjectRef, TransactionEffects, SuiTransactionResponse, SuiCertifiedTransactionEffects, SuiExecuteTransactionResponse, GatewayTxSeqNumber, GetTxnDigestsResponse, MoveCall, SuiJsonValue, EmptySignInfo, AuthorityName, AuthoritySignature, TransactionBytes, SuiParsedMergeCoinResponse, SuiParsedSplitCoinResponse, SuiParsedPublishResponse, SuiPackage, SuiParsedTransactionResponse, DelegationData, DelegationSuiObject, TransferObjectTx, TransferSuiTx, PayTx, PublishTx, ObjectArg, CallArg, StructTag, TypeTag, MoveCallTx, Transaction, TransactionKind, TransactionData } from "./index";
import { TransactionDigest, SuiAddress, ObjectOwner, SuiObjectRef, SuiObjectInfo, ObjectContentFields, MovePackageContent, SuiData, SuiMoveObject, SuiMovePackage, SuiMoveFunctionArgTypesResponse, SuiMoveFunctionArgType, SuiMoveFunctionArgTypes, SuiMoveNormalizedModules, SuiMoveNormalizedModule, SuiMoveModuleId, SuiMoveNormalizedStruct, SuiMoveStructTypeParameter, SuiMoveNormalizedField, SuiMoveNormalizedFunction, SuiMoveVisibility, SuiMoveTypeParameterIndex, SuiMoveAbilitySet, SuiMoveNormalizedType, SuiMoveNormalizedTypeParameterType, SuiMoveNormalizedStructType, SuiObject, ObjectStatus, ObjectType, GetOwnedObjectsResponse, GetObjectDataResponse, ObjectDigest, ObjectId, SequenceNumber, MoveEvent, PublishEvent, TransferObjectEvent, DeleteObjectEvent, NewObjectEvent, SuiEvent, MoveEventField, EventType, SuiEventFilter, SuiEventEnvelope, SuiEvents, SubscriptionId, SubscriptionEvent, TransferObject, SuiTransferSui, SuiChangeEpoch, Pay, ExecuteTransactionRequestType, TransactionKindName, SuiTransactionKind, SuiTransactionData, EpochId, GenericAuthoritySignature, AuthorityQuorumSignInfo, CertifiedTransaction, GasCostSummary, ExecutionStatusType, ExecutionStatus, OwnedObjectRef, TransactionEffects, SuiTransactionResponse, SuiCertifiedTransactionEffects, SuiExecuteTransactionResponse, GatewayTxSeqNumber, GetTxnDigestsResponse, MoveCall, SuiJsonValue, EmptySignInfo, AuthorityName, AuthoritySignature, TransactionBytes, SuiParsedMergeCoinResponse, SuiParsedSplitCoinResponse, SuiParsedPublishResponse, SuiPackage, SuiParsedTransactionResponse, DelegationData, DelegationSuiObject, TransferObjectTx, TransferSuiTx, PayTx, PublishTx, ObjectArg, CallArg, StructTag, TypeTag, MoveCallTx, Transaction, TransactionKind, TransactionData } from "./index";

export function isTransactionDigest(obj: any, _argumentName?: string): obj is TransactionDigest {
return (
Expand Down Expand Up @@ -643,6 +643,26 @@ export function isSuiChangeEpoch(obj: any, _argumentName?: string): obj is SuiCh
)
}

export function isPay(obj: any, _argumentName?: string): obj is Pay {
return (
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
Array.isArray(obj.coins) &&
obj.coins.every((e: any) =>
isSuiObjectRef(e) as boolean
) &&
Array.isArray(obj.recipients) &&
obj.recipients.every((e: any) =>
isTransactionDigest(e) as boolean
) &&
Array.isArray(obj.amounts) &&
obj.amounts.every((e: any) =>
isSuiMoveTypeParameterIndex(e) as boolean
)
)
}

export function isExecuteTransactionRequestType(obj: any, _argumentName?: string): obj is ExecuteTransactionRequestType {
return (
(obj === "ImmediateReturn" ||
Expand All @@ -657,7 +677,8 @@ export function isTransactionKindName(obj: any, _argumentName?: string): obj is
obj === "TransferObject" ||
obj === "Call" ||
obj === "TransferSui" ||
obj === "ChangeEpoch")
obj === "ChangeEpoch" ||
obj === "Pay")
)
}

Expand All @@ -682,7 +703,11 @@ export function isSuiTransactionKind(obj: any, _argumentName?: string): obj is S
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isSuiChangeEpoch(obj.ChangeEpoch) as boolean)
isSuiChangeEpoch(obj.ChangeEpoch) as boolean ||
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isPay(obj.Pay) as boolean)
)
}

Expand Down Expand Up @@ -1142,9 +1167,9 @@ export function isPayTx(obj: any, _argumentName?: string): obj is PayTx {
(obj.Pay !== null &&
typeof obj.Pay === "object" ||
typeof obj.Pay === "function") &&
Array.isArray(obj.Pay.input_coins) &&
obj.Pay.input_coins.every((e: any) =>
isTransactionDigest(e) as boolean
Array.isArray(obj.Pay.coins) &&
obj.Pay.coins.every((e: any) =>
isSuiObjectRef(e) as boolean
) &&
Array.isArray(obj.Pay.recipients) &&
obj.Pay.recipients.every((e: any) =>
Expand Down
12 changes: 11 additions & 1 deletion sdk/typescript/src/types/sui-bcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,21 @@ export type TransferSuiTx = {
*/
export type PayTx = {
Pay: {
input_coins: string[];
coins: SuiObjectRef[];
recipients: string[];
amounts: number[];
};
};

bcs
.registerVectorType('vector<SuiAddress>', 'SuiAddress')
.registerVectorType('vector<SuiObjectRef>', 'SuiObjectRef')
.registerStructType('PayTx', {
coins: 'vector<SuiObjectRef>',
recipients: 'vector<SuiAddress>',
amounts: 'vector<u64>',
});

bcs.registerEnumType('Option<u64>', {
None: null,
Some: 'u64',
Expand Down Expand Up @@ -237,6 +246,7 @@ bcs.registerEnumType('Transaction', {
Publish: 'PublishTx',
Call: 'MoveCallTx',
TransferSui: 'TransferSuiTx',
Pay: 'PayTx',
});
/**
* Transaction kind - either Batch or Single.
Expand Down
17 changes: 15 additions & 2 deletions sdk/typescript/src/types/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export type SuiChangeEpoch = {
computation_charge: number;
};

export type Pay = {
coins: SuiObjectRef[];
recipients: SuiAddress[];
amounts: number[];
};

export type ExecuteTransactionRequestType =
| 'ImmediateReturn'
| 'WaitForTxCert'
Expand All @@ -32,13 +38,16 @@ export type TransactionKindName =
| 'Publish'
| 'Call'
| 'TransferSui'
| 'ChangeEpoch';
| 'ChangeEpoch'
| 'Pay';

export type SuiTransactionKind =
| { TransferObject: TransferObject }
| { Publish: SuiMovePackage }
| { Call: MoveCall }
| { TransferSui: SuiTransferSui }
| { ChangeEpoch: SuiChangeEpoch };
| { ChangeEpoch: SuiChangeEpoch }
| { Pay: Pay };
export type SuiTransactionData = {
transactions: SuiTransactionKind[];
sender: SuiAddress;
Expand Down Expand Up @@ -267,6 +276,10 @@ export function getTransferSuiTransaction(
return 'TransferSui' in data ? data.TransferSui : undefined;
}

export function getPayTransaction(data: SuiTransactionKind): Pay | undefined {
return 'Pay' in data ? data.Pay : undefined;
}

export function getChangeEpochTransaction(
data: SuiTransactionKind
): SuiChangeEpoch | undefined {
Expand Down

0 comments on commit c012117

Please sign in to comment.