Skip to content

Commit

Permalink
Pw/element (#24)
Browse files Browse the repository at this point in the history
* WIP

* Updated bridge data class

* Updated element bridge with tests

* Removed duplicate file

* Set block number

* Removed unused catch param

* Bridge and test updates

* Removed allowanc checks

* Minor refactor

* Added todo comment

* ElementBridge before heap refactor

* Refactored expiry heap and additional tests

* Removed logs

* Removed console import

* Review changes

* Test fix attempt

* Removed redundant value

* Updates to element bridge
  • Loading branch information
PhilWindle committed Mar 10, 2022
1 parent f6994be commit 120ec68
Show file tree
Hide file tree
Showing 22 changed files with 2,773 additions and 836 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"clean": "rm -rf ./cache ./dest ./out ./artifacts ./typechain-types",
"test": "yarn test:contracts && yarn test:clients",
"test:client": "jest test",
"test:contracts": "forge test -vvv",
"test:contracts:block": "forge test --fork-block-number",
"cast": "cast",
"test:contracts": "forge test --fork-block-number 14000000 -vvv",
"build": "yarn clean && yarn compile:typechain && yarn compile:client-dest"
},
"dependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/aztec/DefiBridgeProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {TokenTransfers} from "../libraries/TokenTransfers.sol";

import "../../lib/ds-test/src/test.sol";

import { console } from '../test/console.sol';

