Skip to content

Commit

Permalink
Some clean ups (#204)
Browse files Browse the repository at this point in the history
* clean up

* rebase
  • Loading branch information
0xmaayan authored Nov 21, 2023
1 parent 3728051 commit ae890bd
Show file tree
Hide file tree
Showing 34 changed files with 327 additions and 332 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.aptos/
.DS_Store
*/**/.DS_Store
.env/
.env
.history/
.idea/
.npm/
Expand All @@ -13,6 +13,7 @@ dist/
node_modules/
npm-debug.log
tmp/
examples/typescript/facoin/facoin.json

# Vim swap files
*.swp
4 changes: 2 additions & 2 deletions examples/typescript-esm/simple_sponsored_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* with a sponsor account to pay for the gas fee
*/
import "dotenv";
import { Account, Aptos, AptosConfig, Network, NetworkToNetworkName, U64 } from "@aptos-labs/ts-sdk";
import { Account, Aptos, AptosConfig, Network, NetworkToNetworkName } from "@aptos-labs/ts-sdk";

const ALICE_INITIAL_BALANCE = 100_000_000;
const SPONSOR_INITIAL_BALANCE = 100_000_000;
Expand Down Expand Up @@ -63,7 +63,7 @@ const example = async () => {
withFeePayer: true,
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [bob.accountAddress, new U64(TRANSFER_AMOUNT)],
functionArguments: [bob.accountAddress, TRANSFER_AMOUNT],
},
});

Expand Down
3 changes: 1 addition & 2 deletions examples/typescript-esm/simple_transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Network,
NetworkToNetworkName,
parseTypeTag,
U64,
} from "@aptos-labs/ts-sdk";

// TODO: There currently isn't a way to use the APTOS_COIN in the COIN_STORE due to a regex
Expand Down Expand Up @@ -89,7 +88,7 @@ const example = async () => {
data: {
function: "0x1::coin::transfer",
typeArguments: [parseTypeTag(APTOS_COIN)],
functionArguments: [AccountAddress.from(bob.accountAddress), new U64(TRANSFER_AMOUNT)],
functionArguments: [bob.accountAddress, TRANSFER_AMOUNT],
},
});

Expand Down
4 changes: 2 additions & 2 deletions examples/typescript/facoin/facoin.json

Large diffs are not rendered by default.

