Skip to content

Commit

Permalink
[refactoring] Remove unused genesis check from StateView (#9589)
Browse files Browse the repository at this point in the history
`is_genesis()` was defined in `StateView` but never used apart
from a single test with `MockVM`. But the test set it to false
always...

Removing it to have better view interfaces.
  • Loading branch information
georgemitenkov authored and 0xmaayan committed Aug 21, 2023
1 parent 2788603 commit a8c35e0
Show file tree
Hide file tree
Showing 115 changed files with 2,445 additions and 102 deletions.
140 changes: 140 additions & 0 deletions ecosystem/typescript/sdk_v2/src/api/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { AptosConfig } from "./aptos_config";
import {
AccountData,
LedgerVersion,
MoveModuleBytecode,
MoveResource,
MoveResourceType,
PaginationArgs,
Transaction,
HexInput,
} from "../types";
import { getData, getModule, getModules, getResource, getResources, getTransactions } from "../internal/account";

export class Account {
readonly config: AptosConfig;

constructor(config: AptosConfig) {
this.config = config;
}

/**
* Queries for an Aptos account given an account address
*
* @param accountAddress Aptos account address
*
* @returns The account data
*
* @example An example of the returned account
* ```
* {
* sequence_number: "1",
* authentication_key: "0x5307b5f4bc67829097a8ba9b43dba3b88261eeccd1f709d9bde240fc100fbb69"
* }
* ```
*/
async getData(args: { accountAddress: HexInput }): Promise<AccountData> {
const data = await getData({ aptosConfig: this.config, ...args });
return data;
}

/**
* Queries for an acount modules given an account address
*
* Note: In order to get all account modules, this function may call the API
* multiple times as it auto paginates.
*
* @param accountAddress Aptos account address
* @returns Account modules
*/

async getModules(args: {
accountAddress: HexInput;
options?: PaginationArgs & LedgerVersion;
}): Promise<MoveModuleBytecode[]> {
const modules = await getModules({ aptosConfig: this.config, ...args });
return modules;
}

/**
* Queries for an account module given account address and module name
*
* @param accountAddress Aptos account address
* @param moduleName The name of the module
*
* @returns Account module
*
* @example An example of an account module
* ```
* {
* bytecode: "0xa11ceb0b0600000006010002030206050807070f0d081c200",
* abi: { address: "0x1" }
* }
* ```
*/
async getModule(args: {
accountAddress: HexInput;
moduleName: string;
options?: LedgerVersion;
}): Promise<MoveModuleBytecode> {
const module = await getModule({ aptosConfig: this.config, ...args });
return module;
}

/**
* Queries account transactions given an account address
*
* Note: In order to get all account transactions, this function may call the API
* multiple times as it auto paginates.
*
* @param accountAddress Aptos account address
*
* @returns The account transactions
*/
async getTransactions(args: { accountAddress: HexInput; options?: PaginationArgs }): Promise<Transaction[]> {
const transactions = await getTransactions({ aptosConfig: this.config, ...args });
return transactions;
}

/**
* Queries account resources given an account address
*
* Note: In order to get all account resources, this function may call the API
* multiple times as it auto paginates.
*
* @param accountAddress Aptos account address
* @returns Account resources
*/
async getResources(args: {
accountAddress: HexInput;
options?: PaginationArgs & LedgerVersion;
}): Promise<MoveResource[]> {
const resources = await getResources({ aptosConfig: this.config, ...args });
return resources;
}

/**
* Queries account resource given account address and resource type
*
* @param accountAddress Aptos account address
* @param resourceType String representation of an on-chain Move struct type, i.e "0x1::aptos_coin::AptosCoin"
*
* @returns Account resource
*
* @example An example of an account resource
* ```
* {
* type: "0x1::aptos_coin::AptosCoin",
* data: { value: 6 }
* }
* ```
*/
async getResource(args: {
accountAddress: HexInput;
resourceType: MoveResourceType;
options?: LedgerVersion;
}): Promise<MoveResource> {
const resource = await getResource({ aptosConfig: this.config, ...args });
return resource;
}
}
4 changes: 4 additions & 0 deletions ecosystem/typescript/sdk_v2/src/api/aptos.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Account } from "./account";
import { AptosConfig } from "./aptos_config";

export class Aptos {
readonly config: AptosConfig;

readonly account: Account;

/**
* This class is the main entry point into Aptos's
* APIs and separates functionality into different namespaces.
Expand All @@ -21,5 +24,6 @@ export class Aptos {
*/
constructor(settings?: AptosConfig) {
this.config = new AptosConfig(settings);
this.account = new Account(this.config);
}
}
40 changes: 31 additions & 9 deletions ecosystem/typescript/sdk_v2/src/api/aptos_config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ClientConfig } from "../client/types";
import { AptosSettings, ClientConfig } from "../types";
import { NetworkToNodeAPI, NetworkToFaucetAPI, NetworkToIndexerAPI, Network } from "../utils/api-endpoints";
import { DEFAULT_NETWORK } from "../utils/const";
import { AptosApiType, DEFAULT_NETWORK } from "../utils/const";

