Skip to content

Commit

Permalink
Underlying amount in erc4626 client (#246)
Browse files Browse the repository at this point in the history
* feat: getUnderlyingAmount in ERC4626 client

* chore: version bump
  • Loading branch information
benesjan authored Sep 29, 2022
1 parent 0fa1401 commit a6d5f32
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aztec/bridge-clients",
"version": "0.1.61",
"version": "0.1.62",
"description": "This repo contains the solidity files and typescript helper class for all of the Aztec Connect Bridge Contracts",
"repository": "[email protected]:AztecProtocol/aztec-connect-bridges.git",
"license": "Apache-2.0",
Expand Down
36 changes: 29 additions & 7 deletions src/client/erc4626/erc4626-bridge-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EthAddress } from "@aztec/barretenberg/address";
import { BigNumber } from "ethers";
import { IERC4626, IERC4626__factory } from "../../../typechain-types";
import { IERC20Metadata, IERC20Metadata__factory, IERC4626, IERC4626__factory } from "../../../typechain-types";
import { AztecAsset, AztecAssetType } from "../bridge-data";
import { ERC4626BridgeData } from "./erc4626-bridge-data";

Expand All @@ -14,18 +14,13 @@ type Mockify<T> = {

describe("ERC4626 bridge data", () => {
let erc4626Contract: Mockify<IERC4626>;
let erc2MetadataContract: Mockify<IERC20Metadata>;

let ethAsset: AztecAsset;
let mplAsset: AztecAsset;
let xmplAsset: AztecAsset;
let emptyAsset: AztecAsset;

beforeAll(() => {
ethAsset = {
id: 0,
assetType: AztecAssetType.ETH,
erc20Address: EthAddress.ZERO,
};
mplAsset = {
id: 10, // Asset has not yet been registered on RollupProcessor so this id is random
assetType: AztecAssetType.ERC20,
Expand Down Expand Up @@ -121,4 +116,31 @@ describe("ERC4626 bridge data", () => {
const asset = await erc4626BridgeData.getAsset(xmplAsset.erc20Address);
expect(asset.toString()).toBe(mplAsset.erc20Address.toString());
});

it("should correctly return underlying asset", async () => {
// Setup mocks
erc4626Contract = {
...erc4626Contract,
asset: jest.fn().mockResolvedValue(mplAsset.erc20Address.toString()),
previewRedeem: jest.fn(() => BigNumber.from("100")),
};
IERC4626__factory.connect = () => erc4626Contract as any;

erc2MetadataContract = {
...erc2MetadataContract,
name: jest.fn().mockResolvedValue("Maple Token"),
symbol: jest.fn().mockResolvedValue("MPL"),
decimals: jest.fn().mockResolvedValue(18),
};
IERC20Metadata__factory.connect = () => erc2MetadataContract as any;

const erc4626BridgeData = ERC4626BridgeData.create({} as any);
const underlyingAsset = await erc4626BridgeData.getUnderlyingAmount(xmplAsset, 10n ** 18n);

expect(underlyingAsset.address.toString()).toBe(mplAsset.erc20Address.toString());
expect(underlyingAsset.name).toBe("Maple Token");
expect(underlyingAsset.symbol).toBe("MPL");
expect(underlyingAsset.decimals).toBe(18);
expect(underlyingAsset.amount).toBe(100n);
});
});
30 changes: 27 additions & 3 deletions src/client/erc4626/erc4626-bridge-data.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { EthAddress } from "@aztec/barretenberg/address";
import { EthereumProvider } from "@aztec/barretenberg/blockchain";
import { Web3Provider } from "@ethersproject/providers";
import { IERC4626__factory } from "../../../typechain-types";
import { IERC20Metadata__factory, IERC4626__factory } from "../../../typechain-types";
import { createWeb3Provider } from "../aztec/provider";
import { AuxDataConfig, AztecAsset, BridgeDataFieldGetters, SolidityType } from "../bridge-data";
import { AuxDataConfig, AztecAsset, BridgeDataFieldGetters, SolidityType, UnderlyingAsset } from "../bridge-data";

export class ERC4626BridgeData implements BridgeDataFieldGetters {
readonly WETH = EthAddress.fromString("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
shareToAssetMap = new Map<EthAddress, EthAddress>();

protected constructor(protected ethersProvider: Web3Provider) {}
Expand Down Expand Up @@ -97,4 +96,29 @@ export class ERC4626BridgeData implements BridgeDataFieldGetters {
}
return asset;
}

/**
* @notice This function gets the underlying amount of asset corresponding to shares
* @param share Address of the share/vault
* @param amount Amount of shares to get underlying amount for
* @return Underlying amount of asset corresponding to the amount of shares
*/
async getUnderlyingAmount(share: AztecAsset, amount: bigint): Promise<UnderlyingAsset> {
const vault = IERC4626__factory.connect(share.erc20Address.toString(), this.ethersProvider);
const assetAddress = EthAddress.fromString(await vault.asset());

const tokenContract = IERC20Metadata__factory.connect(assetAddress.toString(), this.ethersProvider);
const namePromise = tokenContract.name();
const symbolPromise = tokenContract.symbol();
const decimalsPromise = tokenContract.decimals();
const amountPromise = vault.previewRedeem(amount);

return {
address: assetAddress,
name: await namePromise,
symbol: await symbolPromise,
decimals: await decimalsPromise,
amount: (await amountPromise).toBigInt(),
};
}
}

0 comments on commit a6d5f32

Please sign in to comment.