Skip to content

Commit

Permalink
[Typescript SDK] Update data types (MystenLabs#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz authored Apr 28, 2022
1 parent 5f5a85b commit 1046a3b
Show file tree
Hide file tree
Showing 4 changed files with 2,372 additions and 7,903 deletions.
15 changes: 11 additions & 4 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, SignedTransaction, TransactionResponse, TransferTransaction, TxnDataSerializer, ObjectRef, ObjectExistsInfo, ObjectNotExistsInfo, ObjectStatus, GetOwnedObjectRefsResponse, GetObjectInfoResponse, ObjectDigest, ObjectId, SequenceNumber, RawObjectRef, Transfer, RawAuthoritySignInfo, SingleTransactionKind, TransactionKind, TransactionData, Transaction, CertifiedTransaction, TransactionDigest, GatewayTxSeqNumber, GetTxnDigestsResponse, MoveModulePublish, MoveTypeTag, MoveCall, EmptySignInfo, AuthorityName, AuthoritySignature } from "./index";
import { Ed25519KeypairData, Keypair, PublicKeyInitData, PublicKeyData, SignedTransaction, TransactionResponse, TransferTransaction, TxnDataSerializer, ObjectRef, ObjectExistsInfo, ObjectNotExistsInfo, ObjectStatus, ObjectType, GetOwnedObjectRefsResponse, GetObjectInfoResponse, ObjectDigest, ObjectId, SequenceNumber, RawObjectRef, Transfer, RawAuthoritySignInfo, SingleTransactionKind, TransactionKind, TransactionData, Transaction, CertifiedTransaction, TransactionDigest, GatewayTxSeqNumber, GetTxnDigestsResponse, MoveModulePublish, MoveTypeTag, MoveCall, EmptySignInfo, AuthorityName, AuthoritySignature } from "./index";
import { BN } from "bn.js";

export function isEd25519KeypairData(obj: any, _argumentName?: string): obj is Ed25519KeypairData {
Expand Down Expand Up @@ -106,7 +106,8 @@ export function isObjectExistsInfo(obj: any, _argumentName?: string): obj is Obj
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isObjectRef(obj.objectRef) as boolean
isObjectRef(obj.objectRef) as boolean &&
isObjectType(obj.objectType) as boolean
)
}

Expand All @@ -126,6 +127,13 @@ export function isObjectStatus(obj: any, _argumentName?: string): obj is ObjectS
)
}

export function isObjectType(obj: any, _argumentName?: string): obj is ObjectType {
return (
(obj === "moveObject" ||
obj === "movePackage")
)
}

export function isGetOwnedObjectRefsResponse(obj: any, _argumentName?: string): obj is GetOwnedObjectRefsResponse {
return (
(obj !== null &&
Expand Down Expand Up @@ -246,8 +254,7 @@ export function isTransaction(obj: any, _argumentName?: string): obj is Transact
typeof obj === "object" ||
typeof obj === "function") &&
isTransactionData(obj.data) as boolean &&
isTransactionResponse(obj.tx_signature) as boolean &&
isTransactionResponse(obj.auth_signature) as boolean
isTransactionResponse(obj.tx_signature) as boolean
)
}

Expand Down
79 changes: 44 additions & 35 deletions sdk/typescript/src/rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,36 @@ export class JsonRpcClient {
}

private createRpcClient(url: string, httpHeaders?: HttpHeaders): RpcClient {
const client = new RpcClient(async (request: any, callback: (arg0: Error | null, arg1?: string | undefined) => void) => {
const options = {
method: 'POST',
body: request,
headers: Object.assign(
{
'Content-Type': 'application/json',
},
httpHeaders || {}
),
};
const client = new RpcClient(
async (
request: any,
callback: (arg0: Error | null, arg1?: string | undefined) => void
) => {
const options = {
method: 'POST',
body: request,
headers: Object.assign(
{
'Content-Type': 'application/json',
},
httpHeaders || {}
),
};

try {
let res: Response = await fetch(url, options);
const text = await res.text();
if (res.ok) {
callback(null, text);
} else {
callback(new Error(`${res.status} ${res.statusText}: ${text}`));
try {
let res: Response = await fetch(url, options);
const text = await res.text();
if (res.ok) {
callback(null, text);
} else {
callback(new Error(`${res.status} ${res.statusText}: ${text}`));
}
} catch (err) {
if (err instanceof Error) callback(err);
}
} catch (err) {
if (err instanceof Error) callback(err);
}
}, {});
},
{}
);

return client;
}
Expand All @@ -55,10 +61,13 @@ export class JsonRpcClient {
if (isErrorResponse(response)) {
throw new Error(`RPC Error: ${response.error.message}`);
} else if (isValidResponse(response)) {
if (isT(response.result))
return response.result;
if (isT(response.result)) return response.result;
else
throw new Error(`RPC Error: result not of expected type`)
throw new Error(
`RPC Error: result not of expected type. Result received was: ${JSON.stringify(
response.result
)}`
);
}
throw new Error(`Unexpected RPC Response: ${response}`);
}
Expand All @@ -77,17 +86,17 @@ export class JsonRpcClient {
}

export type ValidResponse = {
jsonrpc: '2.0',
id: string,
result: any,
jsonrpc: '2.0';
id: string;
result: any;
};

export type ErrorResponse = {
jsonrpc: '2.0',
id: string,
jsonrpc: '2.0';
id: string;
error: {
code: any,
message: string,
data?: any,
}
}
code: any;
message: string;
data?: any;
};
};
36 changes: 17 additions & 19 deletions sdk/typescript/src/types/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0


import { RawObjectRef } from './objects';

export type Transfer = {
Expand All @@ -25,7 +24,6 @@ export type TransactionData = {
export type Transaction = {
data: TransactionData;
tx_signature: string;
auth_signature: string;
};

export type CertifiedTransaction = {
Expand All @@ -39,29 +37,29 @@ export type GatewayTxSeqNumber = number;
export type GetTxnDigestsResponse = [GatewayTxSeqNumber, TransactionDigest][];

export type MoveModulePublish = {
modules: any,
modules: any;
};

export type MoveTypeTag =
'bool' |
'u8' |
'u64' |
'u128' |
'address' |
'signer' |
'vector' |
'struct';
| 'bool'
| 'u8'
| 'u64'
| 'u128'
| 'address'
| 'signer'
| 'vector'
| 'struct';

export type MoveCall = {
packages: RawObjectRef,
module: string,
function: string,
type_arguments: MoveTypeTag[],
object_arguments: RawObjectRef[],
shared_object_arguments: string[],
pure_arguments: any[],
packages: RawObjectRef;
module: string;
function: string;
type_arguments: MoveTypeTag[];
object_arguments: RawObjectRef[];
shared_object_arguments: string[];
pure_arguments: any[];
};

export type EmptySignInfo = object;
export type AuthorityName = string;
export type AuthoritySignature = string;
export type AuthoritySignature = string;
Loading

0 comments on commit 1046a3b

Please sign in to comment.