Skip to content

Commit

Permalink
[Typescript SDK] add MergeCoin and SplitCoin (MystenLabs#2354)
Browse files Browse the repository at this point in the history
* add functions and declarations

* Fix typo: sui_SplitCoin -> sui_splitCoin

* Fix data schema

Co-authored-by: Ge Gao <gegao@Ges-MacBook-Pro.local>
Co-authored-by: Chris Li <666lcz@gmail.com>
  • Loading branch information
3 people authored Jun 1, 2022
1 parent 6e8e0c9 commit 3a7392e
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 5 deletions.
52 changes: 49 additions & 3 deletions sdk/typescript/src/index.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Generated type guards for "index.ts".
* WARNING: Do not manually change this file.
*/
import { Ed25519KeypairData, Keypair, PublicKeyInitData, PublicKeyData, TransferCoinTransaction, MoveCallTransaction, TxnDataSerializer, TransactionDigest, SuiAddress, ObjectOwner, SuiObjectRef, SuiObjectInfo, ObjectContentFields, MovePackageContent, SuiData, SuiMoveObject, SuiMovePackage, SuiObject, ObjectStatus, ObjectType, GetOwnedObjectsResponse, GetObjectDataResponse, ObjectDigest, ObjectId, SequenceNumber, TransferCoin, RawAuthoritySignInfo, TransactionKindName, SuiTransactionKind, TransactionData, EpochId, AuthorityQuorumSignInfo, CertifiedTransaction, GasCostSummary, ExecutionStatusType, ExecutionStatus, OwnedObjectRef, TransactionEffects, TransactionEffectsResponse, GatewayTxSeqNumber, GetTxnDigestsResponse, Event, MoveCall, SuiJsonValue, EmptySignInfo, AuthorityName, AuthoritySignature, TransactionBytes, SplitCoinResponse, TransactionResponse } from "./index";
import { Ed25519KeypairData, Keypair, PublicKeyInitData, PublicKeyData, TransferCoinTransaction, MergeCoinTransaction, SplitCoinTransaction, MoveCallTransaction, TxnDataSerializer, TransactionDigest, SuiAddress, ObjectOwner, SuiObjectRef, SuiObjectInfo, ObjectContentFields, MovePackageContent, SuiData, SuiMoveObject, SuiMovePackage, SuiObject, ObjectStatus, ObjectType, GetOwnedObjectsResponse, GetObjectDataResponse, ObjectDigest, ObjectId, SequenceNumber, TransferCoin, RawAuthoritySignInfo, TransactionKindName, SuiTransactionKind, TransactionData, EpochId, AuthorityQuorumSignInfo, CertifiedTransaction, GasCostSummary, ExecutionStatusType, ExecutionStatus, OwnedObjectRef, TransactionEffects, TransactionEffectsResponse, GatewayTxSeqNumber, GetTxnDigestsResponse, Event, MoveCall, SuiJsonValue, EmptySignInfo, AuthorityName, AuthoritySignature, TransactionBytes, MergeCoinResponse, SplitCoinResponse, TransactionResponse } from "./index";
import { BN } from "bn.js";

export function isEd25519KeypairData(obj: any, _argumentName?: string): obj is Ed25519KeypairData {
Expand Down Expand Up @@ -64,6 +64,35 @@ export function isTransferCoinTransaction(obj: any, _argumentName?: string): obj
)
}

export function isMergeCoinTransaction(obj: any, _argumentName?: string): obj is MergeCoinTransaction {
return (
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isTransactionDigest(obj.primaryCoin) as boolean &&
isTransactionDigest(obj.coinToMerge) as boolean &&
(typeof obj.gasPayment === "undefined" ||
isTransactionDigest(obj.gasPayment) as boolean) &&
isSequenceNumber(obj.gasBudget) as boolean
)
}

export function isSplitCoinTransaction(obj: any, _argumentName?: string): obj is SplitCoinTransaction {
return (
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isTransactionDigest(obj.coinObjectId) as boolean &&
Array.isArray(obj.splitAmounts) &&
obj.splitAmounts.every((e: any) =>
isSequenceNumber(e) as boolean
) &&
(typeof obj.gasPayment === "undefined" ||
isTransactionDigest(obj.gasPayment) as boolean) &&
isSequenceNumber(obj.gasBudget) as boolean
)
}

export function isMoveCallTransaction(obj: any, _argumentName?: string): obj is MoveCallTransaction {
return (
(obj !== null &&
Expand Down Expand Up @@ -92,7 +121,9 @@ export function isTxnDataSerializer(obj: any, _argumentName?: string): obj is Tx
typeof obj === "object" ||
typeof obj === "function") &&
typeof obj.newTransferCoin === "function" &&
typeof obj.newMoveCall === "function"
typeof obj.newMoveCall === "function" &&
typeof obj.newMergeCoin === "function" &&
typeof obj.newSplitCoin === "function"
)
}

Expand Down Expand Up @@ -549,6 +580,17 @@ export function isTransactionBytes(obj: any, _argumentName?: string): obj is Tra
)
}

export function isMergeCoinResponse(obj: any, _argumentName?: string): obj is MergeCoinResponse {
return (
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isCertifiedTransaction(obj.certificate) as boolean &&
isSuiObject(obj.updatedCoin) as boolean &&
isSuiObject(obj.updatedGas) as boolean
)
}

