Skip to content

Commit

Permalink
[Typescript SDK] Add getObjectRef and getGasObjectsOwnedByAddress (My…
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz authored Aug 3, 2022
1 parent 7af627e commit c9c2be9
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 10 deletions.
7 changes: 4 additions & 3 deletions sdk/typescript/src/index.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ export function isTransferSuiTransaction(obj: any, _argumentName?: string): obj
typeof obj === "function") &&
isTransactionDigest(obj.suiObjectId) as boolean &&
isSequenceNumber(obj.gasBudget) as boolean &&
isTransactionDigest(obj.recipient) as boolean &&
(typeof obj.amount === "undefined" ||
isSequenceNumber(obj.amount))
isTransactionDigest(obj.recipient) as boolean &&
(typeof obj.amount === "undefined" ||
isSequenceNumber(obj.amount) as boolean)
)
}

Expand Down Expand Up @@ -151,6 +151,7 @@ export function isTxnDataSerializer(obj: any, _argumentName?: string): obj is Tx
typeof obj === "object" ||
typeof obj === "function") &&
typeof obj.newTransferObject === "function" &&
typeof obj.newTransferSui === "function" &&
typeof obj.newMoveCall === "function" &&
typeof obj.newMergeCoin === "function" &&
typeof obj.newSplitCoin === "function" &&
Expand Down
13 changes: 13 additions & 0 deletions sdk/typescript/src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import {
TransactionDigest,
TransactionEffectsResponse,
TransactionResponse,
SuiObjectRef,
getObjectReference,
Coin,
} from '../types';

const isNumber = (val: any): val is number => typeof val === 'number';
Expand Down Expand Up @@ -51,6 +54,11 @@ export class JsonRpcProvider extends Provider {
}
}

async getGasObjectsOwnedByAddress(address: string): Promise<SuiObjectInfo[]> {
const objects = await this.getObjectsOwnedByAddress(address);
return objects.filter((obj: SuiObjectInfo) => Coin.isSUI(obj));
}