26 changes: 8 additions & 18 deletions examples/typescript/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import {
AccountAddress,
Aptos,
AptosConfig,
Bool,
Ed25519PrivateKey,
Network,
NetworkToNetworkName,
U64,
InputViewRequestData,
} from "@aptos-labs/ts-sdk";
import { createInterface } from "readline";
Expand All @@ -42,7 +40,7 @@ const getOptimalLpAmount = async (
): Promise<void> => {
const payload: InputViewRequestData = {
function: `${swap.toString()}::router::optimal_liquidity_amounts`,
functionArguments: [token1Addr.toString(), token2Addr.toString(), false, "200000", "300000", "200", "300"],
functionArguments: [token1Addr, token2Addr, false, "200000", "300000", "200", "300"],
};
const result = await aptos.view({ payload });
console.log("Optimal LP amount: ", result);
Expand All @@ -56,18 +54,10 @@ const addLiquidity = async (
token2Addr: AccountAddress,
): Promise<string> => {
const rawTxn = await aptos.build.transaction({
sender: deployer.accountAddress.toString(),
sender: deployer.accountAddress,
data: {
function: `${swap.toString()}::router::add_liquidity_entry`,
functionArguments: [
token1Addr,
token2Addr,
new Bool(false),
new U64(200000),
new U64(300000),
new U64(200),
new U64(300),
],
functionArguments: [token1Addr, token2Addr, false, 200000, 300000, 200, 300],
},
});
const pendingTxn = await aptos.signAndSubmitTransaction({ signer: deployer, transaction: rawTxn });
Expand All @@ -90,7 +80,7 @@ const swapAssets = async (
sender: deployer.accountAddress.toString(),
data: {
function: `${swap.toString()}::router::swap_entry`,
functionArguments: [new U64(amountIn), new U64(amountOutMin), fromToken, toToken, new Bool(false), recipient],
functionArguments: [amountIn, amountOutMin, fromToken, toToken, false, recipient],
},
});
const pendingTxn = await aptos.signAndSubmitTransaction({ signer: deployer, transaction: rawTxn });
Expand Down Expand Up @@ -138,7 +128,7 @@ const createLiquidityPool = async (
catCoinAddr: AccountAddress,
): Promise<string> => {
const rawTxn = await aptos.build.transaction({
sender: deployer.accountAddress.toString(),
sender: deployer.accountAddress,
data: {
function: `${swap.toString()}::router::create_pool`,
functionArguments: [dogCoinAddr, catCoinAddr, false],
Expand All @@ -152,7 +142,7 @@ const createLiquidityPool = async (

const initLiquidityPool = async (aptos: Aptos, swap: AccountAddress, deployer: Account): Promise<string> => {
const rawTxn = await aptos.build.transaction({
sender: deployer.accountAddress.toString(),
sender: deployer.accountAddress,
data: {
function: `${swap.toString()}::liquidity_pool::initialize`,
functionArguments: [],
Expand Down Expand Up @@ -184,7 +174,7 @@ const createFungibleAsset = async (aptos: Aptos, admin: Account): Promise<void>
*/
const mintCoin = async (aptos: Aptos, admin: Account, amount: number | bigint, coinName: string): Promise<string> => {
const rawTxn = await aptos.build.transaction({
sender: admin.accountAddress.toString(),
sender: admin.accountAddress,
data: {
function: `${admin.accountAddress.toString()}::${coinName}::mint`,
functionArguments: [admin.accountAddress, amount],
Expand Down Expand Up @@ -225,7 +215,7 @@ const example = async () => {
console.log(`Admin's address is: ${admin.accountAddress.toString()}`);
console.log(`Swap address is: ${swapAddress.toString()}`);
// Fund Admin account
await aptos.fundAccount({ accountAddress: admin.accountAddress.toString(), amount: 100_000_000 });
await aptos.fundAccount({ accountAddress: admin.accountAddress, amount: 100_000_000 });

console.log("\n====== Create Fungible Asset -> (Dog and Cat coin) ======\n");
await createFungibleAsset(aptos, admin);
Expand Down
6 changes: 5 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ module.exports = {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
testEnvironment: "node",
coveragePathIgnorePatterns: ["./src/internal/queries/", "./src/types/generated"],
coveragePathIgnorePatterns: [
"./src/internal/queries/",
"./src/types/generated",
"./tests/e2e/ans/publishANSContracts.ts",
],
testPathIgnorePatterns: ["dist/*", "examples/*"],
collectCoverage: true,
setupFiles: ["dotenv/config"],
Expand Down
8 changes: 4 additions & 4 deletions src/api/ans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export class ANS {
* point to addresses that are not the owner of the name
*
* ```ts
* await aptos.setTargetAddress({sender: alice, name: "test.aptos", address: bob.accountAddress.toString})
* await aptos.setTargetAddress({sender: alice, name: "test.aptos", address: bob.accountAddress})
* const address = await aptos.getTargetAddress({name: "test.aptos"})
* // address = bob.accountAddress.toString()
* // address = bob.accountAddress
* ```
*
* @param args.name - A string of the name: test.aptos.apt, test.apt, test, test.aptos, etc.
Expand All @@ -115,7 +115,7 @@ export class ANS {
* account also may not have a primary name.
*
* ```ts
* const name = await aptos.getPrimaryName({address: alice.accountAddress.toString()})
* const name = await aptos.getPrimaryName({address: alice.accountAddress})
* // name = test.aptos
* ```
*
Expand All @@ -134,7 +134,7 @@ export class ANS {
*
* ```ts
* await aptos.setPrimaryName({sender: alice, name: "test.aptos"})
* const primaryName = await aptos.getPrimaryName({address: alice.accountAddress.toString()})
* const primaryName = await aptos.getPrimaryName({address: alice.accountAddress})
* // primaryName = test.aptos
* ```
*
Expand Down
4 changes: 2 additions & 2 deletions src/api/aptosConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { AptosApiType, DEFAULT_NETWORK } from "../utils/const";
* This class holds the config information for the SDK client instance.
*/
export class AptosConfig {
/** The Network that this SDK is associated with. */
/** The Network that this SDK is associated with. Defaults to DEVNET */
readonly network: Network;

/**
* The client instance the SDK uses
* The client instance the SDK uses. Defaults to `@aptos-labs/aptos-client
*/
readonly client: Client;

Expand Down
18 changes: 9 additions & 9 deletions src/internal/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,12 @@ export async function getAccountTokensCount(args: {
query: graphqlQuery,
originMethod: "getAccountTokensCount",
});
if (!data.current_token_ownerships_v2_aggregate.aggregate) {
throw Error("Failed to get the count of account tokens");
}
return data.current_token_ownerships_v2_aggregate.aggregate.count;

// commonjs (aka cjs) doesnt handle Nullish Coalescing for some reason
// might be because of how ts infer the graphql generated scheme type
return data.current_token_ownerships_v2_aggregate.aggregate
? data.current_token_ownerships_v2_aggregate.aggregate.count
: 0;
}

export async function getAccountOwnedTokens(args: {
Expand Down Expand Up @@ -407,11 +409,9 @@ export async function getAccountTransactionsCount(args: {
originMethod: "getAccountTransactionsCount",
});

if (!data.account_transactions_aggregate.aggregate) {
throw Error("Failed to get the count of account transactions");
}

return data.account_transactions_aggregate.aggregate.count;
// commonjs (aka cjs) doesnt handle Nullish Coalescing for some reason
// might be because of how ts infer the graphql generated scheme type
return data.account_transactions_aggregate.aggregate ? data.account_transactions_aggregate.aggregate.count : 0;
}

export async function getAccountCoinsData(args: {
Expand Down
10 changes: 5 additions & 5 deletions src/internal/queries/getNumberOfDelegatorsQuery.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
query getNumberOfDelegators($where_condition: num_active_delegator_per_pool_bool_exp!, $order_by: [num_active_delegator_per_pool_order_by!]) {
num_active_delegator_per_pool(
where: $where_condition,
order_by: $order_by
) {
query getNumberOfDelegators(
$where_condition: num_active_delegator_per_pool_bool_exp
$order_by: [num_active_delegator_per_pool_order_by!]
) {
num_active_delegator_per_pool(where: $where_condition, order_by: $order_by) {
num_active_delegator
pool_address
}
Expand Down
14 changes: 7 additions & 7 deletions src/internal/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export async function getNumberOfDelegators(args: {
query: GetNumberOfDelegators,
variables: { where_condition: { pool_address: { _eq: address } } },
};
const data: GetNumberOfDelegatorsQuery = await queryIndexer<GetNumberOfDelegatorsQuery>({ aptosConfig, query });
if (data.num_active_delegator_per_pool.length === 0) {
throw Error("Delegator pool not found");
}
return data.num_active_delegator_per_pool[0].num_active_delegator;
const data = await queryIndexer<GetNumberOfDelegatorsQuery>({ aptosConfig, query });

// commonjs (aka cjs) doesnt handle Nullish Coalescing for some reason
// might be because of how ts infer the graphql generated scheme type
return data.num_active_delegator_per_pool[0] ? data.num_active_delegator_per_pool[0].num_active_delegator : 0;
}

export async function getNumberOfDelegatorsForAllPools(args: {
Expand All @@ -41,9 +41,9 @@ export async function getNumberOfDelegatorsForAllPools(args: {
const { aptosConfig, options } = args;
const query = {
query: GetNumberOfDelegators,
variables: { where_condition: {}, order_by: options?.orderBy },
variables: { order_by: options?.orderBy },
};
const data: GetNumberOfDelegatorsQuery = await queryIndexer<GetNumberOfDelegatorsQuery>({
const data = await queryIndexer<GetNumberOfDelegatorsQuery>({
aptosConfig,
query,
});
Expand Down
4 changes: 2 additions & 2 deletions src/internal/transactionSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export async function rotateAuthKey(args: {
const { aptosConfig, fromAccount, toNewPrivateKey } = args;
const accountInfo = await getInfo({
aptosConfig,
accountAddress: fromAccount.accountAddress.toString(),
accountAddress: fromAccount.accountAddress,
});

const newAccount = Account.fromPrivateKey({ privateKey: toNewPrivateKey, legacy: true });
Expand All @@ -305,7 +305,7 @@ export async function rotateAuthKey(args: {
// Generate transaction
const rawTxn = await generateTransaction({
aptosConfig,
sender: fromAccount.accountAddress.toString(),
sender: fromAccount.accountAddress,
data: {
function: "0x1::account::rotate_authentication_key",
functionArguments: [
Expand Down
2 changes: 1 addition & 1 deletion src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ export type GetNamesQuery = {
};

export type GetNumberOfDelegatorsQueryVariables = Types.Exact<{
where_condition: Types.NumActiveDelegatorPerPoolBoolExp;
where_condition?: Types.InputMaybe<Types.NumActiveDelegatorPerPoolBoolExp>;
order_by?: Types.InputMaybe<Array<Types.NumActiveDelegatorPerPoolOrderBy> | Types.NumActiveDelegatorPerPoolOrderBy>;
}>;

Expand Down
4 changes: 2 additions & 2 deletions src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export const GetNames = `
}
${AnsTokenFragmentFragmentDoc}`;
export const GetNumberOfDelegators = `
query getNumberOfDelegators($where_condition: num_active_delegator_per_pool_bool_exp!, $order_by: [num_active_delegator_per_pool_order_by!]) {
query getNumberOfDelegators($where_condition: num_active_delegator_per_pool_bool_exp, $order_by: [num_active_delegator_per_pool_order_by!]) {
num_active_delegator_per_pool(where: $where_condition, order_by: $order_by) {
num_active_delegator
pool_address
Expand Down Expand Up @@ -698,7 +698,7 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
);
},
getNumberOfDelegators(
variables: Types.GetNumberOfDelegatorsQueryVariables,
variables?: Types.GetNumberOfDelegatorsQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
): Promise<Types.GetNumberOfDelegatorsQuery> {
return withWrapper(
Expand Down
Loading

0 comments on commit ae890bd

Please sign in to comment.