Apy on Aptos nodejs and web client
The Apy on Aptos client provides a convenient way to interact with the Apy protocol on Aptos blockchain using JavaScript. It offers a set of utility functions, classes, and types to simplify the integration process and enhance developer productivity.
This library provides a clean wrapper around all the methods exposed by a Apy-on-Aptos contract, as well as the APY indexer REST API. The library is written in TypeScript and defines all the types exposed by underlying services.
Main features
- Thin wrapper around the standard Aptos client.
- Fully written in TypeScript, with strict types for security and safety.
- Integration with popular Aptos wallets, such as Pontem.
- Complete functionality for making calls to the contract and effective retriving market state from the Apy indexer.
Installation
For use in Node.js or a web application
$ pnpm install
Once the package is installed, you can import the library using import or require approach:
import { ApyClient, AccountProvider } from "@apy/client"
import { Apy } from "@apy/client/markets"
Example
To run the example it needs the Apy-on-Aptos to be published and lend-borrow markets to be configured.
For details refer to Apy-on-Aptos testnet deployment instruction
import { ApyClient, AccountProvider } from "@apy/client"
import { AptosClient, AptosAccount, HexString } from "aptos"
import { Apy } from "@apy/client/markets"
// coin types
const USDC = process.env.USDC!
const BTC = process.env.BTC!
const ETH = process.env.ETH!
const APY = new HexString(process.env.APY!)
const NODE_URL = process.env.APTOS_NODE_URL!
// create apy client powered by simple account provider. In real world applications use Pontem wallet.
export function getApyClient(signer: AptosAccount, client: AptosClient, apy: HexString): ApyClient {
return new ApyClient(new AccountProvider(client, signer), apy);
}
export function isSet<T>(val?: T): T {
if (val === undefined) {
throw new Error("value is unknown")
}
return val
}
// It supposed, we have configured BTC, USDC and ETH markets.
const markets = new Apy(APY)
await markets.updateMarkets(new AptosClient(NODE_URL))
const borrower = new AptosAccount();
const lender = new AptosAccount();
// ... endow borrower with USDC and lender with BTC
const client = new AptosClient(NODE_URL);
let borrowerClient = getApyClient(borrower, client, APY)
let lenderClient = getApyClient(lender, client, APY)
// fetch markets into application cache
const apy = new Apy(APY)
await apy.updateMarkets(client)
const usdcMarket = isSet(apy.getMarketByCoinPeriod(USDC, "Hour"))
const ethMarket = isSet(apy.getMarketByCoinPeriod(ETH, "Hour"))
const btcMarket = isSet(apy.getMarketByCoinPeriod(BTC, "Hour"))
// deposit USDC as a collateral
await borrowerClient.deposit(usdcMarket, '100_000')
// deposit BTC and place a lend order
await lenderClient.deposit(btcMarket, '1.0')
await lenderClient.lend(btcMarket, '1.0', 1) // 1%
// cancel previous order and place new one at 2%
await lenderClient.lend_cancel(btcMarket)
await lenderClient.lend(btcMarket, '1.0', 2.0) // 2%
// create borrow request on the 0.01 BTC
await borrowerClient.borrow(btcMarket, '0.01', 0) // 0.01 BTC at market rate(2%)
// withdraw borrowed BTC
await borrowerClient.withdraw(btcMarket, '0.01')