contract DefiBridgeProxy is DSTest {
bytes4 private constant BALANCE_OF_SELECTOR = 0x70a08231; // bytes4(keccak256('balanceOf(address)'));
bytes4 private constant TRANSFER_SELECTOR = 0xa9059cbb; // bytes4(keccak256('transfer(address,uint256)'));
Expand Down Expand Up @@ -166,7 +168,8 @@ contract DefiBridgeProxy is DSTest {
outputAssetB,
totalInputValue,
interactionNonce,
uint64(auxInputData)
uint64(auxInputData),
address(0)
);

if (isAsync) {
Expand Down
159 changes: 104 additions & 55 deletions src/aztec/RollupProcessor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {AztecTypes} from "./AztecTypes.sol";

import "../../lib/ds-test/src/test.sol";

import { console } from '../test/console.sol';

contract RollupProcessor is DSTest {
DefiBridgeProxy private bridgeProxy;

Expand All @@ -26,6 +28,7 @@ contract RollupProcessor is DSTest {

bytes4 private constant TRANSFER_FROM_SELECTOR = 0x23b872dd; // bytes4(keccak256('transferFrom(address,address,uint256)'));
mapping(uint256 => uint256) ethPayments;
mapping(address => uint256) bridgeGasLimits;

// DEFI_BRIDGE_PROXY_CONVERT_SELECTOR = function signature of:
// function convert(
Expand Down Expand Up @@ -63,6 +66,15 @@ contract RollupProcessor is DSTest {
ethPayments[interactionNonce] += msg.value;
}

function setBridgeGasLimit(address bridgeAddress, uint256 gasLimit) external {
bridgeGasLimits[bridgeAddress] = gasLimit;
}

function getBridgeGasLimit(address bridgeAddress) private view returns (uint256 limit) {
limit = bridgeGasLimits[bridgeAddress];
limit = limit == 0 ? 200000 : limit;
}

mapping(uint256 => DefiInteraction) private defiInteractions;

constructor(address _bridgeProxyAddress) {
Expand Down Expand Up @@ -106,7 +118,7 @@ contract RollupProcessor is DSTest {
// VIRTUAL Assets are not transfered.
}

function processAsyncDeFiInteraction(uint256 interactionNonce) external {
function processAsyncDefiInteraction(uint256 interactionNonce) external returns (bool completed) {
// call canFinalise on the bridge
// call finalise on the bridge
DefiInteraction storage interaction = defiInteractions[
Expand All @@ -127,6 +139,7 @@ contract RollupProcessor is DSTest {
interaction.interactionNonce,
uint64(interaction.auxInputData)
);
completed = interactionComplete;

if (
outputValueB > 0 &&
Expand Down Expand Up @@ -167,7 +180,92 @@ contract RollupProcessor is DSTest {
outputValueB,
true
);
}

struct ConvertArgs {
address bridgeAddress;
AztecTypes.AztecAsset inputAssetA;
AztecTypes.AztecAsset inputAssetB;
AztecTypes.AztecAsset outputAssetA;
AztecTypes.AztecAsset outputAssetB;
uint256 totalInputValue;
uint256 interactionNonce;
uint256 auxInputData;
uint256 ethPaymentsSlot;
}

struct ConvertReturnValues {
uint256 outputValueA;
uint256 outputValueB;
bool isAsync;
}

function _convert(ConvertArgs memory convertArgs) private returns (
ConvertReturnValues memory results
) {
defiInteractions[convertArgs.interactionNonce] = DefiInteraction(
convertArgs.bridgeAddress,
convertArgs.inputAssetA,
convertArgs.inputAssetB,
convertArgs.outputAssetA,
convertArgs.outputAssetB,
convertArgs.totalInputValue,
convertArgs.interactionNonce,
convertArgs.auxInputData
);


(bool success, bytes memory result) = address(bridgeProxy).delegatecall{gas: bridgeGasLimits[convertArgs.bridgeAddress]}(
abi.encodeWithSelector(
DEFI_BRIDGE_PROXY_CONVERT_SELECTOR,
convertArgs.bridgeAddress,
convertArgs.inputAssetA,
convertArgs.inputAssetB,
convertArgs.outputAssetA,
convertArgs.outputAssetB,
convertArgs.totalInputValue,
convertArgs.interactionNonce,
convertArgs.auxInputData,
convertArgs.ethPaymentsSlot
)
);

results = ConvertReturnValues(0, 0, false);

if (success) {
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = abi.decode(
result,
(uint256, uint256, bool)
);
if (!isAsync) {
emit DefiBridgeProcessed (
0,
convertArgs.interactionNonce,
convertArgs.totalInputValue,
outputValueA,
outputValueB,
true
);
}
results.outputValueA = outputValueA;
results.outputValueB = outputValueB;
results.isAsync = isAsync;
}
require(success, 'Interaction Failed');

// else {


// emit DefiBridgeProcessed(
// 0,
// interactionNonce,
// totalInputValue,
// totalInputValue,
// inputAssetB.NOT_USED || inputAssetB.,
// success
// );
// }
// TODO: Should probaby emit an event for failed?
}

function convert(
Expand All @@ -191,66 +289,17 @@ contract RollupProcessor is DSTest {
defiInteractions[interactionNonce].auxInputData == 0,
"Rollup Contract: INTERACTION_ALREADY_EXISTS"
);
defiInteractions[interactionNonce] = DefiInteraction(
bridgeAddress,
inputAssetA,
inputAssetB,
outputAssetA,
outputAssetB,
totalInputValue,
interactionNonce,
auxInputData
);

uint256 ethPayments_slot;

assembly {
ethPayments_slot := ethPayments.slot
}

(bool success, bytes memory result) = address(bridgeProxy).delegatecall(
abi.encodeWithSelector(
DEFI_BRIDGE_PROXY_CONVERT_SELECTOR,
bridgeAddress,
inputAssetA,
inputAssetB,
outputAssetA,
outputAssetB,
totalInputValue,
interactionNonce,
auxInputData,
ethPayments_slot
)
);

if (success) {
(outputValueA, outputValueB, isAsync) = abi.decode(
result,
(uint256, uint256, bool)
);

emit DefiBridgeProcessed(
0,
interactionNonce,
totalInputValue,
outputValueA,
outputValueB,
true
);
}
require(success, 'Interation Failed');
// else {


// emit DefiBridgeProcessed(
// 0,
// interactionNonce,
// totalInputValue,
// totalInputValue,
// inputAssetB.NOT_USED || inputAssetB.,
// success
// );
// }
// TODO: Should probaby emit an event for failed?
ConvertArgs memory convertArgs = ConvertArgs(bridgeAddress, inputAssetA, inputAssetB, outputAssetA, outputAssetB, totalInputValue, interactionNonce, auxInputData, ethPayments_slot);
(ConvertReturnValues memory results) = _convert(convertArgs);
outputValueA = results.outputValueA;
outputValueB = results.outputValueB;
isAsync = results.isAsync;
}
}
5 changes: 3 additions & 2 deletions src/bridges/aave/AaveLending.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract AaveLendingBridge is IDefiBridge {

mapping(address => address) public underlyingToZkAToken;

constructor(address _rollupProcessor, address _addressesProvider) public {
constructor(address _rollupProcessor, address _addressesProvider) {
rollupProcessor = _rollupProcessor;
// needed in case AAVE governance changes the lending pool address
addressesProvider = ILendingPoolAddressesProvider(_addressesProvider);
Expand Down Expand Up @@ -64,7 +64,8 @@ contract AaveLendingBridge is IDefiBridge {
AztecTypes.AztecAsset memory outputAssetB,
uint256 totalInputValue,
uint256 interactionNonce,
uint64 auxData
uint64 auxData,
address rollupBeneficiary
)
external
payable
Expand Down
Loading

0 comments on commit 120ec68

Please sign in to comment.