export function isSplitCoinResponse(obj: any, _argumentName?: string): obj is SplitCoinResponse {
return (
(obj !== null &&
Expand All @@ -573,6 +615,10 @@ export function isTransactionResponse(obj: any, _argumentName?: string): obj is
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isSplitCoinResponse(obj.SplitCoinResponse) as boolean)
isSplitCoinResponse(obj.SplitCoinResponse) as boolean ||
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isMergeCoinResponse(obj.MergeCoinResponse) as boolean)
)
}
30 changes: 30 additions & 0 deletions sdk/typescript/src/signers/signer-with-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { SignaturePubkeyPair, Signer } from './signer';
import { RpcTxnDataSerializer } from './txn-data-serializers/rpc-txn-data-serializer';
import {
MoveCallTransaction,
MergeCoinTransaction,
SplitCoinTransaction,
TransferCoinTransaction,
TxnDataSerializer,
} from './txn-data-serializers/txn-data-serializer';
Expand Down Expand Up @@ -77,6 +79,34 @@ export abstract class SignerWithProvider implements Signer {
return await this.signAndExecuteTransaction(txBytes);
}

/**
* Serialize and Sign a `MergeCoin` transaction and submit to the Gateway for execution
*/
async mergeCoin(
transaction: MergeCoinTransaction
): Promise<TransactionResponse> {
const signerAddress = await this.getAddress();
const txBytes = await this.serializer.newMergeCoin(
signerAddress,
transaction
);
return await this.signAndExecuteTransaction(txBytes);
}

/**
* Serialize and Sign a `SplitCoin` transaction and submit to the Gateway for execution
*/
async splitCoin(
transaction: SplitCoinTransaction
): Promise<TransactionResponse> {
const signerAddress = await this.getAddress();
const txBytes = await this.serializer.newSplitCoin(
signerAddress,
transaction
);
return await this.signAndExecuteTransaction(txBytes);
}

/**
* Serialize and Sign a `MoveCall` transaction and submit to the Gateway for execution
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Base64DataBuffer } from '../../serialization/base64';
import { SuiAddress } from '../../types';
import {
MoveCallTransaction,
MergeCoinTransaction,
SplitCoinTransaction,
TransferCoinTransaction,
TxnDataSerializer,
} from './txn-data-serializer';
Expand Down Expand Up @@ -71,5 +73,47 @@ export class RpcTxnDataSerializer implements TxnDataSerializer {
}
}

// TODO: add more interface methods
async newMergeCoin(
signerAddress: SuiAddress,
t: MergeCoinTransaction
): Promise<Base64DataBuffer> {
try {
const resp = await this.client.requestWithType(
'sui_mergeCoins',
[
signerAddress,
t.primaryCoin,
t.coinToMerge,
t.gasPayment,
t.gasBudget,
],
isTransactionBytes
);
return new Base64DataBuffer(resp.txBytes);
} catch (err) {
throw new Error(`Error merging coin: ${err}`);
}
}

async newSplitCoin(
signerAddress: SuiAddress,
t: SplitCoinTransaction
): Promise<Base64DataBuffer> {
try {
const resp = await this.client.requestWithType(
'sui_splitCoin',
[
signerAddress,
t.coinObjectId,
t.splitAmounts,
t.gasPayment,
t.gasBudget,
],
isTransactionBytes
);
return new Base64DataBuffer(resp.txBytes);
} catch (err) {
throw new Error(`Error splitting coin: ${err}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ export interface TransferCoinTransaction {
recipient: SuiAddress;
}

export interface MergeCoinTransaction {
primaryCoin: ObjectId;
coinToMerge: ObjectId;
gasPayment?: ObjectId;
gasBudget: number;
}

export interface SplitCoinTransaction {
coinObjectId: ObjectId;
splitAmounts: number[];
gasPayment?: ObjectId;
gasBudget: number;
}

export interface MoveCallTransaction {
packageObjectId: ObjectId;
module: string;
Expand All @@ -39,5 +53,13 @@ export interface TxnDataSerializer {
txn: MoveCallTransaction
): Promise<Base64DataBuffer>;

// TODO: add `newSplitCoin` and `newMergeCoin`
newMergeCoin(
signerAddress: SuiAddress,
txn: MergeCoinTransaction
): Promise<Base64DataBuffer>;

newSplitCoin(
signerAddress: SuiAddress,
txn: SplitCoinTransaction
): Promise<Base64DataBuffer>;
}
9 changes: 9 additions & 0 deletions sdk/typescript/src/types/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ export type TransactionBytes = {
// TODO: Add input_objects field
};

export type MergeCoinResponse = {
certificate: CertifiedTransaction;
updatedCoin: SuiObject;
updatedGas: SuiObject;
};

export type SplitCoinResponse = {
certificate: CertifiedTransaction;
updatedCoin: SuiObject;
Expand All @@ -140,6 +146,9 @@ export type TransactionResponse =
}
| {
SplitCoinResponse: SplitCoinResponse;
}
| {
MergeCoinResponse: MergeCoinResponse;
};

/* -------------------------------------------------------------------------- */
Expand Down

0 comments on commit 3a7392e

Please sign in to comment.