Skip to content

Commit e942802

Browse files
author
Joe Andrews
authored
fix up the bridge data class (#63)
* fix up the bridge data class * update yarn.lock * increment version
1 parent f137fa4 commit e942802

12 files changed

+171
-165
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
},
1919
"dependencies": {
2020
"@ethersproject/providers": "^5.6.5",
21-
"axios": "^0.26.1",
2221
"detect-node": "^2.1.0",
2322
"sha3": "^2.1.4"
2423
},
@@ -30,7 +29,7 @@
3029
"rootDir": "./src"
3130
},
3231
"name": "@aztec/bridge-clients",
33-
"version": "0.1.35",
32+
"version": "0.1.36",
3433
"description": "This repo contains the solidity files and typescript helper class for all of the Aztec Connect Bridge Contracts",
3534
"repository": "[email protected]:AztecProtocol/aztec-connect-bridges.git",
3635
"license": "MIT",
@@ -44,9 +43,11 @@
4443
"@typechain/hardhat": "^4.0.0",
4544
"@types/detect-node": "^2.0.0",
4645
"@types/jest": "^27.4.0",
46+
"@types/node-fetch": "^2.6.1",
4747
"ethers": "^5.5.4",
4848
"hardhat": "^2.6.8",
4949
"jest": "^27.5.0",
50+
"node-fetch": "2.6.7",
5051
"ts-jest": "^27.1.3",
5152
"ts-node": "^10.4.0",
5253
"typechain": "^7.0.1",

src/client/bridge-data.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ export interface AuxDataConfig {
3434
solidityType: SolidityType;
3535
}
3636

