forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Typescript SDK] construct MoveCall without gateway (MystenLabs#3735)
- Loading branch information
Showing
15 changed files
with
828 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
sdk/typescript/src/providers/json-rpc-provider-with-cache.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.