Skip to content

Commit

Permalink
update Typescript SDK for new Rest APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse authored and aptos-bot committed Apr 20, 2022
1 parent 9de7e00 commit eb1497d
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 52 deletions.
3 changes: 2 additions & 1 deletion ecosystem/typescript/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"axios": "^0.26.1",
"buffer": "^6.0.3",
"js-sha3": "^0.8.0",
"tweetnacl": "^1.0.3"
"tweetnacl": "^1.0.3",
"yarn": "^1.22.18"
},
"version": "0.0.15"
}
67 changes: 45 additions & 22 deletions ecosystem/typescript/sdk/src/api/Accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ export class Accounts<SecurityDataType = unknown> {
/**
* No description
*
* @tags accounts
* @tags accounts, state
* @name GetAccount
* @summary Get account
* @request GET:/accounts/{address}
* @response `200` `Account` Returns the latest account core data resource.
* @response `400` `(AptosError)`
* @response `404` `(AptosError)`
* @response `500` `(AptosError)`
*/
getAccount = (address: Address, params: RequestParams = {}) =>
this.http.request<Account, AptosError>({
Expand All @@ -51,14 +47,10 @@ export class Accounts<SecurityDataType = unknown> {
/**
* No description
*
* @tags accounts
* @tags accounts, state
* @name GetAccountResources
* @summary Get account resources
* @request GET:/accounts/{address}/resources
* @response `200` `(AccountResource)[]` This API returns account resources for a specific ledger version (AKA transaction version). If not present, the latest version is used. The Aptos nodes prune account state history, via a configurable time window (link). If the requested data has been pruned, the server responds with a 404
* @response `400` `(AptosError)`
* @response `404` `(AptosError)`
* @response `500` `(AptosError)`
*/
getAccountResources = (address: Address, query?: { version?: LedgerVersion }, params: RequestParams = {}) =>
this.http.request<AccountResource[], AptosError>({
Expand All @@ -68,17 +60,34 @@ export class Accounts<SecurityDataType = unknown> {
format: "json",
...params,
});
/**
* @description This API renders a resource identified by the owner account `address` and the `resource_type`, at a ledger version (AKA transaction version) specified as a query param, otherwise the latest version is used.
*
* @tags accounts, state
* @name GetAccountResource
* @summary Get resource by account address and resource type.
* @request GET:/accounts/{address}/resource/{resource_type}
*/
getAccountResource = (
address: Address,
resourceType: MoveStructTagId,
query?: { version?: LedgerVersion },
params: RequestParams = {},
) =>
this.http.request<AccountResource, AptosError>({
path: `/accounts/${address}/resource/${resourceType}`,
method: "GET",
query: query,
format: "json",
...params,
});
/**
* No description
*
* @tags accounts
* @tags accounts, state
* @name GetAccountModules
* @summary Get account modules
* @request GET:/accounts/{address}/modules
* @response `200` `(MoveModule)[]` This API returns account modules for a specific ledger version (AKA transaction version). If not present, the latest version is used. The Aptos nodes prune account state history, via a configurable time window (link). If the requested data has been pruned, the server responds with a 404
* @response `400` `(AptosError)`
* @response `404` `(AptosError)`
* @response `500` `(AptosError)`
*/
getAccountModules = (address: Address, query?: { version?: LedgerVersion }, params: RequestParams = {}) =>
this.http.request<MoveModule[], AptosError>({
Expand All @@ -88,16 +97,34 @@ export class Accounts<SecurityDataType = unknown> {
format: "json",
...params,
});
/**
* @description This API renders a Move module identified by the module id. A module id consists of the module owner `address` and the `module_name`. The module is rendered at a ledger version (AKA transaction version) specified as a query param, otherwise the latest version is used.
*
* @tags accounts, state
* @name GetAccountModule
* @summary Get module by module id.
* @request GET:/accounts/{address}/module/{module_name}
*/
getAccountModule = (
address: Address,
moduleName: string,
query?: { version?: LedgerVersion },
params: RequestParams = {},
) =>
this.http.request<MoveModule, AptosError>({
path: `/accounts/${address}/module/${moduleName}`,
method: "GET",
query: query,
format: "json",
...params,
});
/**
* No description
*
* @tags transactions
* @name GetAccountTransactions
* @summary Get account transactions
* @request GET:/accounts/{address}/transactions
* @response `200` `(OnChainTransaction)[]` Returns on-chain transactions, paginated.
* @response `400` `(AptosError)`
* @response `500` `(AptosError)`
*/
getAccountTransactions = (address: Address, query?: { start?: number; limit?: number }, params: RequestParams = {}) =>
this.http.request<OnChainTransaction[], AptosError>({
Expand All @@ -114,10 +141,6 @@ export class Accounts<SecurityDataType = unknown> {
* @name GetEventsByEventHandle
* @summary Get events by event handle
* @request GET:/accounts/{address}/events/{event_handle_struct}/{field_name}
* @response `200` `(Event)[]` Returns events
* @response `400` `(AptosError)`
* @response `404` `(AptosError)`
* @response `500` `(AptosError)`
*/
getEventsByEventHandle = (
address: Address,
Expand Down
4 changes: 0 additions & 4 deletions ecosystem/typescript/sdk/src/api/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export class Events<SecurityDataType = unknown> {
* @name GetEventsByEventKey
* @summary Get events by event key
* @request GET:/events/{event_key}
* @response `200` `(Event)[]` Returns events
* @response `400` `(AptosError)`
* @response `404` `(AptosError)`
* @response `500` `(AptosError)`
*/
getEventsByEventKey = (eventKey: HexEncodedBytes, params: RequestParams = {}) =>
this.http.request<Event[], AptosError>({
Expand Down
19 changes: 0 additions & 19 deletions ecosystem/typescript/sdk/src/api/Transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ export class Transactions<SecurityDataType = unknown> {
* @name GetTransactions
* @summary Get transactions
* @request GET:/transactions
* @response `200` `(OnChainTransaction)[]` Returns on-chain transactions, paginated.
* @response `400` `(AptosError)`
* @response `404` `(AptosError)`
* @response `500` `(AptosError)`
*/
getTransactions = (query?: { start?: number; limit?: number }, params: RequestParams = {}) =>
this.http.request<OnChainTransaction[], AptosError>({
Expand All @@ -54,11 +50,6 @@ export class Transactions<SecurityDataType = unknown> {
* @name SubmitTransaction
* @summary Submit transaction
* @request POST:/transactions
* @response `202` `PendingTransaction` Transaction is accepted and submitted to mempool.
* @response `400` `(AptosError)`
* @response `413` `(AptosError)`
* @response `415` `(AptosError)`
* @response `500` `(AptosError)`
*/
submitTransaction = (data: SubmitTransactionRequest, params: RequestParams = {}) =>
this.http.request<PendingTransaction, AptosError>({
Expand All @@ -76,10 +67,6 @@ export class Transactions<SecurityDataType = unknown> {
* @name GetTransaction
* @summary Get transaction
* @request GET:/transactions/{txn_hash_or_version}
* @response `200` `Transaction` Returns a pending / on-chain transaction.
* @response `400` `(AptosError)`
* @response `404` `(AptosError)`
* @response `500` `(AptosError)`
*/
getTransaction = (txnHashOrVersion: string, params: RequestParams = {}) =>
this.http.request<Transaction, AptosError>({
Expand All @@ -95,12 +82,6 @@ export class Transactions<SecurityDataType = unknown> {
* @name CreateSigningMessage
* @summary Create transaction signing message
* @request POST:/transactions/signing_message
* @response `200` `{ message: HexEncodedBytes }` Returns hex-encoded transaction signing message bytes.
* @response `400` `(AptosError)`
* @response `404` `(AptosError)`
* @response `413` `(AptosError)`
* @response `415` `(AptosError)`
* @response `500` `(AptosError)`
*/
createSigningMessage = (data: UserTransactionRequest, params: RequestParams = {}) =>
this.http.request<{ message: HexEncodedBytes }, AptosError>({
Expand Down
10 changes: 5 additions & 5 deletions ecosystem/typescript/sdk/src/api/data-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ Struct type value examples:
Note:
1. Empty chars should be ignored when comparing 2 struct tag ids.
2. When used in an URL path, should be encoded by url-encoding (AKA percent-encoding).
* @pattern ^(bool|u8|u64|u128|address|signer|vector<.+>|0x[0-9a-zA-Z:_<>]+)$
* @pattern ^(bool|u8|u64|u128|address|signer|vector<.+>|0x[0-9a-zA-Z:_<, >]+)$
* @example 0x1::XUS::XUS
*/
export type MoveTypeTagId = string;
Expand Down Expand Up @@ -254,7 +254,7 @@ the `CoinType` in the Move source code.
Note:
1. Empty chars should be ignored when comparing 2 struct tag ids.
2. When used in an URL path, should be encoded by url-encoding (AKA percent-encoding).
* @pattern ^(bool|u8|u64|u128|address|signer|vector<.+>|0x[0-9a-zA-Z:_<>]+|^&(mut )?.+$|T\d+)$
* @pattern ^(bool|u8|u64|u128|address|signer|vector<.+>|0x[0-9a-zA-Z:_<, >]+|^&(mut )?.+$|T\d+)$
* @example 0x1::AptosAccount::Balance<0x1::XUS::XUS>
*/
export type MoveTypeId = string;
Expand Down Expand Up @@ -423,7 +423,7 @@ Format: "{address}::{module name}"
`address` should be hex-encoded 16 bytes account address
that is prefixed with `0x` and leading zeros are trimmed.
Module name is case sensitive.
Module name is case-sensitive.
See [doc](https://diem.github.io/move/modules-and-scripts.html#modules) for more details.
* @example 0x1::Aptos
Expand Down Expand Up @@ -571,7 +571,7 @@ export interface ScriptFunctionPayload {
Format: `{address}::{module name}::{function name}`
Both `module name` and `function name` are case sensitive.
Both `module name` and `function name` are case-sensitive.
* @example 0x1::PaymentScripts::peer_to_peer_with_metadata
*/
export type ScriptFunctionId = string;
Expand Down Expand Up @@ -639,7 +639,7 @@ export interface DeleteModule {
* Format: "{address}::{module name}"
* `address` should be hex-encoded 16 bytes account address
* that is prefixed with `0x` and leading zeros are trimmed.
* Module name is case sensitive.
* Module name is case-sensitive.
* See [doc](https://diem.github.io/move/modules-and-scripts.html#modules) for more details.
*/
module: MoveModuleId;
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/typescript/sdk/src/api/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class HttpClient<SecurityDataType = unknown> {
const requestParams = this.mergeRequestParams(params, secureParams);
const responseFormat = (format && this.format) || void 0;

if (type === ContentType.FormData && body && typeof body === "object") {
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
// @ts-ignore
requestParams.headers.common = { Accept: "*/*" };
// @ts-ignore
Expand Down
12 changes: 12 additions & 0 deletions ecosystem/typescript/sdk/src/aptos_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@ test("gets genesis resources", async () => {
expect((accountResource.data as AnyObject)["self_address"]).toBe("0x1");
});

test("gets the Account resource", async () => {
const client = new AptosClient(NODE_URL);
const accountResource = await client.getAccountResource("0x1", "0x1::Account::Account");
expect((accountResource.data as AnyObject)["self_address"]).toBe("0x1");
});

test("gets account modules", async () => {
const client = new AptosClient(NODE_URL);
const modules = await client.getAccountModules("0x1");
const module = modules.find((r) => r.abi.name === "TestCoin");
expect(module.abi.address).toBe("0x1");
});

test("gets the TestCoin module", async () => {
const client = new AptosClient(NODE_URL);
const module = await client.getAccountModule("0x1", "TestCoin");
expect(module.abi.address).toBe("0x1");
});

test("test raiseForStatus", async () => {
const testData = { hello: "wow" };
const fakeResponse: AxiosResponse = {
Expand Down
26 changes: 26 additions & 0 deletions ecosystem/typescript/sdk/src/aptos_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ export class AptosClient {
return response.data;
}

/** Returns the module identified by address and module name */
async getAccountModule(
accountAddress: MaybeHexString,
moduleName: string,
query?: { version?: Types.LedgerVersion },
): Promise<Types.MoveModule> {
const response = await this.accounts.getAccountModule(HexString.ensure(accountAddress).hex(), moduleName, query);
raiseForStatus(200, response);
return response.data;
}

/** Returns all resources associated with the account */
async getAccountResources(
accountAddress: MaybeHexString,
Expand All @@ -104,6 +115,21 @@ export class AptosClient {
return response.data;
}

/** Returns the resource by the address and resource type */
async getAccountResource(
accountAddress: MaybeHexString,
resourceType: string,
query?: { version?: Types.LedgerVersion },
): Promise<Types.AccountResource> {
const response = await this.accounts.getAccountResource(
HexString.ensure(accountAddress).hex(),
resourceType,
query,
);
raiseForStatus(200, response);
return response.data;
}

/** Generates a transaction request that can be submitted to produce a raw transaction that
* can be signed, which upon being signed can be submitted to the blockchain. */
async generateTransaction(
Expand Down

0 comments on commit eb1497d

Please sign in to comment.