Skip to content

Commit

Permalink
Add updated typescript models to reflect structure of API responses +…
Browse files Browse the repository at this point in the history
… add Raw method to API client allowing non-formatted data to be returned from the API.
  • Loading branch information
iamalwaysuncomfortable committed Jan 15, 2025
1 parent c7e055c commit 7518507
Show file tree
Hide file tree
Showing 19 changed files with 212 additions and 93 deletions.
13 changes: 11 additions & 2 deletions sdk/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import "./polyfill/shared";
import { Account } from "./account";
import { AleoNetworkClient, ProgramImports } from "./network-client";
import { BlockJSON, Header, Metadata } from "./models/blockJSON";
import { DeploymentMetadata } from "./models/deploy";
import { ConfirmedTransactionJSON } from "./models/confirmed_transaction";
import { DeploymentJSON, VerifyingKeys } from "./models/deployment/deploymentJSON";
import { DeploymentObject } from "./models/deployment/deploymentObject";
import { ExecutionJSON } from "./models/executionJSON";
import { FeeJSON} from "./models/feeJSON";
import { FunctionObject } from "./models/functionObject";
import { InputJSON } from "./models/input/inputJSON";
import { InputObject } from "./models/input/inputObject";
import { OutputJSON } from "./models/output/outputJSON";
import { OutputObject } from "./models/output/outputObject";
import { OwnerJSON } from "./models/ownerJSON";
import { PlaintextArray} from "./models/plaintext/array";
import { PlaintextLiteral} from "./models/plaintext/literal";
import { PlaintextObject } from "./models/plaintext/plaintext";
Expand Down Expand Up @@ -99,8 +103,11 @@ export {
BlockJSON,
BlockHeightSearch,
CachedKeyPair,
DeploymentMetadata,
ConfirmedTransactionJSON,
DeploymentJSON,
DeploymentObject,
ExecutionJSON,
FeeJSON,
FunctionObject,
FunctionKeyPair,
FunctionKeyProvider,
Expand All @@ -113,6 +120,7 @@ export {
ProgramImports,
OfflineKeyProvider,
OfflineSearchParams,
OwnerJSON,
PlaintextArray,
PlaintextLiteral,
PlaintextObject,
Expand All @@ -125,4 +133,5 @@ export {
TransactionObject,
TransitionJSON,
TransitionObject,
VerifyingKeys,
};
4 changes: 2 additions & 2 deletions sdk/src/models/blockJSON.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ConfirmedTransaction } from "./confirmed_transaction";
import { ConfirmedTransactionJSON } from "./confirmed_transaction";

export type BlockJSON = {
block_hash: string;
previous_hash: string;
header: Header;
transactions?: (ConfirmedTransaction)[];
transactions?: (ConfirmedTransactionJSON)[];
signature: string;
}
export type Header = {
Expand Down
7 changes: 5 additions & 2 deletions sdk/src/models/confirmed_transaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { TransactionJSON } from "./transaction/transactionJSON";
import { FinalizeJSON } from "./finalizeJSON";

export interface ConfirmedTransaction {
export interface ConfirmedTransactionJSON {
status: string
type: string;
id: string;
index: number;
transaction: TransactionJSON;
finalize: FinalizeJSON[];
}
6 changes: 0 additions & 6 deletions sdk/src/models/deploy.ts

This file was deleted.

7 changes: 7 additions & 0 deletions sdk/src/models/deployment/deploymentJSON.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type VerifyingKeys = [string, [string, string]];

export interface DeploymentJSON {
"edition" : number,
"program" : string,
"verifying_keys" : VerifyingKeys,
}
7 changes: 7 additions & 0 deletions sdk/src/models/deployment/deploymentObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FunctionObject } from "../functionObject";

export interface DeploymentObject {
"edition" : number,
"program" : string,
"functions" : FunctionObject[],
}
5 changes: 3 additions & 2 deletions sdk/src/models/executionJSON.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TransitionJSON } from "./transition/transitionJSON";

export interface ExecutionJSON {
edition: number;
transitions?: (TransitionJSON)[];
transitions: TransitionJSON[];
proof: string[];
global_state_root: string;
}
7 changes: 7 additions & 0 deletions sdk/src/models/feeJSON.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TransitionJSON } from "./transition/transitionJSON";

export interface FeeJSON {
transition: TransitionJSON;
global_state_root: string;
proof: string;
}
6 changes: 6 additions & 0 deletions sdk/src/models/finalizeJSON.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface FinalizeJSON {
"type": string;
"mapping_id": string;
"key_id": string;
"value_id": string;
}
1 change: 1 addition & 0 deletions sdk/src/models/functionObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface FunctionObject {
"constraints" : number,
"variables" : number,
"verifyingKey" : string | VerifyingKey,
"certificate" : string,
}
2 changes: 1 addition & 1 deletion sdk/src/models/output/outputJSON.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface OutputJSON {
type: string;
id: string;
checksum: string;
checksum?: string;
value: string;
}
4 changes: 4 additions & 0 deletions sdk/src/models/ownerJSON.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface OwnerJSON {
address: string;
signature: string;
}
10 changes: 8 additions & 2 deletions sdk/src/models/transaction/transactionJSON.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { DeploymentJSON } from "../deployment/deploymentJSON";
import { ExecutionJSON } from "../executionJSON";
import { FeeJSON } from "../feeJSON";
import { OwnerJSON } from "../ownerJSON";

