Skip to content

Commit

Permalink
Merge pull request #47 from BuiltByMom/feat/integrate-helper-contract
Browse files Browse the repository at this point in the history
feat: integrate helper contract and fix tests
  • Loading branch information
imbaniac authored Jan 25, 2024
2 parents 10be8aa + dd480d1 commit c6d861e
Show file tree
Hide file tree
Showing 21 changed files with 742 additions and 118 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ yarn-error.log*
_archivepackage-lock.json

examples/*.json

.env
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"editor.wordWrap": "off",
"editor.autoIndent": "keep",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"[jsonc]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3.7'

services:
ajna-testnet:
image: ghcr.io/ajna-finance/ajna-testnet:rc9
image: ghcr.io/builtbymom/ajna-testnet:rc10
ports:
- 8555:8555
container_name: ajna-testnet-sdk
7 changes: 6 additions & 1 deletion examples/lend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ async function addLiquidity(amount: BigNumber, price: BigNumber) {
throw new SdkError('Please provide a valid price');

const bucket = await pool.getBucketByPrice(price);
let tx = await pool.quoteApprove(signerLender, amount);

let tx = await pool.quoteApproveHelper(signerLender, amount);
await tx.verifyAndSubmit();

tx = await pool.approveLenderHelperLPTransferor(signerLender);
await tx.verifyAndSubmit();

tx = await bucket.addQuoteToken(signerLender, amount);
await tx.verifyAndSubmit();
console.log('Added', fromWad(amount), 'liquidity to bucket', bucket.index);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ajna-finance/sdk",
"description": "A typescript SDK that can be used to create Dapps in Ajna ecosystem.",
"version": "0.3.5",
"version": "0.4.0",
"repository": {
"type": "git",
"url": "https://github.com/ajna-finance/sdk.git"
Expand Down
176 changes: 176 additions & 0 deletions src/abis/AjnaLenderHelper.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
[
{
"inputs": [],
"name": "BucketIndexOutOfBounds",
"type": "error"
},
{
"inputs": [],
"name": "InsufficientLP",
"type": "error"
},
{
"inputs": [
{
"internalType": "int256",
"name": "x",
"type": "int256"
}
],
"name": "PRBMathSD59x18__Exp2InputTooBig",
"type": "error"
},
{
"inputs": [
{
"internalType": "int256",
"name": "x",
"type": "int256"
}
],
"name": "PRBMathSD59x18__FromIntOverflow",
"type": "error"
},
{
"inputs": [
{
"internalType": "int256",
"name": "x",
"type": "int256"
}
],
"name": "PRBMathSD59x18__FromIntUnderflow",
"type": "error"
},
{
"inputs": [
{
"internalType": "int256",
"name": "x",
"type": "int256"
}
],
"name": "PRBMathSD59x18__LogInputTooSmall",
"type": "error"
},
{
"inputs": [],
"name": "PRBMathSD59x18__MulInputTooSmall",
"type": "error"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "rAbs",
"type": "uint256"
}
],
"name": "PRBMathSD59x18__MulOverflow",
"type": "error"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "prod1",
"type": "uint256"
}
],
"name": "PRBMath__MulDivFixedPointOverflow",
"type": "error"
},
{
"inputs": [],
"name": "RoundedAmountExceededRequestedMaximum",
"type": "error"
},
{
"inputs": [
{
"internalType": "address",
"name": "pool_",
"type": "address"
},
{
"internalType": "uint256",
"name": "maxAmount_",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "index_",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "expiry_",
"type": "uint256"
}
],
"name": "addQuoteToken",
"outputs": [
{
"internalType": "uint256",
"name": "bucketLP_",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "addedAmount_",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "pool_",
"type": "address"
},
{
"internalType": "uint256",
"name": "maxAmount_",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "fromIndex_",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "toIndex_",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "expiry_",
"type": "uint256"
}
],
"name": "moveQuoteToken",
"outputs": [
{
"internalType": "uint256",
"name": "fromBucketRedeemedLP_",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "toBucketAwardedLP_",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "movedAmount_",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
42 changes: 8 additions & 34 deletions src/classes/Bucket.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { BigNumber, Contract, Signer, constants } from 'ethers';
import { MIN_BUCKET_LP, MAX_FENWICK_INDEX } from '../constants';
import { MAX_FENWICK_INDEX } from '../constants';
import { multicall } from '../contracts/common';
import {
addQuoteToken,
lenderInfo,
lenderKick,
moveQuoteToken,
removeQuoteToken,
} from '../contracts/pool';
import { lenderInfo, lenderKick, removeQuoteToken } from '../contracts/pool';
import {
bucketInfo,
getPoolInfoUtilsContract,
lpToCollateral,
lpToQuoteTokens,
} from '../contracts/pool-info-utils';
import { Address, CallData, PoolInfoUtils, SdkError, SignerOrProvider } from '../types';
import { Address, CallData, PoolInfoUtils, SignerOrProvider } from '../types';
import { fromWad, toWad } from '../utils/numeric';
import { indexToPrice } from '../utils/pricing';
import { getExpiry } from '../utils/time';
import { Pool } from './Pool';
import { addQuoteToken, moveQuoteToken } from '../contracts/lender-helper';

export interface BucketStatus {
/* amount of quote token, including accrued interest, owed to the bucket */
Expand Down Expand Up @@ -104,22 +99,6 @@ export class Bucket {
};
}