async getObjectsOwnedByObject(objectId: string): Promise<SuiObjectInfo[]> {
try {
return await this.client.requestWithType(
Expand All @@ -77,6 +85,11 @@ export class JsonRpcProvider extends Provider {
}
}

async getObjectRef(objectId: string): Promise<SuiObjectRef | undefined> {
const resp = await this.getObject(objectId);
return getObjectReference(resp);
}

async getObjectBatch(objectIds: string[]): Promise<GetObjectDataResponse[]> {
const requests = objectIds.map(id => ({
method: 'sui_getObject',
Expand Down
14 changes: 14 additions & 0 deletions sdk/typescript/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GatewayTxSeqNumber,
GetTxnDigestsResponse,
TransactionResponse,
SuiObjectRef,
} from '../types';

///////////////////////////////
Expand All @@ -20,11 +21,24 @@ export abstract class Provider {
addressOrObjectId: string
): Promise<SuiObjectInfo[]>;

/**
* Convenience method for getting all gas objects(SUI Tokens) owned by an address
*/
abstract getGasObjectsOwnedByAddress(
_address: string
): Promise<SuiObjectInfo[]>;

/**
* Get details about an object
*/
abstract getObject(objectId: string): Promise<GetObjectDataResponse>;

/**
* Get object reference(id, tx digest, version id)
* @param objectId
*/
abstract getObjectRef(objectId: string): Promise<SuiObjectRef | undefined>;

// Transactions
/**
* Get transaction digests for a given range
Expand Down
13 changes: 12 additions & 1 deletion sdk/typescript/src/providers/void-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,30 @@ import {
SuiObjectInfo,
GetObjectDataResponse,
TransactionResponse,
SuiObjectRef,
} from '../types';
import { Provider } from './provider';

export class VoidProvider extends Provider {
// Objects
async getObjectsOwnedByAddress(_address: string): Promise<SuiObjectInfo[]> {
throw this.newError('getOwnedObjects');
throw this.newError('getObjectsOwnedByAddress');
}

async getGasObjectsOwnedByAddress(
_address: string
): Promise<SuiObjectInfo[]> {
throw this.newError('getGasObjectsOwnedByAddress');
}

async getObject(_objectId: string): Promise<GetObjectDataResponse> {
throw this.newError('getObject');
}

async getObjectRef(_objectId: string): Promise<SuiObjectRef | undefined> {
throw this.newError('getObjectRef');
}

// Transactions
async getTransaction(
_digest: TransactionDigest
Expand Down
41 changes: 37 additions & 4 deletions sdk/typescript/src/types/framework.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { getObjectFields, GetObjectDataResponse } from './objects';
import {
getObjectFields,
GetObjectDataResponse,
SuiMoveObject,
SuiObjectInfo,
} from './objects';

import { getMoveObjectType } from './objects';

import BN from 'bn.js';

const COIN_TYPE = '0x2::coin::Coin';
const COIN_TYPE_ARG_REGEX = /^0x2::coin::Coin<(.+)>$/;

type ObjectData = GetObjectDataResponse | SuiMoveObject | SuiObjectInfo;

/**
* Utility class for 0x2::coin
* as defined in https://github.com/MystenLabs/sui/blob/ca9046fd8b1a9e8634a4b74b0e7dabdc7ea54475/sui_programmability/framework/sources/Coin.move#L4
*/
export class Coin {
static isCoin(data: GetObjectDataResponse): boolean {
return getMoveObjectType(data)?.startsWith('0x2::coin::Coin') ?? false;
static isCoin(data: ObjectData): boolean {
return Coin.getType(data)?.startsWith(COIN_TYPE) ?? false;
}

static getCoinTypeArg(obj: ObjectData) {
const res = Coin.getType(obj)?.match(COIN_TYPE_ARG_REGEX);
return res ? res[1] : null;
}

static isSUI(obj: ObjectData) {
const arg = Coin.getCoinTypeArg(obj);
return arg ? Coin.getCoinSymbol(arg) === 'SUI' : false;
}

static getBalance(data: GetObjectDataResponse): BN | undefined {
static getCoinSymbol(coinTypeArg: string) {
return coinTypeArg.substring(coinTypeArg.lastIndexOf(':') + 1);
}

static getBalance(
data: GetObjectDataResponse | SuiMoveObject
): BN | undefined {
if (!Coin.isCoin(data)) {
return undefined;
}
Expand All @@ -27,4 +53,11 @@ export class Coin {
static getZero(): BN {
return new BN.BN('0', 10);
}

private static getType(data: ObjectData): string | undefined {
if ('status' in data) {
return getMoveObjectType(data);
}
return data.type;
}
}
5 changes: 4 additions & 1 deletion sdk/typescript/src/types/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ export function getMoveObjectType(
}

export function getObjectFields(
resp: GetObjectDataResponse
resp: GetObjectDataResponse | SuiMoveObject
): ObjectContentFields | undefined {
if ('fields' in resp) {
return resp.fields;
}
return getMoveObject(resp)?.fields;
}

Expand Down
3 changes: 2 additions & 1 deletion sdk/typescript/test/types/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { isTransactionResponse } from '../../src/index.guard';
describe('Test Transaction Definition', () => {
it('Test against different transaction definitions', () => {
const txns = mockTransactionData;

expect(isTransactionResponse(txns['move_call'])).toBeTruthy();
expect(isTransactionResponse(txns['transfer'])).toBeTruthy();
expect(isTransactionResponse(txns['coin_split'])).toBeTruthy();
expect(isTransactionResponse(txns['transfer_sui'])).toBeTruthy();
// TODO: add mock data for failed transaction
// expect(
// isTransactionEffectsResponse(txns['fail'])
Expand Down

0 comments on commit c9c2be9

Please sign in to comment.