From 943234dfefa3fb1fe3ffba5ecac7e2eeb82b979e Mon Sep 17 00:00:00 2001 From: jonny rhea Date: Thu, 10 Jun 2021 16:29:58 -0500 Subject: [PATCH] mods for swap example --- examples/swap.ts | 82 ++++++++++++++++++++++++++++++ src/helpers/getElementAddresses.ts | 33 ++++++++++++ src/helpers/getPoolId.ts | 5 +- src/swap.ts | 12 ++--- 4 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 examples/swap.ts diff --git a/examples/swap.ts b/examples/swap.ts new file mode 100644 index 0000000..50593d2 --- /dev/null +++ b/examples/swap.ts @@ -0,0 +1,82 @@ +/* + * Copyright 2021 Element Finance, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ethers } from "hardhat"; +import { + getElementDeploymentAddresses, + getElementTermFactoryAddresses, + getElementTermAddresses, + getElementPtPoolAddresses, + getPoolIdByTermAddress, + PoolType, +} from "../src/helpers/getElementAddresses"; +import { swap, SingleSwap, SwapKind } from "../src/swap"; +import { getTerms } from "../src/helpers/getTerms"; +import { + getTermTokenSymbols, + TermTokenSymbolsResult, +} from "../src/helpers/getTermTokenSymbols"; +import { DeploymentAddresses } from "../typechain/DeploymentAddresses"; +import { BigNumber } from "ethers"; + +async function main() { + const [signer] = await ethers.getSigners(); + const sender = signer.address; + const recipient = signer.address; + // get the official list of Element deployed addresses. + const deploymentAddresses: DeploymentAddresses = ( + await getElementDeploymentAddresses( + "https://raw.githubusercontent.com/element-fi/elf-deploy/main/addresses/goerli.json" + ) + ); + // get all official element term addresses + const elementTermAddresses = getElementTermAddresses(deploymentAddresses); + // find the term that matches the token symbol + const termAddress = elementTermAddresses.find(async (termAddress) => { + // get the symbols of a particular term address + const termTokenSymbols: TermTokenSymbolsResult = await getTermTokenSymbols( + termAddress, + signer + ); + termTokenSymbols.principalTokenSymbol == "eP:eyUSDC:06-AUG-21-GMT"; + }); + const poolId = await getPoolIdByTermAddress( + termAddress, + deploymentAddresses, + PoolType.PT + ); + console.log("Pool ID: " + poolId); + const tokenInAddress = deploymentAddresses.tokens.usdc; + const tokenOutAddress = termAddress; + const balancerVaultAddress = deploymentAddresses.balancerVault; + const amount = BigNumber.from("1000000"); // 1 USDC + const kind = SwapKind.GIVEN_IN; + const limit = BigNumber.from("990000"); + const result = await swap( + signer, + sender, + recipient, + poolId, + tokenInAddress, + tokenOutAddress, + balancerVaultAddress, + amount, + kind, + limit + ); +} + +main(); diff --git a/src/helpers/getElementAddresses.ts b/src/helpers/getElementAddresses.ts index 406f588..eadb6ca 100644 --- a/src/helpers/getElementAddresses.ts +++ b/src/helpers/getElementAddresses.ts @@ -16,6 +16,11 @@ import * as https from "https"; import { DeploymentAddresses } from "../../typechain/DeploymentAddresses"; +export enum PoolType { + PT, + YT, +} + /** * Get the contract addresses deployed by Element * @param url The url of the json changelog file @@ -134,3 +139,31 @@ export function getElementYtPoolAddresses( } return pools; } + +/** + * Returns the PoolId from the DeploymentAddresses that matches a termAddress + * @param termAddress termAddress to filter on + * @param deploymentAddresses The DeploymentAddresses object + * @param PoolType Either PT or YT + * @returns a promise for a poolId + */ +export async function getPoolIdByTermAddress( + termAddress: string, + deploymentAddresses: DeploymentAddresses, + poolType: PoolType +): Promise { + let poolId = ""; + for (const trancheListKey in deploymentAddresses.tranches) { + const trancheList = deploymentAddresses.tranches[trancheListKey]; + for (const tranche of trancheList) { + if (termAddress == tranche.address) { + if (poolType == PoolType.PT) { + poolId = tranche.ptPool.poolId; + } else { + poolId = tranche.ytPool.poolId; + } + } + } + } + return poolId; +} diff --git a/src/helpers/getPoolId.ts b/src/helpers/getPoolId.ts index 9339666..7f6b748 100644 --- a/src/helpers/getPoolId.ts +++ b/src/helpers/getPoolId.ts @@ -17,9 +17,12 @@ import { Signer } from "ethers"; import { Provider } from "@ethersproject/providers"; import { BasePool__factory } from "../../typechain/factories/BasePool__factory"; +import { Tranche__factory } from "../../typechain/factories/Tranche__factory"; +import { InterestToken__factory } from "../../typechain/factories/InterestToken__factory"; +import { DeploymentAddresses } from "../../typechain/DeploymentAddresses"; /** - * Returns the reserves for a given pool. + * Returns the PoolId for a given pool. * @param poolAddress any pool with a getPoolId method * @param signerOrProvider * @returns a promise for a poolId diff --git a/src/swap.ts b/src/swap.ts index 95283f5..fd2d4e0 100644 --- a/src/swap.ts +++ b/src/swap.ts @@ -27,12 +27,12 @@ import { ONE_DAY_IN_SECONDS } from "../src/constants/time"; const BALANCER_ETH_SENTINEL = "0x0000000000000000000000000000000000000000"; -enum SwapKind { +export enum SwapKind { GIVEN_IN, GIVEN_OUT, } -interface SingleSwap { +export interface SingleSwap { poolId: string; kind: SwapKind; assetIn: string; @@ -109,13 +109,7 @@ export async function swap( tokenInAddress === BALANCER_ETH_SENTINEL ? { value: amount } : undefined; const vaultContract = Vault__factory.connect(balancerVaultAddress, signer); - const swapReceipt = await vaultContract.swap( - swap, - funds, - limit, - deadline, - overrides - ); + const swapReceipt = await vaultContract.swap(swap, funds, limit, deadline); return swapReceipt; }