export class AptosConfig {
readonly network?: Network;
readonly network: Network;

readonly fullnode?: string;

Expand All @@ -13,11 +13,33 @@ export class AptosConfig {

readonly clientConfig?: ClientConfig;

constructor(config?: AptosConfig) {
this.network = config?.network ?? DEFAULT_NETWORK;
this.fullnode = config?.fullnode ?? NetworkToNodeAPI[this.network];
this.faucet = config?.faucet ?? NetworkToFaucetAPI[this.network];
this.indexer = config?.indexer ?? NetworkToIndexerAPI[this.network];
this.clientConfig = config?.clientConfig ?? {};
constructor(settings?: AptosSettings) {
this.network = settings?.network ?? DEFAULT_NETWORK;
this.fullnode = settings?.fullnode;
this.faucet = settings?.faucet;
this.indexer = settings?.indexer;
this.clientConfig = settings?.clientConfig ?? {};
}

getRequestUrl(apiType: AptosApiType): string {
switch (apiType) {
case AptosApiType.FULLNODE:
if (this.fullnode !== undefined) return this.fullnode;
if (this.network === Network.CUSTOM && this.fullnode === undefined)
throw new Error("Please provide a custom full node url");
return NetworkToNodeAPI[this.network];
case AptosApiType.FAUCET:
if (this.faucet !== undefined) return this.faucet;
if (this.network === Network.CUSTOM && this.faucet === undefined)
throw new Error("Please provide a custom faucet url");
return NetworkToFaucetAPI[this.network];
case AptosApiType.INDEXER:
if (this.indexer !== undefined) return this.indexer;
if (this.network === Network.CUSTOM && this.indexer === undefined)
throw new Error("Please provide a custom indexer url");
return NetworkToIndexerAPI[this.network];
default:
throw Error(`apiType ${apiType} is not supported`);
}
}
}
4 changes: 3 additions & 1 deletion ecosystem/typescript/sdk_v2/src/client/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import aptosClient from "@aptos-labs/aptos-client";
import { AptosApiError, AptosRequest, AptosResponse, ClientConfig } from "./types";
import { AptosApiError, AptosResponse } from "./types";
import { VERSION } from "../version";
import { ClientConfig, AptosRequest } from "../types";

/**
* Meaningful errors map
Expand Down Expand Up @@ -69,6 +70,7 @@ export async function aptosRequest<Req, Res>(options: AptosRequest): Promise<Apt
if (result.status >= 200 && result.status < 300) {
return result;
}

const errorMessage = errors[result.status];
throw new AptosApiError(options, result, errorMessage ?? "Generic Error");
}
3 changes: 2 additions & 1 deletion ecosystem/typescript/sdk_v2/src/client/get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AptosRequest, AptosResponse } from "./types";
import { AptosResponse } from "./types";
import { aptosRequest } from "./core";
import { AptosRequest } from "../types";

export type GetRequestOptions = Omit<AptosRequest, "body" | "method">;

Expand Down
3 changes: 2 additions & 1 deletion ecosystem/typescript/sdk_v2/src/client/post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AptosRequest } from "../types";
import { aptosRequest } from "./core";
import { AptosRequest, AptosResponse } from "./types";
import { AptosResponse } from "./types";

export type PostRequestOptions = Omit<AptosRequest, "method">;

Expand Down
44 changes: 1 addition & 43 deletions ecosystem/typescript/sdk_v2/src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,4 @@
import { AnyNumber } from "../types";

/**
* A configuration object we can pass with the request to the server.
*
* @param TOKEN - an auth token to send with the request
* @param HEADERS - extra headers we want to send with the request
* @param WITH_CREDENTIALS - whether to carry cookies. By default, it is set to true and cookies will be sent
*/
export type ClientConfig = {
TOKEN?: string;
HEADERS?: Record<string, string | number | boolean>;
WITH_CREDENTIALS?: boolean;
};

/**
* The API request type
*
* @param url - the url to make the request to, i.e https://fullnode.aptoslabs.devnet.com/v1
* @param method - the request method "GET" | "POST"
* @param endpoint (optional) - the endpoint to make the request to, i.e transactions
* @param body (optional) - the body of the request
* @param contentType (optional) - the content type to set the `content-type` header to,
* by default is set to `application/json`
* @param params (optional) - query params to add to the request
* @param originMethod (optional) - the local method the request came from
* @param overrides (optional) - a `ClientConfig` object type to override request data
*/
export type AptosRequest = {
url: string;
method: "GET" | "POST";
endpoint?: string;
body?: any;
contentType?: string;
params?: Record<string, string | AnyNumber | boolean | undefined>;
originMethod?: string;
overrides?: ClientConfig;
};
import { AptosRequest } from "../types";

/**
* The API response type
Expand Down Expand Up @@ -90,8 +53,3 @@ export class AptosApiError extends Error {
this.request = request;
}
}

export interface PaginationArgs {
start?: AnyNumber;
limit?: number;
}
16 changes: 7 additions & 9 deletions ecosystem/typescript/sdk_v2/src/core/account_address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,18 @@ export class AccountAddress {

// Check if the address is in LONG form. If it is not, this is only allowed for
// special addresses, in which case we check it is in proper SHORT form.
if (args.input.length != AccountAddress.LONG_STRING_LENGTH + 2) {
if (args.input.length !== AccountAddress.LONG_STRING_LENGTH + 2) {
if (!address.isSpecial()) {
throw new ParsingError(
"The given hex string is not a special address, it must be represented as 0x + 64 chars.",
`The given hex string ${address} is not a special address, it must be represented as 0x + 64 chars.`,
AddressInvalidReason.LONG_FORM_REQUIRED_UNLESS_SPECIAL,
);
} else {
} else if (args.input.length !== 3) {
// 0x + one hex char is the only valid SHORT form for special addresses.
if (args.input.length != 3) {
throw new ParsingError(
"The given hex string is a special address not in LONG form, it must be 0x0 to 0xf without padding zeroes.",
AddressInvalidReason.INVALID_PADDING_ZEROES,
);
}
throw new ParsingError(
"The given hex string is a special address not in LONG form, it must be 0x0 to 0xf without padding zeroes.",
AddressInvalidReason.INVALID_PADDING_ZEROES,
);
}
}

Expand Down
Loading

0 comments on commit a8c35e0

Please sign in to comment.