Skip to content

Commit

Permalink
feat: approve methods for straight exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
Makeev Ivan committed Oct 26, 2021
1 parent 72c5aa0 commit 41f5f58
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ await pool.exchangeWrappedApprove("0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490",
```
**Note.** Removing wrapped does not require approve.
### Exchange
```ts
await curve.exchangeisApproved("DAI", "0x99d8a9c45b2eca8864373a26d1459e3dff1e17f3", "1000"); // DAI -> MIM
await curve.exchangeApprove("DAI", "0x99d8a9c45b2eca8864373a26d1459e3dff1e17f3", "1000"); // DAI -> MIM
```
### Boosting
```ts
await curve.boosting.isApproved('1000')
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
Pool,
getBestPoolAndOutput,
exchangeExpected,
exchangeIsApproved,
exchangeApproveEstimateGas,
exchangeApprove,
exchangeEstimateGas,
exchange,
crossAssetExchangeAvailable,
Expand Down Expand Up @@ -56,6 +59,8 @@ const curve = {
hasAllowance,
ensureAllowance,
getBestPoolAndOutput,
exchangeIsApproved,
exchangeApprove,
exchangeExpected,
exchange,
crossAssetExchangeAvailable,
Expand All @@ -64,6 +69,7 @@ const curve = {
crossAssetExchange,
estimateGas: {
ensureAllowance: ensureAllowanceEstimateGas,
exchangeApprove: exchangeApproveEstimateGas,
exchange: exchangeEstimateGas,
crossAssetExchange: crossAssetExchangeEstimateGas,
},
Expand Down
52 changes: 52 additions & 0 deletions src/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,25 @@ export const _getBestPoolAndOutput = async (
return { poolAddress, output }
}

const _getExchangeData = async (inputCoin: string, outputCoin: string, amount: string): Promise<[string, number, number, boolean]> => {
const [inputCoinAddress, outputCoinAddress] = _getCoinAddresses(inputCoin, outputCoin);
const [inputCoinDecimals] = _getCoinDecimals(inputCoinAddress);
const addressProviderContract = curve.contracts[ALIASES.address_provider].contract;
const registryAddress = await addressProviderContract.get_registry();
const registryContract = new ethers.Contract(registryAddress, registryABI, curve.signer);

const { poolAddress } = await _getBestPoolAndOutput(inputCoinAddress, outputCoinAddress, inputCoinDecimals, amount);
if (poolAddress === "0x0000000000000000000000000000000000000000") {
throw new Error("This pair can't be exchanged");
}
const poolName = getPoolNameBySwapAddress(poolAddress);
const [_i, _j, isUnderlying] = await registryContract.get_coin_indices(poolAddress, inputCoinAddress, outputCoinAddress);
const i = Number(_i.toString());
const j = Number(_j.toString());

return [poolName, i, j, isUnderlying]
}

export const getBestPoolAndOutput = async (inputCoin: string, outputCoin: string, amount: string): Promise<{ poolAddress: string, output: string }> => {
const [inputCoinAddress, outputCoinAddress] = _getCoinAddresses(inputCoin, outputCoin);
const [inputCoinDecimals, outputCoinDecimals] = _getCoinDecimals(inputCoinAddress, outputCoinAddress);
Expand All @@ -1698,6 +1717,39 @@ export const exchangeExpected = async (inputCoin: string, outputCoin: string, am
return (await getBestPoolAndOutput(inputCoin, outputCoin, amount))['output'];
}

export const exchangeIsApproved = async (inputCoin: string, outputCoin: string, amount: string): Promise<boolean> => {
const [poolName, i, , isUnderlying] = await _getExchangeData(inputCoin, outputCoin, amount);
const pool = new Pool(poolName);

if (isUnderlying) {
return await pool.exchangeIsApproved(i, amount);
} else {
return await pool.exchangeWrappedIsApproved(i, amount);
}
}

export const exchangeApproveEstimateGas = async (inputCoin: string, outputCoin: string, amount: string): Promise<number> => {
const [poolName, i, , isUnderlying] = await _getExchangeData(inputCoin, outputCoin, amount);
const pool = new Pool(poolName);

if (isUnderlying) {
return await pool.estimateGas.exchangeApprove(i, amount);
} else {
return await pool.estimateGas.exchangeWrappedApprove(i, amount);
}
}

export const exchangeApprove = async (inputCoin: string, outputCoin: string, amount: string): Promise<string[]> => {
const [poolName, i, , isUnderlying] = await _getExchangeData(inputCoin, outputCoin, amount);
const pool = new Pool(poolName);

if (isUnderlying) {
return await pool.exchangeApprove(i, amount);
} else {
return await pool.exchangeWrappedApprove(i, amount);
}
}

export const exchangeEstimateGas = async (inputCoin: string, outputCoin: string, amount: string, maxSlippage = 0.01): Promise<number> => {
const [inputCoinAddress, outputCoinAddress] = _getCoinAddresses(inputCoin, outputCoin);
const [inputCoinDecimals] = _getCoinDecimals(inputCoinAddress);
Expand Down

0 comments on commit 41f5f58

Please sign in to comment.