Skip to content
This repository has been archived by the owner on Oct 8, 2022. It is now read-only.

Commit

Permalink
mods for swap example
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhea committed Jun 10, 2021
1 parent 79da5a8 commit 943234d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 10 deletions.
82 changes: 82 additions & 0 deletions examples/swap.ts
Original file line number Diff line number Diff line change
@@ -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 = <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();
33 changes: 33 additions & 0 deletions src/helpers/getElementAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string> {
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;
}
5 changes: 4 additions & 1 deletion src/helpers/getPoolId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions src/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

0 comments on commit 943234d

Please sign in to comment.