# @apyrase/client

> Apy-on-Aptos client

Latest version **0.0.3** (published 2023-08-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @apyrase/client
pnpm add @apyrase/client
yarn add @apyrase/client
bun add @apyrase/client
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2023-08-15 |
| First published | 2023-08-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 2 |
| Unpacked size | 149.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dmitrii Dulesov |
| Maintainers | ddulesov |
| Keywords | Aptos, Apy, Lending, Borrow |

## Links

- npm: https://www.npmjs.com/package/@apyrase/client
- npm.io page: https://npm.io/package/@apyrase/client

## Dependencies (2)

- [aptos](https://npm.io/package/aptos.md) 1.10.0
- [big-integer](https://npm.io/package/big-integer.md) ^1.6.51

## Recent versions

- 0.0.3 (latest) — 2023-08-15

## README

# 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
```bash
$ pnpm install 
```

Once the package is installed, you can import the library using import or require approach:

```js
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](../deploy/README.md)

```
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')
```
## Getting APY market states

---
_Source: https://npm.io/package/@apyrase/client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
