diff --git a/package.json b/package.json index 10d74f1..18b470c 100644 --- a/package.json +++ b/package.json @@ -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 ", "repository": "https://github.com/CrocSwap/sdk.git", diff --git a/src/vaults/tempest.ts b/src/vaults/tempest.ts index a1ea1c4..bfa82f5 100644 --- a/src/vaults/tempest.ts +++ b/src/vaults/tempest.ts @@ -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' @@ -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 } @@ -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) } } @@ -40,48 +42,51 @@ 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 { 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 { - 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 { 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 { 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 { - 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 { - return (await this.token1).approveAddr(await this.vaultAddr, approveQty); + return this.token1.approveAddr(this.vaultAddr, approveQty); } @@ -89,7 +94,8 @@ export class TempestVault { private token1: CrocTokenView; private vaultToken: CrocTokenView; private strategy: TempestStrategy - private vault: Promise; + private vaultWrite: Promise; + private vaultRead: Promise; private minDepositCache: Promise | undefined private context: Promise }