export interface TransactionJSON {
type: string;
id: string;
execution: ExecutionJSON;
}
deployment?: DeploymentJSON;
execution?: ExecutionJSON;
fee: FeeJSON;
owner?: OwnerJSON;
}
4 changes: 2 additions & 2 deletions sdk/src/models/transaction/transactionObject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransitionObject } from "../transition/transitionObject";
import { DeploymentMetadata } from "../deploy";
import { DeploymentObject } from "../deployment/deploymentObject";

export interface TransactionObject {
id : string;
Expand All @@ -8,5 +8,5 @@ export interface TransactionObject {
baseFee : bigint;
priorityFee : bigint;
transitions : TransitionObject[];
deployment?: DeploymentMetadata;
deployment? : DeploymentObject;
}
78 changes: 32 additions & 46 deletions sdk/src/network-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PrivateKey,
Transaction,
} from "./wasm";
import { ConfirmedTransactionJSON } from "./models/confirmed_transaction";

type ProgramImports = { [key: string]: string | Program };

Expand Down Expand Up @@ -80,17 +81,37 @@ class AleoNetworkClient {
this.host = host + "/%%NETWORK%%";
}

/**
* Fetches data from the Aleo network and returns it as a JSON object.
*
* @param url
*/
async fetchData<Type>(
url = "/",
): Promise<Type> {
try {
return parseJSON(await this.fetchRaw(url));
} catch (error) {
throw new Error("Error fetching data.");
}
}

/**
* Fetches data from the Aleo network and returns it as an unparsed string.
*
* This method should be used when it is desired to reconstitute data returned
* from the network into a WASM object.
*
* @param url
*/
async fetchRaw(
url = "/",
): Promise<string> {
try {
const response = await get(this.host + url, {
headers: this.headers
});

const text = await response.text();
return parseJSON(text);

return await response.text();
} catch (error) {
throw new Error("Error fetching data.");
}
Expand Down Expand Up @@ -612,10 +633,10 @@ class AleoNetworkClient {
async getProgramMappingPlaintext(programId: string, mappingName: string, key: string | Plaintext): Promise<Plaintext> {
try {
const keyString = key instanceof Plaintext ? key.toString() : key;
const value = await this.fetchData<string>("/program/" + programId + "/mapping/" + mappingName + "/" + keyString);
const value = await this.fetchRaw("/program/" + programId + "/mapping/" + mappingName + "/" + keyString);
return Plaintext.fromString(value);
} catch (error) {
throw new Error("Failed to fetch mapping value");
throw new Error("Failed to fetch mapping value." + error);
}
}

Expand Down Expand Up @@ -676,7 +697,7 @@ class AleoNetworkClient {
*/
async getTransactionObject(transactionId: string): Promise<Transaction> {
try {
const transaction = await this.fetchData<string>("/transaction/" + transactionId);
const transaction = await this.fetchRaw("/transaction/" + transactionId);
return Transaction.fromString(transaction);
} catch (error) {
throw new Error("Error fetching transaction.");
Expand All @@ -690,35 +711,16 @@ class AleoNetworkClient {
* @example
* const transactions = networkClient.getTransactions(654);
*/
async getTransactions(height: number): Promise<Array<TransactionJSON>> {
try {
return await this.fetchData<Array<TransactionJSON>>("/block/" + height.toString() + "/transactions");
} catch (error) {
throw new Error("Error fetching transactions.");
}
}

/**
* Returns an array of transactions as wasm objects present at the specified block height.
*
* @param {number} height
* @example
* const transactions = networkClient.getTransactionObjects(654);
*
* let transaction_summaries = transactions.map(transaction => transaction.summary());
*/
async getTransactionObjects(height: number): Promise<Array<Transaction>> {
async getTransactions(height: number): Promise<Array<ConfirmedTransactionJSON>> {
try {
const transactionStrings = await this.fetchData<Array<string>>(`/block/${height}/transactions`);
return transactionStrings.map(transaction => Transaction.fromString(transaction));
return await this.fetchData<Array<ConfirmedTransactionJSON>>("/block/" + height.toString() + "/transactions");
} catch (error) {
throw new Error("Error fetching transactions.");
throw new Error("Error fetching transactions. " + error);
}
}

/**
* Returns the transactions in the memory pool. This method will only work with a validator node with its REST API
* enabled.
* Returns the transactions in the memory pool. This method requires access to a validator's REST API.
*
* @example
* const transactions = networkClient.getTransactionsInMempool();
Expand All @@ -731,22 +733,6 @@ class AleoNetworkClient {
}
}

/**
* Returns the transactions in the memory pool as wasm objects. This method will only work with a validator node with
* its REST API enabled.
*
* @example
* const transactions = networkClient.getTransactionsInMempool();
*/
async getTransactionObjectsInMempool(): Promise<Array<Transaction>> {
try {
const transactionStrings = await this.fetchData<Array<string>>("/memoryPool/transactions");
return transactionStrings.map(transaction => Transaction.fromString(transaction));
} catch (error) {
throw new Error("Error fetching transactions from mempool.");
}
}

/**
* Returns the transition ID of the transition corresponding to the ID of the input or output.
* @param {string} inputOrOutputID - ID of the input or output.
Expand Down
1 change: 0 additions & 1 deletion sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export function parseJSON(json: string): any {
function revive(key: string, value: any, context: any) {
if (Number.isInteger(value)) {
return BigInt(context.source);

} else {
return value;
}
Expand Down
Loading

0 comments on commit 7518507

Please sign in to comment.