Skip to content

Commit

Permalink
feat: gas estimation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Makeev Ivan committed Sep 1, 2021
1 parent 05b2e04 commit 3c95fa9
Show file tree
Hide file tree
Showing 5 changed files with 659 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/api",
"version": "1.2.0",
"version": "1.3.0",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"scripts": {
Expand Down
50 changes: 49 additions & 1 deletion src/boosting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "ethers";
import BigNumber from "bignumber.js";
import { _getBalances, _prepareAddresses } from "./utils";
import {_getBalances, _prepareAddresses, hasAllowance} from "./utils";
import { _ensureAllowance, toBN, toStringFromBN } from './utils';
import { curve, ALIASES } from "./curve";
import { DictInterface } from "./interfaces";
Expand Down Expand Up @@ -75,6 +75,23 @@ export const getVeCrvPct = async (...addresses: string[] | string[][]): Promise<
return addresses.length === 1 ? result[addresses[0]] : result
}

export const createLockEstimateGas = async (amount: string, days: number): Promise<number> => {
const crvBalance = await getCrv() as string;

if (Number(crvBalance) < Number(amount)) {
throw Error(`Not enough . Actual: ${crvBalance}, required: ${amount}`);
}

if (!(await hasAllowance([ALIASES.crv], [amount], curve.signerAddress, ALIASES.voting_escrow))) {
throw Error("Token allowance is needed to estimate gas")
}

const _amount = ethers.utils.parseUnits(amount);
const unlockTime = Math.floor(Date.now() / 1000) + (days * 86400);

return (await curve.contracts[ALIASES.voting_escrow].contract.estimateGas.create_lock(_amount, unlockTime)).toNumber()
}

export const createLock = async (amount: string, days: number): Promise<string> => {
const _amount = ethers.utils.parseUnits(amount);
const unlockTime = Math.floor(Date.now() / 1000) + (days * 86400);
Expand All @@ -85,6 +102,23 @@ export const createLock = async (amount: string, days: number): Promise<string>
return (await contract.create_lock(_amount, unlockTime, { ...curve.options, gasLimit })).hash
}

export const increaseAmountEstimateGas = async (amount: string): Promise<number> => {
const crvBalance = await getCrv() as string;

if (Number(crvBalance) < Number(amount)) {
throw Error(`Not enough. Actual: ${crvBalance}, required: ${amount}`);
}

if (!(await hasAllowance([ALIASES.crv], [amount], curve.signerAddress, ALIASES.voting_escrow))) {
throw Error("Token allowance is needed to estimate gas")
}

const _amount = ethers.utils.parseUnits(amount);
const contract = curve.contracts[ALIASES.voting_escrow].contract;

return (await contract.estimateGas.increase_amount(_amount)).toNumber()
}

export const increaseAmount = async (amount: string): Promise<string> => {
const _amount = ethers.utils.parseUnits(amount);
await _ensureAllowance([ALIASES.crv], [_amount], ALIASES.voting_escrow);
Expand All @@ -94,6 +128,14 @@ export const increaseAmount = async (amount: string): Promise<string> => {
return (await contract.increase_amount(_amount, { ...curve.options, gasLimit })).hash
}

export const increaseUnlockTimeEstimateGas = async (days: number): Promise<number> => {
const { unlockTime } = await getLockedAmountAndUnlockTime() as { lockedAmount: string, unlockTime: number };
const newUnlockTime = Math.floor(unlockTime / 1000) + (days * 86400);
const contract = curve.contracts[ALIASES.voting_escrow].contract;

return (await contract.estimateGas.increase_unlock_time(newUnlockTime)).toNumber()
}

export const increaseUnlockTime = async (days: number): Promise<string> => {
const { unlockTime } = await getLockedAmountAndUnlockTime() as { lockedAmount: string, unlockTime: number };
const newUnlockTime = Math.floor(unlockTime / 1000) + (days * 86400);
Expand All @@ -103,6 +145,12 @@ export const increaseUnlockTime = async (days: number): Promise<string> => {
return (await contract.increase_unlock_time(newUnlockTime, { ...curve.options, gasLimit })).hash
}

export const withdrawLockedCrvEstimateGas = async (): Promise<number> => {
const contract = curve.contracts[ALIASES.voting_escrow].contract;

return (await contract.estimateGas.withdraw()).toNumber()
}

export const withdrawLockedCrv = async (): Promise<string> => {
const contract = curve.contracts[ALIASES.voting_escrow].contract;

Expand Down
41 changes: 38 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import { ethers } from "ethers";
import { Networkish } from "@ethersproject/networks";
import { Pool, getBestPoolAndOutput, exchangeExpected, exchange, crossAssetExchangeAvailable, crossAssetExchangeOutputAndSlippage, crossAssetExchangeExpected, crossAssetExchange } from "./pools";
import {
Pool,
getBestPoolAndOutput,
exchangeExpected,
exchangeEstimateGas,
exchange,
crossAssetExchangeAvailable,
crossAssetExchangeOutputAndSlippage,
crossAssetExchangeExpected,
crossAssetExchangeEstimateGas,
crossAssetExchange,
} from "./pools";
import { curve as _curve } from "./curve";
import { getCrv, getLockedAmountAndUnlockTime, getVeCrv, getVeCrvPct, createLock, increaseAmount, increaseUnlockTime, withdrawLockedCrv } from "./boosting";
import { getBalances, getAllowance, hasAllowance, ensureAllowance } from "./utils";
import {
getCrv,
getLockedAmountAndUnlockTime,
getVeCrv,
getVeCrvPct,
createLockEstimateGas,
createLock,
increaseAmountEstimateGas,
increaseAmount,
increaseUnlockTimeEstimateGas,
increaseUnlockTime,
withdrawLockedCrvEstimateGas,
withdrawLockedCrv,
} from "./boosting";
import { getBalances, getAllowance, hasAllowance, ensureAllowanceEstimateGas, ensureAllowance } from "./utils";

async function init (
providerType: 'JsonRpc' | 'Web3' | 'Infura',
Expand Down Expand Up @@ -35,6 +59,11 @@ const curve = {
crossAssetExchangeOutputAndSlippage,
crossAssetExchangeExpected,
crossAssetExchange,
estimateGas: {
ensureAllowance: ensureAllowanceEstimateGas,
exchange: exchangeEstimateGas,
crossAssetExchange: crossAssetExchangeEstimateGas,
},
boosting: {
getCrv,
getLockedAmountAndUnlockTime,
Expand All @@ -44,6 +73,12 @@ const curve = {
increaseAmount,
increaseUnlockTime,
withdrawLockedCrv,
estimateGas: {
createLock: createLockEstimateGas,
increaseAmount: increaseAmountEstimateGas,
increaseUnlockTime: increaseUnlockTimeEstimateGas,
withdrawLockedCrv: withdrawLockedCrvEstimateGas,
},
},
}

Expand Down
Loading

0 comments on commit 3c95fa9

Please sign in to comment.