37-
export interface BridgeData {
37+
export interface BridgeDataFieldGetters {
3838
/*
3939
@dev This function should be implemented for stateful bridges. It's purpose is to tell the developer using the bridge the value of a given interaction.
4040
@dev The value should be returned as an array of AssetValue's
4141
*/
42-
getInteractionPresentValue(interactionNonce: bigint): Promise<AssetValue[]>;
42+
getInteractionPresentValue?(interactionNonce: bigint): Promise<AssetValue[]>;
4343

4444
/*
4545
@dev This function should be implemented for all bridges that use auxData that require onchain data. It's purpose is to tell the developer using the bridge
4646
@dev the set of possible auxData's that they can use for a given set of inputOutputAssets.
4747
*/
48-
getAuxData(
48+
getAuxData?(
4949
inputAssetA: AztecAsset,
5050
inputAssetB: AztecAsset,
5151
outputAssetA: AztecAsset,
@@ -70,44 +70,50 @@ export interface BridgeData {
7070
auxData: bigint,
7171
inputValue: bigint,
7272
): Promise<bigint[]>;
73-
}
7473

75-
export interface AsyncBridgeData extends BridgeData {
7674
/*
7775
@dev This function should be implemented for async bridges. It should return the date at which the bridge is expected to be finalised.
7876
@dev For limit orders this should be the expiration.
7977
*/
80-
getExpiration(interactionNonce: bigint): Promise<bigint>;
78+
getExpiration?(interactionNonce: bigint): Promise<bigint>;
8179

82-
hasFinalised(interactionNonce: bigint): Promise<Boolean>;
83-
}
80+
hasFinalised?(interactionNonce: bigint): Promise<Boolean>;
8481

85-
export interface YieldBridgeData extends BridgeData {
8682
/*
8783
@dev This function should be implemented for all bridges are stateful. It should return the expected value 1 year from now of outputAssetA and outputAssetB
8884
@dev given inputValue of inputAssetA and inputAssetB
8985
*/
9086

91-
getExpectedYearlyOuput(
87+
getExpectedYeild?(
9288
inputAssetA: AztecAsset,
9389
inputAssetB: AztecAsset,
9490
outputAssetA: AztecAsset,
9591
outputAssetB: AztecAsset,
9692
auxData: bigint,
9793
inputValue: bigint,
98-
): Promise<bigint[]>;
94+
): Promise<number[]>;
9995

10096
/*
101-
@dev This function should be implemented for all bridges. It should return the Layer liquidity this bridge call will be interacting with
97+
@dev This function should be implemented for all bridges dealing with L1 liqudity pools. It should return the Layer liquidity this bridge call will be interacting with
10298
@dev e.g the liquidity of the underlying yield pool or AMM for a given pair
10399
*/
104-
getMarketSize(
100+
getMarketSize?(
105101
inputAssetA: AztecAsset,
106102
inputAssetB: AztecAsset,
107103
outputAssetA: AztecAsset,
108104
outputAssetB: AztecAsset,
109105
auxData: bigint,
110106
): Promise<AssetValue[]>;
111-
}
112107

113-
export interface AsyncYieldBridgeData extends YieldBridgeData, AsyncBridgeData {}
108+
/*
109+
@dev This function should be implemented for all async yield bridges.
110+
@dev It should return the yield for a given interaction
111+
*/
112+
getCurrentYield?(interactionNonce: bigint): Promise<number[]>;
113+
114+
/*
115+
@dev This function should be implemented for swap / LP bridges.
116+
@dev It should return a packed aux data taking into account a dynamic price and fee
117+
*/
118+
lPAuxData?(data: bigint[]): Promise<bigint[]>;
119+
}

src/client/element/element-bridge-data.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe('element bridge data', () => {
251251
balancerContract as any,
252252
rollupContract as any,
253253
);
254-
const output = await elementBridgeData.getExpectedYearlyOuput(
254+
const output = await elementBridgeData.getExpectedYield(
255255
{
256256
assetType: AztecAssetType.ERC20,
257257
erc20Address: 'test',
@@ -280,7 +280,7 @@ describe('element bridge data', () => {
280280
const scaledOut = (BigInt(interest) * elementBridgeData.scalingFactor) / timeToExpiration;
281281
const yearlyOut = (scaledOut * BigInt(YEAR)) / elementBridgeData.scalingFactor;
282282

283-
expect(output[0]).toBe(yearlyOut + BigInt(inputValue));
283+
expect(output[0]).toBe(Number((BigInt(inputValue) / (yearlyOut + BigInt(inputValue))) * 100n));
284284
});
285285

286286
it('should return the correct market size for a given tranche', async () => {

src/client/element/element-bridge-data.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BridgeId } from '../aztec/bridge_id';
22
import { AddressZero } from '@ethersproject/constants';
3-
import { AssetValue, AsyncYieldBridgeData, AuxDataConfig, AztecAsset, SolidityType } from '../bridge-data';
3+
import { AssetValue, BridgeDataFieldGetters, AuxDataConfig, AztecAsset, SolidityType } from '../bridge-data';
44
import {
55
ElementBridge,
66
IVault,
@@ -68,7 +68,7 @@ const decodeEvent = async (event: AsyncDefiBridgeProcessedEvent) => {
6868
return newEventBlock;
6969
};
7070

71-
export class ElementBridgeData implements AsyncYieldBridgeData {
71+
export class ElementBridgeData implements BridgeDataFieldGetters {
7272
public scalingFactor = BigInt(1n * 10n ** 18n);
7373
private interactionBlockNumbers: Array<EventBlock> = [];
7474

@@ -211,13 +211,44 @@ export class ElementBridgeData implements AsyncYieldBridgeData {
211211
];
212212
}
213213

214+
async getCurrentYield(interactionNonce: bigint): Promise<number[]> {
215+
const interaction = await this.elementBridgeContract.interactions(interactionNonce);
216+
if (interaction === undefined) {
217+
return [];
218+
}
219+
const exitTimestamp = interaction.expiry;
220+
const endValue = interaction.quantityPT;
221+
222+
// we get the present value of the interaction
223+
const defiEvent = await this.findDefiEventForNonce(interactionNonce);
224+
if (defiEvent === undefined) {
225+
return [];
226+
}
227+
228+
const latestBlock = await this.getCurrentBlock();
229+
230+
const now = latestBlock.timestamp;
231+
const totalInterest = endValue.toBigInt() - defiEvent.totalInputValue;
232+
const elapsedTime = BigInt(now - defiEvent.timestamp);
233+
const totalTime = exitTimestamp.toBigInt() - BigInt(defiEvent.timestamp);
234+
const timeRatio = divide(elapsedTime, totalTime, this.scalingFactor);
235+
const accruedInterst = (totalInterest * timeRatio) / this.scalingFactor;
236+
237+
const currentYield = (defiEvent.totalInputValue / (defiEvent.totalInputValue + accruedInterst)) * 100n;
238+
return [Number(currentYield)];
239+
}
240+
214241
async getAuxData(
215242
inputAssetA: AztecAsset,
216243
inputAssetB: AztecAsset,
217244
outputAssetA: AztecAsset,
218245
outputAssetB: AztecAsset,
219246
): Promise<bigint[]> {
220-
return (await this.elementBridgeContract.getAssetExpiries(inputAssetA.erc20Address)).map(a => a.toBigInt());
247+
const assetExpiries = await this.elementBridgeContract.getAssetExpiries(inputAssetA.erc20Address);
248+
if (assetExpiries && assetExpiries.length) {
249+
return assetExpiries.map(a => a.toBigInt());
250+
}
251+
return [];
221252
}
222253

223254
public auxDataConfig: AuxDataConfig[] = [
@@ -241,14 +272,14 @@ export class ElementBridgeData implements AsyncYieldBridgeData {
241272
return [BigInt(0), BigInt(0), BigInt(1)];
242273
}
243274

244-
async getExpectedYearlyOuput(
275+
async getExpectedYield(
245276
inputAssetA: AztecAsset,
246277
inputAssetB: AztecAsset,
247278
outputAssetA: AztecAsset,
248279
outputAssetB: AztecAsset,
249280
auxData: bigint,
250281
precision: bigint,
251-
): Promise<bigint[]> {
282+
): Promise<number[]> {
252283
const assetExpiryHash = await this.elementBridgeContract.hashAssetAndExpiry(inputAssetA.erc20Address, auxData);
253284
const pool = await this.elementBridgeContract.pools(assetExpiryHash);
254285
const poolId = pool.poolId;
@@ -287,7 +318,7 @@ export class ElementBridgeData implements AsyncYieldBridgeData {
287318
const scaledOutput = divide(interest, timeToExpiration, this.scalingFactor);
288319
const yearlyOutput = (scaledOutput * YEAR) / this.scalingFactor;
289320

290-
return [yearlyOutput + precision, BigInt(0)];
321+
return [Number((precision * this.scalingFactor) / (yearlyOutput + precision) / this.scalingFactor / 10000n) / 100];
291322
}
292323

293324
async getMarketSize(

src/client/example/example-bridge-data.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { AssetValue, AuxDataConfig, AztecAsset, BridgeData, SolidityType } from '../bridge-data';
1+
import { AssetValue, AuxDataConfig, AztecAsset, BridgeDataFieldGetters, SolidityType } from '../bridge-data';
22

3-
export class ExampleBridgeData implements BridgeData {
3+
export class ExampleBridgeData implements BridgeDataFieldGetters {
44
constructor() {}
55

66
// @dev This function should be implemented for stateful bridges. It should return an array of AssetValue's
@@ -45,17 +45,6 @@ export class ExampleBridgeData implements BridgeData {
4545
return [100n, 0n];
4646
}
4747

48-
async getExpectedYearlyOuput(
49-
inputAssetA: AztecAsset,
50-
inputAssetB: AztecAsset,
51-
outputAssetA: AztecAsset,
52-
outputAssetB: AztecAsset,
53-
auxData: bigint,
54-
precision: bigint,
55-
): Promise<bigint[]> {
56-
return [100n, 0n];
57-
}
58-
5948
async getMarketSize(
6049
inputAssetA: AztecAsset,
6150
inputAssetB: AztecAsset,

src/client/lido/lido-bridge-data.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('lido bridge data', () => {
131131

132132
it('should correctly return the expectedYearlyOutput', async () => {
133133
const depositAmount = BigInt(1 * 10e18);
134-
const expectedOutput = 10432001397269423610n;
134+
const expectedOutput = 4.32;
135135

136136
wstethContract = {
137137
...wstethContract,
@@ -160,7 +160,7 @@ describe('lido bridge data', () => {
160160

161161
lidoBridgeData = createLidoBridgeData(wstethContract as any, curvePoolContract as any, lidoOracleContract as any);
162162

163-
const output = await lidoBridgeData.getExpectedYearlyOuput(
163+
const output = await lidoBridgeData.getExpectedYield(
164164
wstETHAsset,
165165
emptyAsset,
166166
ethAsset,

src/client/lido/lido-bridge-data.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AssetValue, AuxDataConfig, AztecAsset, SolidityType, AztecAssetType, YieldBridgeData } from '../bridge-data';
1+
import { AssetValue, AuxDataConfig, AztecAsset, SolidityType, AztecAssetType, BridgeDataFieldGetters } from '../bridge-data';
22

33
import {
44
IWstETH,
@@ -11,7 +11,7 @@ import {
1111
import { createWeb3Provider, EthereumProvider } from '../aztec/provider';
1212
import { EthAddress } from '../aztec/eth_address';
1313

14-
export class LidoBridgeData implements YieldBridgeData {
14+
export class LidoBridgeData implements BridgeDataFieldGetters {
1515
public scalingFactor: bigint = 1n * 10n ** 18n;
1616

1717
private constructor(
@@ -84,14 +84,14 @@ export class LidoBridgeData implements YieldBridgeData {
8484
}
8585
return [0n];
8686
}
87-
async getExpectedYearlyOuput(
87+
async getExpectedYield(
8888
inputAssetA: AztecAsset,
8989
inputAssetB: AztecAsset,
9090
outputAssetA: AztecAsset,
9191
outputAssetB: AztecAsset,
9292
auxData: bigint,
9393
precision: bigint,
94-
): Promise<bigint[]> {
94+
): Promise<Number[]> {
9595
const YEAR = 60n * 60n * 24n * 365n;
9696
if (outputAssetA.assetType === AztecAssetType.ETH) {
9797
const { postTotalPooledEther, preTotalPooledEther, timeElapsed } =
@@ -100,13 +100,10 @@ export class LidoBridgeData implements YieldBridgeData {
100100
const scaledAPR =
101101
((postTotalPooledEther.toBigInt() - preTotalPooledEther.toBigInt()) * YEAR * this.scalingFactor) /
102102
(preTotalPooledEther.toBigInt() * timeElapsed.toBigInt());
103-
const stETHBalance = await this.wstETHContract.getStETHByWstETH(precision);
104-
const ETHBalance = (await this.curvePoolContract.get_dy(1, 0, stETHBalance)).toBigInt();
105-
const expectedYearlyOutputETH = ETHBalance + (ETHBalance * scaledAPR) / this.scalingFactor;
106103

107-
return [expectedYearlyOutputETH];
104+
return [Number(scaledAPR / (this.scalingFactor / 10000n)) / 100];
108105
}
109-
return [0n];
106+
return [0];
110107
}
111108

112109
async getMarketSize(

0 commit comments

Comments
 (0)