Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #12

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Huma Monorepo CI
on:
pull_request:
types: [opened, synchronize]
branches: ["develop", "main"]

jobs:
ci:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18.13.0
cache: "yarn"

- name: Cache Yarn dependencies
uses: actions/cache@v3
with:
path: ~/.cache/yarn
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

- name: Install dependencies
run: yarn install

- name: Lint all
run: yarn lint:all

- name: Build all
run: yarn build:all

- name: Unit tests
run: yarn test:all

- name: Generate docs
run: yarn docs:all

- name: Check for uncommitted changes
run: |
# Check for changes after build/docs
git diff
# git diff --exit-code --quiet || { git status --porcelain; exit 1; }
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"docs:sdk": "yarn workspace @huma-finance/soroban-sdk generateDocs",
"lint:all": "yarn lint:sdk",
"lint:sdk": "yarn workspace @huma-finance/soroban-sdk lint",
"test:all": "yarn test:sdk",
"test:sdk": "yarn workspace @huma-finance/soroban-sdk test",
"prepare": "husky install",
"binding": "ts-node scripts/bindings.ts"
},
Expand Down
158 changes: 158 additions & 0 deletions packages/sdk/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Getting Started

In this guide we'll take a look at using Huma's Soroban SDK to call functions on our pool, and more.

## Installation

Huma's Soroban SDK can be installed using npm or yarn.

```
npm install @huma-finance/soroban-sdk
yarn add @huma-finance/soroban-sdk
```

## Approve lender

```javascript
import {
getTrancheVaultClient,
POOL_NAME,
StellarNetwork,
StellarWallet,
} from '@huma-finance/soroban-sdk'

const poolName = POOL_NAME.Arf
const network = StellarNetwork.testnet
const poolOperator = new StellarWallet(process.env.POOL_OPERATOR_SECRET_KEY)
const lender = new StellarWallet(process.env.LENDER_SECRET_KEY)

const trancheVaultClient = getTrancheVaultClient(
poolName,
network,
poolOperator,
'junior',
)

if (trancheVaultClient) {
const tx = await trancheVaultClient.add_approved_lender(
{
caller: poolOperator.userInfo.publicKey,
lender: lender.userInfo.publicKey,
},
{
timeoutInSeconds: 30,
},
)
await tx.signAndSend()
}
```

## Deposit

```javascript
import {
getTrancheVaultClient,
POOL_NAME,
StellarNetwork,
StellarWallet,
} from '@huma-finance/soroban-sdk'

const poolName = POOL_NAME.Arf
const network = StellarNetwork.testnet
const lender = new StellarWallet(process.env.LENDER_SECRET_KEY)

const trancheVaultClient = getTrancheVaultClient(
poolName,
network,
lender,
'junior',
)

if (trancheVaultClient) {
const depositAmount = 100000000n
const tx = await trancheVaultClient.deposit(
{
lender: lender.userInfo.publicKey,
assets: depositAmount,
},
{
timeoutInSeconds: 30,
},
)
await tx.signAndSend()
}
```

## Approve borrower

```javascript
import {
getCreditManagerClient,
POOL_NAME,
StellarNetwork,
StellarWallet,
} from '@huma-finance/soroban-sdk'

const poolName = POOL_NAME.Arf
const network = StellarNetwork.testnet
const ea = new StellarWallet(process.env.EA_SECRET_KEY)
const borrower = new StellarWallet(process.env.BORROWER_SECRET_KEY)

const creditManagerClient = getCreditManagerClient(poolName, network, ea)

if (creditManagerClient) {
const tx = await creditManagerClient.approve_borrower(
{
borrower: borrower.userInfo.publicKey,
credit_limit: 1000_0000000n,
num_periods: 5,
yield_bps: 1200,
committed_amount: 0n,
designated_start_date: 0n,
revolving: true,
},
{
timeoutInSeconds: 30,
},
)
await tx.signAndSend()
}
```

## Borrower drawdown and makePayment

```javascript
import {
drawdown,
makePayment,
POOL_NAME,
StellarNetwork,
StellarWallet,
} from '@huma-finance/soroban-sdk'

const poolName = POOL_NAME.Arf
const network = StellarNetwork.testnet
const borrower = new StellarWallet(process.env.BORROWER_SECRET_KEY)

const borrowAmount = 100_0000000n
const drawdownResult = await drawdown(poolName, network, borrower, borrowAmount)
console.log(
`Drawdown success. Tx hash: ${drawdownResult.sendTransactionResponse?.hash}`,
)

const paymentAmount = 100_0000000n
const makePaymentResult = await makePayment(
poolName,
network,
borrower,
paymentAmount,
true,
)
console.log(
`Payment success. Tx hash: ${makePaymentResult.sendTransactionResponse?.hash}`,
)
```

## Next steps

If you want to learn more about the different functions and helpers available in the Huma SDK, please check out our [API documentation](API.md).
2 changes: 1 addition & 1 deletion packages/sdk/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
transform: {
'^.+\\.ts?$': ['ts-jest', { tsconfig: 'tsconfig.test.json' }],
'^.+\\.ts?$': ['ts-jest'],
'^.+\\.(js|jsx)$': 'babel-jest',
},
coverageReporters: ['json-summary', 'text', 'lcov'],
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './CreditContractHelper'
export * from './Sep41ContractHelper'
35 changes: 35 additions & 0 deletions packages/sdk/src/utils/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Client as CreditStorageClient } from '@huma-finance/soroban-credit-storage'
import { Client as CreditManagerClient } from '@huma-finance/soroban-credit-manager'
import { Client as PoolClient } from '@huma-finance/soroban-pool'
import { Client as PoolCreditClient } from '@huma-finance/soroban-pool-credit'
import { Client as PoolStorageClient } from '@huma-finance/soroban-pool-storage'
import { Client as TrancheVaultClient } from '@huma-finance/soroban-tranche-vault'
import { Client as Sep41Client } from '@huma-finance/soroban-sep41'

import { StellarWallet } from '../services/StellarWallet'
Expand Down Expand Up @@ -87,6 +89,39 @@ export const getCreditStorageClient = (
})
}

export const getCreditManagerClient = (
poolName: POOL_NAME,
network: StellarNetwork,
wallet: StellarWallet,
) => {
const poolMetadata = findPoolMetadata(network, poolName)
if (!poolMetadata) {
return undefined
}

return new CreditManagerClient({
contractId: poolMetadata.contracts.creditManager,
...getCommonProps(network, wallet),
})
}

export const getTrancheVaultClient = (
poolName: POOL_NAME,
network: StellarNetwork,
wallet: StellarWallet,
tranche: 'senior' | 'junior',
) => {
const poolMetadata = findPoolMetadata(network, poolName)
if (!poolMetadata) {
return undefined
}

return new TrancheVaultClient({
contractId: poolMetadata.contracts[`${tranche}Tranche`],
...getCommonProps(network, wallet),
})
}

export const getUnderlyingTokenClient = (
tokenAddress: string,
network: StellarNetwork,
Expand Down
Loading
Loading