Skip to content

Commit

Permalink
[Typescript SDK] construct MoveCall without gateway (MystenLabs#3735)
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz authored Aug 16, 2022
1 parent 4f3ec83 commit b0ec0ae
Show file tree
Hide file tree
Showing 15 changed files with 828 additions and 71 deletions.
6 changes: 5 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
# These files are used in "porcelain" file comparison,
# we don't want an equality to fail because of line endings.
crates/sui-core/tests/staged/sui.yaml text eol=lf
crates/sui-open-rpc/spec/openrpc.json text eol=lf
crates/sui-open-rpc/spec/openrpc.json text eol=lf
sui_core/tests/staged/sui.yaml text eol=lf

# These files is auto generated
sdk/typescript/src/index.guard.ts linguist-generated=true
1 change: 1 addition & 0 deletions crates/sui-types/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ impl TransactionData {
TransactionData {
kind,
sender,
// TODO: Update local-txn-data-serializer.ts if `gas_price` is changed
gas_price: 1,
gas_payment,
gas_budget,
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"typescript": "^4.6.3"
},
"dependencies": {
"@mysten/bcs": "^0.2.0",
"bn.js": "^5.2.0",
"buffer": "^6.0.3",
"cross-fetch": "^3.1.5",
Expand Down
199 changes: 188 additions & 11 deletions sdk/typescript/src/index.guard.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions sdk/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export * from './cryptography/publickey';

export * from './providers/provider';
export * from './providers/json-rpc-provider';
export * from './providers/json-rpc-provider-with-cache';

export * from './serialization/base64';
export * from './serialization/hex';

export * from './signers/txn-data-serializers/rpc-txn-data-serializer';
export * from './signers/txn-data-serializers/txn-data-serializer';
export * from './signers/txn-data-serializers/local-txn-data-serializer';

export * from './signers/signer';
export * from './signers/raw-signer';
Expand Down
104 changes: 104 additions & 0 deletions sdk/typescript/src/providers/json-rpc-provider-with-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { SignatureScheme } from '../cryptography/publickey';
import { isSuiObjectRef } from '../index.guard';
import {
GetObjectDataResponse,
SuiObjectInfo,
SuiTransactionResponse,
SuiObjectRef,
getObjectReference,
TransactionEffects,
normalizeSuiObjectId,
} from '../types';
import { JsonRpcProvider } from './json-rpc-provider';

export class JsonRpcProviderWithCache extends JsonRpcProvider {
/**
* A list of object references which are being tracked.
*
* Whenever an object is fetched or updated within the transaction,
* its record gets updated.
*/
private objectRefs: Map<string, SuiObjectRef> = new Map();

// Objects
async getObjectsOwnedByAddress(address: string): Promise<SuiObjectInfo[]> {
const resp = await super.getObjectsOwnedByAddress(address);
resp.forEach(r => this.updateObjectRefCache(r));
return resp;
}

async getObjectsOwnedByObject(objectId: string): Promise<SuiObjectInfo[]> {
const resp = await super.getObjectsOwnedByObject(objectId);
resp.forEach(r => this.updateObjectRefCache(r));
return resp;
}

async getObject(objectId: string): Promise<GetObjectDataResponse> {
const resp = await super.getObject(objectId);
this.updateObjectRefCache(resp);
return resp;
}

async getObjectRef(
objectId: string,
skipCache = false
): Promise<SuiObjectRef | undefined> {
const normalizedId = normalizeSuiObjectId(objectId);
if (!skipCache && this.objectRefs.has(normalizedId)) {
return this.objectRefs.get(normalizedId);
}

const ref = await super.getObjectRef(objectId);
this.updateObjectRefCache(ref);
return ref;
}

async getObjectBatch(objectIds: string[]): Promise<GetObjectDataResponse[]> {
const resp = await super.getObjectBatch(objectIds);
resp.forEach(r => this.updateObjectRefCache(r));
return resp;
}

// Transactions
async executeTransaction(
txnBytes: string,
signatureScheme: SignatureScheme,
signature: string,
pubkey: string
): Promise<SuiTransactionResponse> {
const resp = await super.executeTransaction(
txnBytes,
signatureScheme,
signature,
pubkey
);

this.updateObjectRefCacheFromTransactionEffects(resp.effects);
return resp;
}

private updateObjectRefCache(
newData: GetObjectDataResponse | SuiObjectRef | undefined
) {
if (newData == null) {
return;
}
const ref = isSuiObjectRef(newData) ? newData : getObjectReference(newData);
if (ref != null) {
this.objectRefs.set(ref.objectId, ref);
}
}

private updateObjectRefCacheFromTransactionEffects(
effects: TransactionEffects
) {
effects.created?.forEach(r => this.updateObjectRefCache(r.reference));
effects.mutated?.forEach(r => this.updateObjectRefCache(r.reference));
effects.unwrapped?.forEach(r => this.updateObjectRefCache(r.reference));
effects.wrapped?.forEach(r => this.updateObjectRefCache(r));
effects.deleted?.forEach(r => this.objectRefs.delete(r.objectId));
}
}
2 changes: 1 addition & 1 deletion sdk/typescript/src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const isNumber = (val: any): val is number => typeof val === 'number';
const isAny = (_val: any): _val is any => true;

export class JsonRpcProvider extends Provider {
private client: JsonRpcClient;
protected client: JsonRpcClient;

/**
* Establish a connection to a Sui Gateway endpoint
Expand Down
Loading

0 comments on commit b0ec0ae

Please sign in to comment.