Skip to content

Commit

Permalink
[view] Abi upgrades (#334)
Browse files Browse the repository at this point in the history
* [abi] Add number of signers to the ABI

* [refactor] Remove valueOf()

* [view] Add BCS view functions

* Update src/transactions/transactionBuilder/remoteAbi.ts

Co-authored-by: Maayan <maayan@aptoslabs.com>

* Update src/transactions/transactionBuilder/transactionBuilder.ts

Co-authored-by: Maayan <maayan@aptoslabs.com>

* [abi] Address comments

* fix test

* fix changelog

---------

Co-authored-by: Maayan <maayan@aptoslabs.com>
  • Loading branch information
gregnazario and 0xmaayan authored Mar 26, 2024
1 parent c2774d9 commit 19506cf
Show file tree
Hide file tree
Showing 18 changed files with 302 additions and 127 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T
- Handle `Unauthorized` server error
- Add function to create object address locally
- Add function to create token object address locally
- Add signers to entry function ABI for future signature count checking
- [`Breaking`] Add type-safe view functions with ABI support

# 1.10.0 (2024-03-11)

Expand Down
4 changes: 2 additions & 2 deletions examples/typescript-esm/multisig_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
Account,
Aptos,
AptosConfig,
InputViewRequestData,
Network,
NetworkToNetworkName,
MoveString,
Expand All @@ -30,6 +29,7 @@ import {
TransactionPayloadMultiSig,
MultiSig,
AccountAddress,
InputViewFunctionData,
} from "@aptos-labs/ts-sdk";

// Default to devnet, but allow for overriding
Expand Down Expand Up @@ -90,7 +90,7 @@ const settingUpMultiSigAccount = async () => {
// ===========================================================================================
// Get the next multisig account address. This will be the same as the account address of the multisig account we'll
// be creating.
const payload: InputViewRequestData = {
const payload: InputViewFunctionData = {
function: "0x1::multisig_account::get_next_multisig_account_address",
functionArguments: [owner1.accountAddress.toString()],
};
Expand Down
4 changes: 2 additions & 2 deletions examples/typescript/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import {
Aptos,
AptosConfig,
Ed25519PrivateKey,
InputViewFunctionData,
Network,
NetworkToNetworkName,
InputViewRequestData,
} from "@aptos-labs/ts-sdk";
import { createInterface } from "readline";
// Default to devnet, but allow for overriding
Expand All @@ -38,7 +38,7 @@ const getOptimalLpAmount = async (
token1Addr: AccountAddress,
token2Addr: AccountAddress,
): Promise<void> => {
const payload: InputViewRequestData = {
const payload: InputViewFunctionData = {
function: `${swap.toString()}::router::optimal_liquidity_amounts`,
functionArguments: [token1Addr, token2Addr, false, "200000", "300000", "200", "300"],
};
Expand Down
4 changes: 2 additions & 2 deletions examples/typescript/your_fungible_asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AnyNumber,
Aptos,
AptosConfig,
InputViewRequestData,
InputViewFunctionData,
Network,
NetworkToNetworkName,
} from "@aptos-labs/ts-sdk";
Expand Down Expand Up @@ -127,7 +127,7 @@ const getFaBalance = async (owner: Account, assetType: string): Promise<number>

/** Return the address of the managed fungible asset that's created when this module is deployed */
async function getMetadata(admin: Account): Promise<string> {
const payload: InputViewRequestData = {
const payload: InputViewFunctionData = {
function: `${admin.accountAddress}::fa_coin::get_metadata`,
functionArguments: [],
};
Expand Down
6 changes: 3 additions & 3 deletions src/api/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
getProcessorStatus,
getTableItem,
queryIndexer,
view,
} from "../internal/general";
import { view } from "../internal/view";
import {
AnyNumber,
Block,
Expand All @@ -23,9 +23,9 @@ import {
LedgerVersionArg,
MoveValue,
TableItemRequest,
InputViewRequestData,
} from "../types";
import { ProcessorType } from "../utils/const";
import { InputViewFunctionData } from "../transactions";

/**
* A class to query all `General` Aptos related queries
Expand Down Expand Up @@ -137,7 +137,7 @@ export class General {
* @returns an array of Move values
*/
async view<T extends Array<MoveValue>>(args: {
payload: InputViewRequestData;
payload: InputViewFunctionData;
options?: LedgerVersionArg;
}): Promise<T> {
return view<T>({ aptosConfig: this.config, ...args });
Expand Down
4 changes: 2 additions & 2 deletions src/client/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export async function get<Req extends {}, Res extends {}>(
method: "GET",
originMethod,
path,
contentType: contentType?.valueOf(),
acceptType: acceptType?.valueOf(),
contentType,
acceptType,
params,
overrides: {
...aptosConfig.clientConfig,
Expand Down
4 changes: 2 additions & 2 deletions src/client/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export async function post<Req extends {}, Res extends {}>(
originMethod,
path,
body,
contentType: contentType?.valueOf(),
acceptType: acceptType?.valueOf(),
contentType,
acceptType,
params,
overrides,
},
Expand Down
19 changes: 7 additions & 12 deletions src/internal/ans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import { AptosConfig } from "../api/aptosConfig";
import { Account, AccountAddress, AccountAddressInput } from "../core";
import { InputGenerateTransactionOptions, SimpleTransaction } from "../transactions/types";
import { GetANSNameResponse, MoveAddressType, MoveValue, OrderByArg, PaginationArgs, WhereArg } from "../types";
import { GetANSNameResponse, MoveAddressType, OrderByArg, PaginationArgs, WhereArg } from "../types";
import { GetNamesQuery } from "../types/generated/operations";
import { GetNames } from "../types/generated/queries";
import { CurrentAptosNamesBoolExp } from "../types/generated/types";
import { Network } from "../utils/apiEndpoints";
import { queryIndexer, view } from "./general";
import { queryIndexer } from "./general";
import { view } from "./view";
import { generateTransaction } from "./transactionSubmission";

export const VALIDATION_RULES_DESCRIPTION = [
Expand Down Expand Up @@ -85,12 +86,6 @@ function getRouterAddress(aptosConfig: AptosConfig): string {
return address;
}

const Some = <T>(value: T): MoveValue => ({ vec: [value] });
const None = (): MoveValue => ({ vec: [] });
// != here is intentional, we want to check for null and undefined
// eslint-disable-next-line eqeqeq
const Option = <T>(value: T | undefined | null): MoveValue => (value != undefined ? Some(value) : None());

const unwrapOption = <T>(option: any): T | undefined => {
if (!!option && typeof option === "object" && "vec" in option && Array.isArray(option.vec)) {
return option.vec[0];
Expand All @@ -108,7 +103,7 @@ export async function getOwnerAddress(args: { aptosConfig: AptosConfig; name: st
aptosConfig,
payload: {
function: `${routerAddress}::router::get_owner_addr`,
functionArguments: [domainName, Option(subdomainName)],
functionArguments: [domainName, subdomainName],
},
});

Expand Down Expand Up @@ -219,7 +214,7 @@ export async function getExpiration(args: { aptosConfig: AptosConfig; name: stri
aptosConfig,
payload: {
function: `${routerAddress}::router::get_expiration`,
functionArguments: [domainName, Option(subdomainName)],
functionArguments: [domainName, subdomainName],
},
});

Expand Down Expand Up @@ -303,7 +298,7 @@ export async function getTargetAddress(args: {
aptosConfig,
payload: {
function: `${routerAddress}::router::get_target_addr`,
functionArguments: [domainName, Option(subdomainName)],
functionArguments: [domainName, subdomainName],
},
});

Expand Down Expand Up @@ -570,6 +565,6 @@ export async function renewDomain(args: {
function sanitizeANSName(name: GetANSNameResponse[0]): GetANSNameResponse[0] {
return {
...name,
expiration_timestamp: new Date(name.expiration_timestamp).valueOf(),
expiration_timestamp: new Date(name.expiration_timestamp).getTime(),
};
}
26 changes: 1 addition & 25 deletions src/internal/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import {
GraphqlQuery,
LedgerInfo,
LedgerVersionArg,
MoveValue,
TableItemRequest,
ViewRequest,
InputViewRequestData,
} from "../types";
import { GetChainTopUserTransactionsQuery, GetProcessorStatusQuery } from "../types/generated/operations";
import { GetChainTopUserTransactions, GetProcessorStatus } from "../types/generated/queries";
Expand Down Expand Up @@ -84,27 +81,6 @@ export async function getTableItem<T>(args: {
return response.data as T;
}

export async function view<T extends Array<MoveValue> = Array<MoveValue>>(args: {
aptosConfig: AptosConfig;
payload: InputViewRequestData;
options?: LedgerVersionArg;
}): Promise<T> {
const { aptosConfig, payload, options } = args;
const { data } = await postAptosFullNode<ViewRequest, MoveValue[]>({
aptosConfig,
originMethod: "view",
path: "view",
params: { ledger_version: options?.ledgerVersion },
body: {
function: payload.function,
type_arguments: payload.typeArguments ?? [],
arguments: payload.functionArguments ?? [],
},
});

return data as T;
}

export async function getChainTopUserTransactions(args: {
aptosConfig: AptosConfig;
limit: number;
Expand Down Expand Up @@ -168,7 +144,7 @@ export async function getProcessorStatus(args: {
const { aptosConfig, processorType } = args;

const whereCondition: { processor: { _eq: string } } = {
processor: { _eq: processorType.valueOf() },
processor: { _eq: processorType },
};

const graphqlQuery = {
Expand Down
27 changes: 10 additions & 17 deletions src/internal/transactionSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
generateSignedTransaction,
sign,
generateSigningMessage,
generateTransactionPayloadWithABI,
} from "../transactions/transactionBuilder/transactionBuilder";
import {
InputGenerateTransactionData,
Expand Down Expand Up @@ -111,20 +110,14 @@ export async function buildTransactionPayload(
// TODO: Add ABI checking later
payload = await generateTransactionPayload(data);
} else if ("multisigAddress" in data) {
if (data.abi) {
payload = generateTransactionPayloadWithABI({ abi: data.abi, ...data });
} else {
generateTransactionPayloadData = {
aptosConfig,
multisigAddress: data.multisigAddress,
function: data.function,
functionArguments: data.functionArguments,
typeArguments: data.typeArguments,
};
payload = await generateTransactionPayload(generateTransactionPayloadData);
}
} else if (data.abi) {
payload = generateTransactionPayloadWithABI({ abi: data.abi, ...data });
generateTransactionPayloadData = {
aptosConfig,
multisigAddress: data.multisigAddress,
function: data.function,
functionArguments: data.functionArguments,
typeArguments: data.typeArguments,
};
payload = await generateTransactionPayload(generateTransactionPayloadData);
} else {
generateTransactionPayloadData = {
aptosConfig,
Expand Down Expand Up @@ -365,9 +358,9 @@ export async function rotateAuthKey(args: {
data: {
function: "0x1::account::rotate_authentication_key",
functionArguments: [
new U8(fromAccount.signingScheme.valueOf()), // from scheme
new U8(fromAccount.signingScheme), // from scheme
MoveVector.U8(fromAccount.publicKey.toUint8Array()),
new U8(newAccount.signingScheme.valueOf()), // to scheme
new U8(newAccount.signingScheme), // to scheme
MoveVector.U8(newAccount.publicKey.toUint8Array()),
MoveVector.U8(proofSignedByCurrentPrivateKey.toUint8Array()),
MoveVector.U8(proofSignedByNewPrivateKey.toUint8Array()),
Expand Down
35 changes: 35 additions & 0 deletions src/internal/view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import { LedgerVersionArg, MimeType, MoveValue } from "../types";
import { AptosConfig } from "../api/aptosConfig";
import { generateViewFunctionPayload, InputViewFunctionData } from "../transactions";
import { Serializer } from "../bcs";
import { postAptosFullNode } from "../client";

export async function view<T extends Array<MoveValue> = Array<MoveValue>>(args: {
aptosConfig: AptosConfig;
payload: InputViewFunctionData;
options?: LedgerVersionArg;
}): Promise<T> {
const { aptosConfig, payload, options } = args;
const viewFunctionPayload = await generateViewFunctionPayload({
...payload,
aptosConfig,
});

const serializer = new Serializer();
viewFunctionPayload.serialize(serializer);
const bytes = serializer.toUint8Array();

const { data } = await postAptosFullNode<Uint8Array, MoveValue[]>({
aptosConfig,
path: "view",
originMethod: "view",
contentType: MimeType.BCS_VIEW_FUNCTION,
params: { ledger_version: options?.ledgerVersion },
body: bytes,
});

return data as T;
}
Loading

0 comments on commit 19506cf

Please sign in to comment.