/**
* Validates bucket LP balance to be greater than MIN_BUCKET_LP constant
* @returns true or throws SdkError
*/
validateLPBalance = async (index?: number) => {
const bucketStatus = await this.getStatus(index);

if (!bucketStatus.bucketLP.isZero() && bucketStatus.bucketLP.lt(MIN_BUCKET_LP)) {
throw new SdkError(
'You can’t deposit in this price bucket right now. Please try another one.'
);
}

return true;
};

/**
* Deposits quote token into the bucket.
* @param signer lender
Expand All @@ -128,12 +107,9 @@ export class Bucket {
* @returns promise to transaction
*/
async addQuoteToken(signer: Signer, amount: BigNumber, ttlSeconds?: number) {
const contractPoolWithSigner = this.poolContract.connect(signer);

await this.validateLPBalance();

return addQuoteToken(
contractPoolWithSigner,
signer,
this.pool.poolAddress,
amount,
this.index,
await getExpiry(this.provider, ttlSeconds)
Expand All @@ -154,11 +130,9 @@ export class Bucket {
maxAmountToMove = constants.MaxUint256,
ttlSeconds?: number
) {
const contractPoolWithSigner = this.poolContract.connect(signer);
await this.validateLPBalance(toIndex);

return moveQuoteToken(
contractPoolWithSigner,
signer,
this.pool.poolAddress,
maxAmountToMove,
this.index,
toIndex,
Expand Down
9 changes: 7 additions & 2 deletions src/classes/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Config {
static ajnaToken: Address;
static grantFund: Address;
static burnWrapper: Address;
static lenderHelper: Address;

/**
* Allows consumer to configure with their own addresses.
Expand All @@ -20,6 +21,7 @@ class Config {
* @param ajnaToken address of the AJNA token contract
* @param grantFund address of the ecosystem coordination contract
* @param burnWrapper address of the contract used to wrap AJNA for transferring across an L2 bridge
* @param lenderHelper address of the contract used as a helper to add and move liquidity across buckets
*/
constructor(
erc20PoolFactory: Address,
Expand All @@ -28,7 +30,8 @@ class Config {
positionManager: Address,
ajnaToken: Address,
grantFund: Address,
burnWrapper: Address
burnWrapper: Address,
lenderHelper: Address
) {
Config.erc20PoolFactory = erc20PoolFactory;
Config.erc721PoolFactory = erc721PoolFactory;
Expand All @@ -37,6 +40,7 @@ class Config {
Config.ajnaToken = ajnaToken;
Config.grantFund = grantFund;
Config.burnWrapper = burnWrapper;
Config.lenderHelper = lenderHelper;
}

/**
Expand All @@ -50,7 +54,8 @@ class Config {
process.env.AJNA_POSITION_MANAGER || '',
process.env.AJNA_TOKEN_ADDRESS || '',
process.env.AJNA_GRANT_FUND || '',
process.env.AJNA_BURN_WRAPPER || ''
process.env.AJNA_BURN_WRAPPER || '',
process.env.AJNA_LENDER_HELPER || ''
);
}
}
Expand Down
Loading

0 comments on commit c6d861e

Please sign in to comment.