From 0aa527a5a2c2db43775d223b2127052b5877595d Mon Sep 17 00:00:00 2001 From: mpsc0x Date: Mon, 14 Oct 2024 15:16:13 +0300 Subject: [PATCH] feat: updated AptosProvider, examples and contracts --- examples/{ => admin}/mintUnderlyings.ts | 10 +- .../setAssetPrices.ts} | 10 +- .../setupTestnet.ts} | 9 +- examples/{ => users}/borrow.ts | 8 +- examples/{ => users}/getAddresses.ts | 8 +- examples/{ => users}/getAptos.ts | 6 +- examples/{ => users}/getProtocolData.ts | 16 +-- examples/{ => users}/getUserData.ts | 12 +- examples/{ => users}/repay.ts | 8 +- examples/{ => users}/supply.ts | 13 +- examples/{ => users}/withdraw.ts | 13 +- package.json | 21 +-- pnpm-lock.yaml | 31 +++-- src/clients/aptosProvider.ts | 131 ++++++++++++++---- src/clients/index.ts | 20 ++- src/contracts/acl_manage.ts | 4 +- src/contracts/bridge.ts | 6 +- src/contracts/flashloan.ts | 7 +- src/contracts/oracle.ts | 4 +- src/contracts/pool.ts | 6 +- src/contracts/supply_borrow.ts | 7 +- src/contracts/tokens.ts | 7 +- src/contracts/uiIncentiveDataProvider.ts | 7 +- src/contracts/uiPoolDataProvider.ts | 7 +- 24 files changed, 235 insertions(+), 136 deletions(-) rename examples/{ => admin}/mintUnderlyings.ts (82%) rename examples/{setAssetsPrice.ts => admin/setAssetPrices.ts} (77%) rename examples/{setupTtestnet.ts => admin/setupTestnet.ts} (96%) rename examples/{ => users}/borrow.ts (84%) rename examples/{ => users}/getAddresses.ts (84%) rename examples/{ => users}/getAptos.ts (86%) rename examples/{ => users}/getProtocolData.ts (90%) rename examples/{ => users}/getUserData.ts (81%) rename examples/{ => users}/repay.ts (88%) rename examples/{ => users}/supply.ts (76%) rename examples/{ => users}/withdraw.ts (76%) diff --git a/examples/mintUnderlyings.ts b/examples/admin/mintUnderlyings.ts similarity index 82% rename from examples/mintUnderlyings.ts rename to examples/admin/mintUnderlyings.ts index ef799e5..e4cea45 100644 --- a/examples/mintUnderlyings.ts +++ b/examples/admin/mintUnderlyings.ts @@ -1,8 +1,8 @@ import { Account, AccountAddress, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"; -import { UnderlyingTokensClient } from "../src/clients/underlyingTokensClient"; -import { PoolClient } from "../src/clients/poolClient"; -import { AptosProvider } from "../src/clients"; -import { testnetConfig } from "../src/configs/testnet"; +import { UnderlyingTokensClient } from "../../src/clients/underlyingTokensClient"; +import { PoolClient } from "../../src/clients/poolClient"; +import { AptosProvider } from "../../src/clients"; +import { testnetConfig } from "../../src/configs/testnet"; const UNDERLYING_MANAGER_PRIVATE_KEY = "0x0"; const ADDRESSES_TO_FUND = ["0x0"].map((addr) => @@ -12,7 +12,7 @@ const fundAmount = BigInt(1000); (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); // all underlying-tokens-related operations client const underlyingTokensClient = new UnderlyingTokensClient(aptosProvider); // all pool-related operations client diff --git a/examples/setAssetsPrice.ts b/examples/admin/setAssetPrices.ts similarity index 77% rename from examples/setAssetsPrice.ts rename to examples/admin/setAssetPrices.ts index 6ffe925..b857b5b 100644 --- a/examples/setAssetsPrice.ts +++ b/examples/admin/setAssetPrices.ts @@ -1,10 +1,8 @@ -/* eslint-disable no-console */ -/* eslint-disable no-await-in-loop */ import dotenv from "dotenv"; import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"; -import { OracleClient, PoolClient } from "../src/clients"; -import { AptosProvider } from "../src/clients/aptosProvider"; -import { testnetConfig } from "../src/configs/testnet"; +import { OracleClient, PoolClient } from "../../src/clients"; +import { AptosProvider } from "../../src/clients/aptosProvider"; +import { testnetConfig } from "../../src/configs/testnet"; dotenv.config(); @@ -17,7 +15,7 @@ const priceMapper = { (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); if (!process.env.AAVE_MOCK_ORACLE_PRIVATE_KEY) { throw new Error(`AAVE_MOCK_ORACLE_PRIVATE_KEY env was not found`); diff --git a/examples/setupTtestnet.ts b/examples/admin/setupTestnet.ts similarity index 96% rename from examples/setupTtestnet.ts rename to examples/admin/setupTestnet.ts index 97dc7f8..4d39aa1 100644 --- a/examples/setupTtestnet.ts +++ b/examples/admin/setupTestnet.ts @@ -1,9 +1,8 @@ -/* eslint-disable no-await-in-loop */ import dotenv from "dotenv"; import { Account, AccountAddress, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../src/clients/aptosProvider"; -import { testnetConfig } from "../src/configs/testnet"; -import { PoolClient, UnderlyingTokensClient } from "../src/clients"; +import { AptosProvider } from "../../src/clients/aptosProvider"; +import { testnetConfig } from "../../src/configs/testnet"; +import { PoolClient, UnderlyingTokensClient } from "../../src/clients"; dotenv.config(); @@ -130,7 +129,7 @@ const poolSigner = Account.fromPrivateKey({ }); (async () => { - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); const poolClient = new PoolClient(aptosProvider, poolSigner); const underlyingTokenClient = new UnderlyingTokensClient( diff --git a/examples/borrow.ts b/examples/users/borrow.ts similarity index 84% rename from examples/borrow.ts rename to examples/users/borrow.ts index 741728e..6c5a9cd 100644 --- a/examples/borrow.ts +++ b/examples/users/borrow.ts @@ -1,7 +1,7 @@ import { Account, Ed25519Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"; -import { PoolClient } from "../src/clients/poolClient"; -import { AptosProvider, CoreClient } from "../src/clients"; -import { testnetConfig } from "../src/configs/testnet"; +import { PoolClient } from "../../src/clients/poolClient"; +import { AptosProvider, CoreClient } from "../../src/clients"; +import { testnetConfig } from "../../src/configs/testnet"; const USER_APTOS_ACCOUNT_PRIVATE_KEY = "0x0"; const CURRENCY_TO_BORROW = "DAI"; @@ -9,7 +9,7 @@ const AMOUNT_TO_BORROW = "100"; (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); // all pool-related operations client const poolClient = new PoolClient(aptosProvider); // user account diff --git a/examples/getAddresses.ts b/examples/users/getAddresses.ts similarity index 84% rename from examples/getAddresses.ts rename to examples/users/getAddresses.ts index 18b165b..74c9f98 100644 --- a/examples/getAddresses.ts +++ b/examples/users/getAddresses.ts @@ -1,10 +1,10 @@ -import { AptosProvider } from "../src/clients"; -import { PoolAddressesProviderClient } from "../src/clients/poolAddressesProviderClient"; -import { testnetConfig } from "../src/configs/testnet"; +import { AptosProvider } from "../../src/clients"; +import { PoolAddressesProviderClient } from "../../src/clients/poolAddressesProviderClient"; +import { testnetConfig } from "../../src/configs/testnet"; (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); // pool addresses provider const poolAddressesProviderClient = new PoolAddressesProviderClient( diff --git a/examples/getAptos.ts b/examples/users/getAptos.ts similarity index 86% rename from examples/getAptos.ts rename to examples/users/getAptos.ts index 7bdb27d..53a362c 100644 --- a/examples/getAptos.ts +++ b/examples/users/getAptos.ts @@ -1,6 +1,6 @@ import { Account, AccountAddress, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../src/clients"; -import { testnetConfig } from "../src/configs/testnet"; +import { AptosProvider } from "../../src/clients"; +import { testnetConfig } from "../../src/configs/testnet"; const aptFunderPrivateKey = "0x0"; const addressesToFund = ["0x0"].map((addr) => AccountAddress.fromString(addr)); @@ -8,7 +8,7 @@ const fundAmount = BigInt(0.5); (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); try { // set the tx sender diff --git a/examples/getProtocolData.ts b/examples/users/getProtocolData.ts similarity index 90% rename from examples/getProtocolData.ts rename to examples/users/getProtocolData.ts index 4117100..57b5e3b 100644 --- a/examples/getProtocolData.ts +++ b/examples/users/getProtocolData.ts @@ -1,14 +1,14 @@ -import { ATokensClient } from "../src/clients/aTokensClient"; -import { UnderlyingTokensClient } from "../src/clients/underlyingTokensClient"; -import { UiPoolDataProviderClient } from "../src/clients/uiPoolDataProvider"; -import { PoolClient } from "../src/clients/poolClient"; -import { AptosProvider } from "../src/clients/aptosProvider"; -import { testnetConfig } from "../src/configs/testnet"; -import { VariableTokensClient } from "../src/clients/variableTokensClient"; +import { ATokensClient } from "../../src/clients/aTokensClient"; +import { UnderlyingTokensClient } from "../../src/clients/underlyingTokensClient"; +import { UiPoolDataProviderClient } from "../../src/clients/uiPoolDataProvider"; +import { PoolClient } from "../../src/clients/poolClient"; +import { AptosProvider } from "../../src/clients/aptosProvider"; +import { testnetConfig } from "../../src/configs/testnet"; +import { VariableTokensClient } from "../../src/clients/variableTokensClient"; (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); // all atokens-related operations client const aTokensClient = new ATokensClient(aptosProvider); diff --git a/examples/getUserData.ts b/examples/users/getUserData.ts similarity index 81% rename from examples/getUserData.ts rename to examples/users/getUserData.ts index 4e9493f..6a3cb3a 100644 --- a/examples/getUserData.ts +++ b/examples/users/getUserData.ts @@ -1,15 +1,15 @@ import { AccountAddress } from "@aptos-labs/ts-sdk"; -import { UnderlyingTokensClient } from "../src/clients/underlyingTokensClient"; -import { UiPoolDataProviderClient } from "../src/clients/uiPoolDataProvider"; -import { PoolClient } from "../src/clients/poolClient"; -import { AptosProvider } from "../src/clients/aptosProvider"; -import { testnetConfig } from "../src/configs/testnet"; +import { UnderlyingTokensClient } from "../../src/clients/underlyingTokensClient"; +import { UiPoolDataProviderClient } from "../../src/clients/uiPoolDataProvider"; +import { PoolClient } from "../../src/clients/poolClient"; +import { AptosProvider } from "../../src/clients/aptosProvider"; +import { testnetConfig } from "../../src/configs/testnet"; const USER_APTOS_ACCOUNT = "0x0"; (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); const uiPoolDataProviderClient = new UiPoolDataProviderClient(aptosProvider); const poolClient = new PoolClient(aptosProvider); diff --git a/examples/repay.ts b/examples/users/repay.ts similarity index 88% rename from examples/repay.ts rename to examples/users/repay.ts index 741cfdc..92e8063 100644 --- a/examples/repay.ts +++ b/examples/users/repay.ts @@ -1,9 +1,9 @@ import { Account, Ed25519Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"; import { MaxUint256 } from "ethers"; import { BigNumber } from "bignumber.js"; -import { PoolClient } from "../src/clients/poolClient"; -import { AptosProvider, CoreClient } from "../src/clients"; -import { testnetConfig } from "../src/configs/testnet"; +import { PoolClient } from "../../src/clients/poolClient"; +import { AptosProvider, CoreClient } from "../../src/clients"; +import { testnetConfig } from "../../src/configs/testnet"; const USER_APTOS_ACCOUNT_PRIVATE_KEY = "0x0"; const CURRENCY_TO_REPAY = "DAI"; @@ -12,7 +12,7 @@ const USE_A_TOKENS = true; (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); // all pool-related operations client const poolClient = new PoolClient(aptosProvider); // user account diff --git a/examples/supply.ts b/examples/users/supply.ts similarity index 76% rename from examples/supply.ts rename to examples/users/supply.ts index bacf3ba..b5a1e0d 100644 --- a/examples/supply.ts +++ b/examples/users/supply.ts @@ -1,18 +1,15 @@ -/* eslint-disable no-console */ -/* eslint-disable no-await-in-loop */ import { Account, Ed25519Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"; -import { PoolClient } from "../src/clients/poolClient"; -import { AptosProvider, CoreClient } from "../src/clients"; -import { testnetConfig } from "../src/configs/testnet"; +import { PoolClient } from "../../src/clients/poolClient"; +import { AptosProvider, CoreClient } from "../../src/clients"; +import { testnetConfig } from "../../src/configs/testnet"; -const USER_APTOS_ACCOUNT_PRIVATE_KEY = - "0xf5e502058d6995bccf625107f12f138cc4ff3f57b1487a256a6dec23f338f831"; +const USER_APTOS_ACCOUNT_PRIVATE_KEY = "0x0"; const CURRENCY_TO_SUPPLY = "DAI"; const AMOUNT_TO_SUPPLY = "100"; (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); // all pool-related operations client const poolClient = new PoolClient(aptosProvider); // user account diff --git a/examples/withdraw.ts b/examples/users/withdraw.ts similarity index 76% rename from examples/withdraw.ts rename to examples/users/withdraw.ts index 5e548df..a6a2d00 100644 --- a/examples/withdraw.ts +++ b/examples/users/withdraw.ts @@ -1,18 +1,15 @@ -/* eslint-disable no-console */ -/* eslint-disable no-await-in-loop */ import { Account, Ed25519Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"; -import { PoolClient } from "../src/clients/poolClient"; -import { AptosProvider, CoreClient } from "../src/clients"; -import { testnetConfig } from "../src/configs/testnet"; +import { PoolClient } from "../../src/clients/poolClient"; +import { AptosProvider, CoreClient } from "../../src/clients"; +import { testnetConfig } from "../../src/configs/testnet"; -const USER_APTOS_ACCOUNT_PRIVATE_KEY = - "0xf5e502058d6995bccf625107f12f138cc4ff3f57b1487a256a6dec23f338f831"; +const USER_APTOS_ACCOUNT_PRIVATE_KEY = "0x0"; const CURRENCY_TO_WITHDRAW = "DAI"; const AMOUNT_TO_WITHDRAW = "100"; (async () => { // global aptos provider - const aptosProvider = new AptosProvider(testnetConfig); + const aptosProvider = AptosProvider.fromConfig(testnetConfig); // all pool-related operations client const poolClient = new PoolClient(aptosProvider); // user account diff --git a/package.json b/package.json index bae5b35..1c6734b 100644 --- a/package.json +++ b/package.json @@ -19,14 +19,16 @@ "update-version-patch": "pnpm version patch", "test": "jest", "test:cov": "jest --coverage", - "supply": "ts-node examples/supply.ts", - "borrow": "ts-node examples/borrow.ts", - "withdraw": "ts-node examples/withdraw.ts", - "repay": "ts-node examples/repay.ts", - "get-aptos": "ts-node examples/getAptos.ts", - "get-addresses": "ts-node examples/getAddresses.ts", - "get-user-data": "ts-node examples/getUserData.ts", - "setup-testnet": "ts-node examples/setupTestnet.ts", + "supply": "ts-node examples/users/supply.ts", + "borrow": "ts-node examples/users/borrow.ts", + "withdraw": "ts-node examples/users/withdraw.ts", + "repay": "ts-node examples/users/repay.ts", + "get-aptos": "ts-node examples/quiries/getAptos.ts", + "get-addresses": "ts-node examples/quiries/getAddresses.ts", + "get-user-data": "ts-node examples/quiries/getUserData.ts", + "setup-testnet": "ts-node examples/admin/setupTestnet.ts", + "mint-underlyings": "ts-node examples/admin/mintUnderlyings.ts", + "set-asset-prices": "ts-node examples/admin/setAssetPrices.ts", "lint": "eslint . --no-cache --ignore-pattern 'bundle.js'", "lint:fix": "eslint . --fix --no-cache --ignore-pattern 'bundle.js'", "prettier": "prettier --check .", @@ -71,7 +73,8 @@ "events": "^3.3.0", "jest-environment-jsdom": "^29.7.0", "tslib": "^2.7.0", - "util": "^0.12.5" + "util": "^0.12.5", + "yaml": "^2.6.0" }, "devDependencies": { "@types/bn.js": "^5.1.6", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2e2c174..eac144b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,9 @@ importers: util: specifier: ^0.12.5 version: 0.12.5 + yaml: + specifier: ^2.6.0 + version: 2.6.0 optionalDependencies: bufferutil: specifier: ^4.0.8 @@ -129,7 +132,7 @@ importers: version: 29.2.5(@babel/core@7.25.7)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.25.7))(jest@29.7.0(@types/node@22.7.5)(ts-node@10.9.2(@types/node@22.7.5)(typescript@5.6.3)))(typescript@5.6.3) ts-loader: specifier: ^9.5.1 - version: 9.5.1(typescript@5.6.3)(webpack@5.95.0(webpack-cli@5.1.4)) + version: 9.5.1(typescript@5.6.3)(webpack@5.95.0) ts-node: specifier: ^10.9.2 version: 10.9.2(@types/node@22.7.5)(typescript@5.6.3) @@ -4436,8 +4439,8 @@ packages: resolution: {integrity: sha512-X/v7VDnK+sxbQ2Imq4Jt2PRUsRsP7UcpSl3Llg6+NRRqWLIvxkMFYtH1FmvwNGYRKKPa+EPA4qDBlI9WVG1UKw==} engines: {node: '>= 6'} - yaml@2.5.1: - resolution: {integrity: sha512-bLQOjaX/ADgQ20isPJRvF0iRUHIxVhYvr53Of7wGcWlO2jvtUlH5m87DsmulFVxRpNLOnI4tB6p/oh8D7kpn9Q==} + yaml@2.6.0: + resolution: {integrity: sha512-a6ae//JvKDEra2kdi1qzCyrJW/WZCgFi8ydDV+eXExl95t+5R+ijnqHJbz9tmMh8FUjx3iv2fCQ4dclAQlO2UQ==} engines: {node: '>= 14'} hasBin: true @@ -5462,17 +5465,17 @@ snapshots: '@webassemblyjs/ast': 1.12.1 '@xtuc/long': 4.2.2 - '@webpack-cli/configtest@2.1.1(webpack-cli@5.1.4(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4))': + '@webpack-cli/configtest@2.1.1(webpack-cli@5.1.4)(webpack@5.95.0)': dependencies: webpack: 5.95.0(webpack-cli@5.1.4) webpack-cli: 5.1.4(webpack@5.95.0) - '@webpack-cli/info@2.0.2(webpack-cli@5.1.4(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4))': + '@webpack-cli/info@2.0.2(webpack-cli@5.1.4)(webpack@5.95.0)': dependencies: webpack: 5.95.0(webpack-cli@5.1.4) webpack-cli: 5.1.4(webpack@5.95.0) - '@webpack-cli/serve@2.0.5(webpack-cli@5.1.4(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4))': + '@webpack-cli/serve@2.0.5(webpack-cli@5.1.4)(webpack@5.95.0)': dependencies: webpack: 5.95.0(webpack-cli@5.1.4) webpack-cli: 5.1.4(webpack@5.95.0) @@ -8960,7 +8963,7 @@ snapshots: tapable@2.2.1: {} - terser-webpack-plugin@5.3.10(webpack@5.95.0(webpack-cli@5.1.4)): + terser-webpack-plugin@5.3.10(webpack@5.95.0): dependencies: '@jridgewell/trace-mapping': 0.3.25 jest-worker: 27.5.1 @@ -9054,7 +9057,7 @@ snapshots: '@jest/types': 29.6.3 babel-jest: 29.7.0(@babel/core@7.25.7) - ts-loader@9.5.1(typescript@5.6.3)(webpack@5.95.0(webpack-cli@5.1.4)): + ts-loader@9.5.1(typescript@5.6.3)(webpack@5.95.0): dependencies: chalk: 4.1.2 enhanced-resolve: 5.17.1 @@ -9155,7 +9158,7 @@ snapshots: minimatch: 9.0.5 shiki: 1.22.0 typescript: 5.6.3 - yaml: 2.5.1 + yaml: 2.6.0 typescript-eslint@8.8.1(eslint@9.12.0)(typescript@5.6.3): dependencies: @@ -9359,9 +9362,9 @@ snapshots: webpack-cli@5.1.4(webpack@5.95.0): dependencies: '@discoveryjs/json-ext': 0.5.7 - '@webpack-cli/configtest': 2.1.1(webpack-cli@5.1.4(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4)) - '@webpack-cli/info': 2.0.2(webpack-cli@5.1.4(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4)) - '@webpack-cli/serve': 2.0.5(webpack-cli@5.1.4(webpack@5.95.0))(webpack@5.95.0(webpack-cli@5.1.4)) + '@webpack-cli/configtest': 2.1.1(webpack-cli@5.1.4)(webpack@5.95.0) + '@webpack-cli/info': 2.0.2(webpack-cli@5.1.4)(webpack@5.95.0) + '@webpack-cli/serve': 2.0.5(webpack-cli@5.1.4)(webpack@5.95.0) colorette: 2.0.20 commander: 10.0.1 cross-spawn: 7.0.3 @@ -9403,7 +9406,7 @@ snapshots: neo-async: 2.6.2 schema-utils: 3.3.0 tapable: 2.2.1 - terser-webpack-plugin: 5.3.10(webpack@5.95.0(webpack-cli@5.1.4)) + terser-webpack-plugin: 5.3.10(webpack@5.95.0) watchpack: 2.4.2 webpack-sources: 3.2.3 optionalDependencies: @@ -9527,7 +9530,7 @@ snapshots: dependencies: '@babel/runtime': 7.25.7 - yaml@2.5.1: {} + yaml@2.6.0: {} yargs-parser@21.1.1: {} diff --git a/src/clients/aptosProvider.ts b/src/clients/aptosProvider.ts index ae97319..1f36a8e 100644 --- a/src/clients/aptosProvider.ts +++ b/src/clients/aptosProvider.ts @@ -3,7 +3,12 @@ import { AptosConfig, Aptos, AccountAddress, + Ed25519PrivateKey, + Account, } from "@aptos-labs/ts-sdk"; +import { PathLike } from "fs"; +import fs from "fs"; +import YAML from "yaml"; export interface AptosProviderConfig { network: Network; @@ -15,7 +20,6 @@ export interface AptosProviderConfig { AAVE_CONFIG: string; AAVE_MOCK_ORACLE: string; AAVE_POOL: string; - AAVE_ROLE_SUPER_ADMIN: string; }; oracle: { URL: string; @@ -25,53 +29,124 @@ export interface AptosProviderConfig { }; } +export interface AptosAccountConfig { + private_key: string; + public_key: string; + account: string; + rest_url: string; + faucet_url: string; +} + +export enum AAVE_PROFILES { + A_TOKENS = "a_tokens", + UNDERLYING_TOKENS = "underlying_tokens", + VARIABLE_TOKENS = "variable_tokens", + AAVE_ACL = "aave_acl", + AAVE_CONFIG = "aave_config", + AAVE_MOCK_ORACLE = "aave_mock_oracle", + AAVE_ORACLE = "aave_oracle", + AAVE_POOL = "aave_pool", + AAVE_LARGE_PACKAGES = "aave_large_packages", + AAVE_MATH = "aave_math", +} + export class AptosProvider { - private readonly network: Network; + private network: Network; - private readonly oracleUrl: string; + private oracleUrl: string; - private accountProfilesMap: Map = new Map(); + private profileAddressMap: Map = new Map(); + private profileAccountMap: Map = new Map(); private aptos: Aptos; - constructor(config: AptosProviderConfig) { - this.network = config.network; - this.oracleUrl = config.oracle.URL; - this.accountProfilesMap.set( - "A_TOKENS_ADDRESS", + private constructor() {} + + public setNetwork(network: Network) { + this.network = network; + } + + public setOracleUrl(oracleUrl: string) { + this.oracleUrl = oracleUrl; + } + + public addProfileAddress(profileName: string, address: AccountAddress) { + this.profileAddressMap.set(profileName, address); + } + + public addProfileAccount(profileName: string, account: Ed25519PrivateKey) { + this.profileAccountMap.set(profileName, account); + } + + public setAptos(aptosConfig: AptosConfig) { + this.aptos = new Aptos(aptosConfig); + } + + public static fromConfig(config: AptosProviderConfig): AptosProvider { + let aptosProvider = new AptosProvider(); + aptosProvider.setNetwork(config.network); + aptosProvider.setOracleUrl(config.oracle.URL); + aptosProvider.addProfileAddress( + AAVE_PROFILES.A_TOKENS, AccountAddress.fromString(config.addresses.A_TOKENS), ); - this.accountProfilesMap.set( - "UNDERLYING_TOKENS_ADDRESS", + aptosProvider.addProfileAddress( + AAVE_PROFILES.UNDERLYING_TOKENS, AccountAddress.fromString(config.addresses.UNDERLYING_TOKENS), ); - this.accountProfilesMap.set( - "VARIABLE_TOKENS_ADDRESS", + aptosProvider.addProfileAddress( + AAVE_PROFILES.VARIABLE_TOKENS, AccountAddress.fromString(config.addresses.VARIABLE_TOKENS), ); - this.accountProfilesMap.set( - "AAVE_ACL_ADDRESS", + aptosProvider.addProfileAddress( + AAVE_PROFILES.AAVE_ACL, AccountAddress.fromString(config.addresses.AAVE_ACL), ); - this.accountProfilesMap.set( - "AAVE_CONFIG_ADDRESS", + aptosProvider.addProfileAddress( + AAVE_PROFILES.AAVE_CONFIG, AccountAddress.fromString(config.addresses.AAVE_CONFIG), ); - this.accountProfilesMap.set( - "AAVE_MOCK_ORACLE_ADDRESS", + aptosProvider.addProfileAddress( + AAVE_PROFILES.AAVE_MOCK_ORACLE, AccountAddress.fromString(config.addresses.AAVE_MOCK_ORACLE), ); - this.accountProfilesMap.set( - "AAVE_POOL_ADDRESS", + aptosProvider.addProfileAddress( + AAVE_PROFILES.AAVE_POOL, AccountAddress.fromString(config.addresses.AAVE_POOL), ); - this.accountProfilesMap.set( - "AAVE_ROLE_SUPER_ADMIN_ADDRESS", - AccountAddress.fromString(config.addresses.AAVE_ROLE_SUPER_ADMIN), - ); + const aptosConfig = new AptosConfig({ + network: aptosProvider.getNetwork(), + }); + aptosProvider.setAptos(aptosConfig); + return aptosProvider; + } - const aptosConfig = new AptosConfig({ network: this.network }); - this.aptos = new Aptos(aptosConfig); + public static fromAptosConfigFile(aptosConfigFile: PathLike): AptosProvider { + // read profile set + if (!fs.existsSync(aptosConfigFile as PathLike)) { + throw new Error( + `Aptos config file under path ${aptosConfigFile} does not exist`, + ); + } + const aptosConfigData = fs.readFileSync(aptosConfigFile, "utf8"); + + let aptosProvider = new AptosProvider(); + + const parsedYaml = YAML.parse(aptosConfigData); + for (const profile of Object.keys(parsedYaml.profiles)) { + const profileConfig = parsedYaml.profiles[profile] as AptosAccountConfig; + const aptosPrivateKey = new Ed25519PrivateKey(profileConfig.private_key); + aptosProvider.addProfileAccount(profile, aptosPrivateKey); + const profileAccount = Account.fromPrivateKey({ + privateKey: aptosPrivateKey, + }); + aptosProvider.addProfileAddress(profile, profileAccount.accountAddress); + } + const aptosConfig = new AptosConfig({ + network: aptosProvider.getNetwork(), + }); + aptosProvider.setAptos(aptosConfig); + return aptosProvider; } /** Returns the aptos instance. */ @@ -81,7 +156,7 @@ export class AptosProvider { /** Returns the account profile by name if found. */ public getProfileAccountByName(profileName: string): AccountAddress { - return this.accountProfilesMap.get(profileName); + return this.profileAddressMap.get(profileName); } /** Gets the selected network. */ diff --git a/src/clients/index.ts b/src/clients/index.ts index a76f1dc..102110f 100644 --- a/src/clients/index.ts +++ b/src/clients/index.ts @@ -1,10 +1,28 @@ export { AclClient } from "./aclClient"; -export { AptosProvider, AptosProviderConfig } from "./aptosProvider"; +export { + AptosProvider, + AptosProviderConfig, + AptosAccountConfig, + AAVE_PROFILES, +} from "./aptosProvider"; export { ATokensClient } from "./aTokensClient"; export { CoreClient } from "./coreClient"; export { PoolClient } from "./poolClient"; export { OracleClient } from "./oracleClient"; export { UnderlyingTokensClient } from "./underlyingTokensClient"; +export { BridgeClient } from "./bridgeClient"; +export { FlashloanClient } from "./flashloanClient"; +export { VariableTokensClient } from "./variableTokensClient"; +export { PoolAddressesProviderClient } from "./poolAddressesProviderClient"; +export { + AggregatedReserveIncentiveData, + IncentiveData, + RewardInfo, + UiIncentiveDataProviderClient, + UserIncentiveData, + UserReserveIncentiveData, + UserRewardInfo, +} from "./uiIncentiveDataProvider"; export { UiPoolDataProviderClient, ReservesData, diff --git a/src/contracts/acl_manage.ts b/src/contracts/acl_manage.ts index eb74a8d..64990ed 100644 --- a/src/contracts/acl_manage.ts +++ b/src/contracts/acl_manage.ts @@ -1,5 +1,5 @@ import { MoveFunctionId } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../clients/aptosProvider"; +import { AAVE_PROFILES, AptosProvider } from "../clients/aptosProvider"; export class AclManagerContract { // Resource Func Addr @@ -70,7 +70,7 @@ export class AclManagerContract { setRoleAdmin: MoveFunctionId; constructor(provider: AptosProvider) { - const AclManager = provider.getProfileAccountByName("AAVE_ACL_ADDRESS"); + const AclManager = provider.getProfileAccountByName(AAVE_PROFILES.AAVE_ACL); const AclManagerAccountAddress = AclManager.toString(); this.hasRoleFuncAddr = `${AclManagerAccountAddress}::acl_manage::has_role`; this.grantRoleFuncAddr = `${AclManagerAccountAddress}::acl_manage::grant_role`; diff --git a/src/contracts/bridge.ts b/src/contracts/bridge.ts index 176365a..a874222 100644 --- a/src/contracts/bridge.ts +++ b/src/contracts/bridge.ts @@ -1,5 +1,5 @@ import { MoveFunctionId } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../clients/aptosProvider"; +import { AAVE_PROFILES, AptosProvider } from "../clients/aptosProvider"; export class BridgeContract { // Resource Func Addr @@ -8,7 +8,9 @@ export class BridgeContract { BackUnbackedFuncAddr: MoveFunctionId; constructor(provider: AptosProvider) { - const BridgeManager = provider.getProfileAccountByName("AAVE_POOL_ADDRESS"); + const BridgeManager = provider.getProfileAccountByName( + AAVE_PROFILES.AAVE_POOL, + ); const BridgeManagerAccountAddress = BridgeManager.toString(); this.MintUnbackedFuncAddr = `${BridgeManagerAccountAddress}::bridge_logic::mint_unbacked`; this.BackUnbackedFuncAddr = `${BridgeManagerAccountAddress}::bridge_logic::back_unbacked`; diff --git a/src/contracts/flashloan.ts b/src/contracts/flashloan.ts index c6c2796..6f45b56 100644 --- a/src/contracts/flashloan.ts +++ b/src/contracts/flashloan.ts @@ -1,5 +1,5 @@ import { MoveFunctionId } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../clients/aptosProvider"; +import { AAVE_PROFILES, AptosProvider } from "../clients/aptosProvider"; export class FlashLoanContract { // Resource Func Addr @@ -12,8 +12,9 @@ export class FlashLoanContract { PayFlashLoanSimpleFuncAddr: MoveFunctionId; constructor(provider: AptosProvider) { - const FlashLoanManager = - provider.getProfileAccountByName("AAVE_POOL_ADDRESS"); + const FlashLoanManager = provider.getProfileAccountByName( + AAVE_PROFILES.AAVE_POOL, + ); const FlashLoanManagerAccountAddress = FlashLoanManager.toString(); this.FlashLoanFuncAddr = `${FlashLoanManagerAccountAddress}::flash_loan_logic::flashloan`; this.FlashLoanSimpleFuncAddr = `${FlashLoanManagerAccountAddress}::flash_loan_logic::flash_loan_simple`; diff --git a/src/contracts/oracle.ts b/src/contracts/oracle.ts index 6b3c101..3b70e30 100644 --- a/src/contracts/oracle.ts +++ b/src/contracts/oracle.ts @@ -1,5 +1,5 @@ import { MoveFunctionId } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../clients/aptosProvider"; +import { AAVE_PROFILES, AptosProvider } from "../clients/aptosProvider"; export class OracleContract { // Resource Func Addr @@ -17,7 +17,7 @@ export class OracleContract { constructor(provider: AptosProvider) { const OracleManager = provider.getProfileAccountByName( - "AAVE_MOCK_ORACLE_ADDRESS", + AAVE_PROFILES.AAVE_MOCK_ORACLE, ); const OracleManagerAccountAddress = OracleManager.toString(); this.GetAssetPriceFuncAddr = `${OracleManagerAccountAddress}::oracle::get_asset_price`; diff --git a/src/contracts/pool.ts b/src/contracts/pool.ts index 1f248ca..1e47e9f 100644 --- a/src/contracts/pool.ts +++ b/src/contracts/pool.ts @@ -1,5 +1,5 @@ import { MoveFunctionId } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../clients/aptosProvider"; +import { AAVE_PROFILES, AptosProvider } from "../clients/aptosProvider"; export class PoolContract { // Resource Func Addr @@ -247,7 +247,9 @@ export class PoolContract { SetPoolDataProviderFuncAddr: MoveFunctionId; constructor(provider: AptosProvider) { - const PoolManager = provider.getProfileAccountByName("AAVE_POOL_ADDRESS"); + const PoolManager = provider.getProfileAccountByName( + AAVE_PROFILES.AAVE_POOL, + ); const PoolManagerAccountAddress = PoolManager.toString(); /** diff --git a/src/contracts/supply_borrow.ts b/src/contracts/supply_borrow.ts index a4d2fbc..ed60a94 100644 --- a/src/contracts/supply_borrow.ts +++ b/src/contracts/supply_borrow.ts @@ -1,5 +1,5 @@ import { MoveFunctionId } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../clients/aptosProvider"; +import { AAVE_PROFILES, AptosProvider } from "../clients/aptosProvider"; export class SupplyBorrowContract { // Resource Func Addr @@ -32,8 +32,9 @@ export class SupplyBorrowContract { GetUserAccountDataFuncAddr: MoveFunctionId; constructor(provider: AptosProvider) { - const SupplyBorrowManager = - provider.getProfileAccountByName("AAVE_POOL_ADDRESS"); + const SupplyBorrowManager = provider.getProfileAccountByName( + AAVE_PROFILES.AAVE_POOL, + ); const SupplyBorrowManagerAccountAddress = SupplyBorrowManager.toString(); this.SupplyFuncAddr = `${SupplyBorrowManagerAccountAddress}::supply_logic::supply`; this.WithdrawFuncAddr = `${SupplyBorrowManagerAccountAddress}::supply_logic::withdraw`; diff --git a/src/contracts/tokens.ts b/src/contracts/tokens.ts index 978a93e..7b21a22 100644 --- a/src/contracts/tokens.ts +++ b/src/contracts/tokens.ts @@ -1,5 +1,5 @@ import { MoveFunctionId } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../clients/aptosProvider"; +import { AAVE_PROFILES, AptosProvider } from "../clients/aptosProvider"; export class TokensContract { // Resource Func Addr @@ -90,8 +90,9 @@ export class TokensContract { constructor(provider: AptosProvider) { // Underlying Token - const AaveTokensManager = - provider.getProfileAccountByName("AAVE_POOL_ADDRESS"); + const AaveTokensManager = provider.getProfileAccountByName( + AAVE_PROFILES.AAVE_POOL, + ); const AaveTokensManagerAccountAddress = AaveTokensManager.toString(); // Resource Func Addr // Underlying Token diff --git a/src/contracts/uiIncentiveDataProvider.ts b/src/contracts/uiIncentiveDataProvider.ts index 18f9cf6..547e62e 100644 --- a/src/contracts/uiIncentiveDataProvider.ts +++ b/src/contracts/uiIncentiveDataProvider.ts @@ -1,5 +1,5 @@ import { MoveFunctionId } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../clients/aptosProvider"; +import { AAVE_PROFILES, AptosProvider } from "../clients/aptosProvider"; export class UiIncentiveDataProviderContract { // Resource Func Addr @@ -21,8 +21,9 @@ export class UiIncentiveDataProviderContract { getUserReservesIncentivesData: MoveFunctionId; constructor(provider: AptosProvider) { - const PeripheryManager = - provider.getProfileAccountByName("AAVE_POOL_ADDRESS"); + const PeripheryManager = provider.getProfileAccountByName( + AAVE_PROFILES.AAVE_POOL, + ); const PeripheryManagerAccountAddress = PeripheryManager.toString(); this.uiIncentiveDataProviderV3DataAddress = `${PeripheryManagerAccountAddress}::ui_incentive_data_provider_v3::ui_incentive_data_provider_v3_data_address`; diff --git a/src/contracts/uiPoolDataProvider.ts b/src/contracts/uiPoolDataProvider.ts index f0bd79c..9a03863 100644 --- a/src/contracts/uiPoolDataProvider.ts +++ b/src/contracts/uiPoolDataProvider.ts @@ -1,5 +1,5 @@ import { MoveFunctionId } from "@aptos-labs/ts-sdk"; -import { AptosProvider } from "../clients/aptosProvider"; +import { AAVE_PROFILES, AptosProvider } from "../clients/aptosProvider"; export class UiPoolDataProviderContract { // Resource Func Addr @@ -21,8 +21,9 @@ export class UiPoolDataProviderContract { getUserReservesData: MoveFunctionId; constructor(provider: AptosProvider) { - const PeripheryManager = - provider.getProfileAccountByName("AAVE_POOL_ADDRESS"); + const PeripheryManager = provider.getProfileAccountByName( + AAVE_PROFILES.AAVE_POOL, + ); const PeripheryManagerAccountAddress = PeripheryManager.toString(); this.uiPoolDataProviderV32DataAddress = `${PeripheryManagerAccountAddress}::ui_pool_data_provider_v3::ui_pool_data_provider_v3_data_address`;