Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crocswap-libs/sdk",
"version": "2.0.13",
"version": "2.0.14",
"description": "🛠🐊🛠 An SDK for building applications on top of CrocSwap",
"author": "Ben Wolski <ben@crocodilelabs.io>",
"repository": "https://github.com/CrocSwap/sdk.git",
Expand Down
32 changes: 19 additions & 13 deletions src/vaults/tempest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Contract, Signer, TransactionResponse, Typed } from "ethers";
import { TEMPEST_VAULT_ABI } from "../abis/external/TempestVaultAbi";
import { CrocContext } from "../context";
import { CrocContext, ensureChain } from "../context";
import { CrocTokenView, TokenQty } from "../tokens";

export type TempestStrategy = 'rswEth' | 'symetricAmbient'
Expand All @@ -12,7 +12,8 @@ export class TempestVault {
this.token1 = token1
this.vaultToken = vaultToken
this.strategy = strategy
this.vault = context.then(c => new Contract(this.vaultAddr, TEMPEST_VAULT_ABI, c.actor));
this.vaultWrite = context.then(c => new Contract(this.vaultAddr, TEMPEST_VAULT_ABI, c.actor));
this.vaultRead = context.then(c => new Contract(this.vaultAddr, TEMPEST_VAULT_ABI, c.provider));
this.context = context
}

Expand All @@ -25,11 +26,12 @@ export class TempestVault {
if (this.token1.isNativeEth) {
txArgs = { value: await weiQty };
}
await ensureChain(await this.context)
switch (this.strategy) {
case 'symetricAmbient':
return (await this.vault).deposit(await weiQty, owner, Typed.bool(true), txArgs)
return (await this.vaultWrite).deposit(await weiQty, owner, Typed.bool(true), txArgs)
case 'rswEth':
return (await this.vault).deposit(await weiQty, owner, Typed.bytes('0x'), txArgs)
return (await this.vaultWrite).deposit(await weiQty, owner, Typed.bytes('0x'), txArgs)
}
}

Expand All @@ -40,56 +42,60 @@ export class TempestVault {
let owner = ((await this.context).actor as Signer).getAddress()
let weiQty = this.vaultToken.normQty(vaultQty);
let minWeiQty = this.token1.normQty(minToken1Qty);
await ensureChain(await this.context)
switch (this.strategy) {
case 'symetricAmbient':
return (await this.vault).redeem(await weiQty, owner, owner, Typed.uint256(await minWeiQty), Typed.bool(true))
return (await this.vaultWrite).redeem(await weiQty, owner, owner, Typed.uint256(await minWeiQty), Typed.bool(true))
case 'rswEth':
return (await this.vault).redeem(await weiQty, owner, owner, Typed.bytes('0x'))
return (await this.vaultWrite).redeem(await weiQty, owner, owner, Typed.bytes('0x'))
}
}

/* @notice Retrieves the min deposit quantity in token1 for the Tempest vault */
async minDeposit(): Promise<bigint> {
if (!this.minDepositCache) {
this.minDepositCache = (await this.vault).minimumDeposit();
this.minDepositCache = (await this.vaultRead).minimumDeposit();
}
return this.minDepositCache
}

/* @notice Queries the vault token balance of a wallet */
async balanceVault (wallet: string): Promise<bigint> {
return (await this.vaultToken).wallet(wallet)
return this.vaultToken.wallet(wallet)
}

/* @notice Queries the implied token1 balance based on the share to asset conversion. */
async balanceToken1 (wallet: string): Promise<bigint> {
let balance = await this.balanceVault(wallet);
return (await this.vault).convertToAssets(balance)
if (balance === BigInt(0))
return BigInt(0)
return (await this.vaultRead).convertToAssets(balance)
}

/* @notice Returns the conversion rate between vault tokens and token1 collateral. */
async queryConversionRate(): Promise<number> {
let denom = 1000000
let numer = await (await this.vault).convertToShares(denom)
let numer = await (await this.vaultRead).convertToShares(denom)
return denom / Number(numer)
}

/* @notice Checks a wallet's token approval for the vault's token1. */
async allowance(wallet: string): Promise<bigint> {
return (await this.token1).allowance(wallet, await this.vaultAddr);
return this.token1.allowance(wallet, this.vaultAddr);
}

/* @notice Sends transaction to approve token1 on the vault contract */
async approve (approveQty?: TokenQty): Promise<TransactionResponse | undefined> {
return (await this.token1).approveAddr(await this.vaultAddr, approveQty);
return this.token1.approveAddr(this.vaultAddr, approveQty);
}


private vaultAddr: string;
private token1: CrocTokenView;
private vaultToken: CrocTokenView;
private strategy: TempestStrategy
private vault: Promise<Contract>;
private vaultWrite: Promise<Contract>;
private vaultRead: Promise<Contract>;
private minDepositCache: Promise<bigint> | undefined
private context: Promise<CrocContext>
}