您当前的位置: 首页 > 

mutourend

暂无认证

  • 1浏览

    0关注

    661博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Aztec connect bridge代码解析

mutourend 发布时间:2022-03-02 12:10:04 ,浏览量:1

1. 引言

主要代码见:

  • https://github.com/AztecProtocol/aztec-connect-bridges(Solidity)
  • https://github.com/AztecProtocol/defi-bridge-hackathon(JavaScript)

本文主要借助Element Finance的Aztec Connect Contract合约来说明。

1.1 关键词解释
  • Principal Token:表示lender存入defi中的抵押品。
  • Yield Token:表示lender因存放抵押品而产生的可变利息。
2. aztec connect bridge集成方案

在Element Finance中,有一些对aztec connect bridge合约有用的流程:

  • 1)从AMM中以一定折扣购买principal tokens,按期持有并赎回。
  • 2)批量赎回。
  • 3)Auto rollover of mints。自动累加mints。

最容易支持的流程是从AMM购买token 以及 支持token赎回。 取决于存入的资产 以及 想要支持的粒度,有3种潜在的实现方式:

  • 1)Direct Async Bridge Flow:用户在zk.money中已有相应的source assets,或者愿意支付gas费来向zk.money存入资产。
2.1 方案1——Direct Async Bridge Flow

用户在zk.money中已有相应的source assets,或者愿意支付gas费来向zk.money存入资产。

Element支持的资产类型有:DAI/USDC/WBTC,以及,crv3crypto, steCRV等复合资产。

相应的用户流程为:

  • 1)用户在aztec L2层,选择所支持资产的shielded balance,发起一笔DeFi deposit交易。
  • 2)aztec L2层 与 以太坊L1层 之间的rollup provider,会将L2层的交易打包,以每 n n n笔一组。
  • 3)rollup provider会将打包后的交易提交到以太坊L1层的Aztec Rollup合约。
  • 4)以太坊L1层的Aztec Rollup合约会调用部署在L1层的相应的Element的bridge合约。该bridge合约会受到来自于Aztec Rollup合约的aggregated transaction,在bridge合约内:
    • 4.1)若对input asset有多个Element池,则DBC应使用auxData来选择正确的pool expiry。
    • 4.2)将以given input asset ERC20 tokens,从Element AMM中购买totalInputValue个principal token。
    • 4.3)记录购买交互操作中的interaction nonce值 和 所购买的principal token数量。
    • 4.4)记录interaction nonce值 和 相应的到期日,使得该异步交易可在后续日期finalised。
    • 4.5)给L1层的Aztec Rollup合约返回isAsync=true

bridge合约中应有一个public函数,可返回a list of bridges that can be finalised。

对于返回的每个bridge的interactionNonce值,可创建一笔以太坊交易,调用Aztec Rollup合约中的RollupContract.processAsyncDefiInteraction(uint256 interactionNonce)来最终finalise on the bridge contract。

// Aztec Rollup合约
function processAsyncDeFiInteraction(uint256 interactionNonce) external {
        // call canFinalise on the bridge
        // call finalise on the bridge
        DefiInteraction storage interaction = defiInteractions[
            interactionNonce
        ];
        require(
            interaction.bridgeAddress != address(0),
            "Rollup Contract: UNKNOWN_NONCE"
        );

        (uint256 outputValueA, uint256 outputValueB, bool interactionComplete) = IDefiBridge(
            interaction.bridgeAddress
        ).finalise(
                interaction.inputAssetA,
                interaction.inputAssetB,
                interaction.outputAssetA,
                interaction.outputAssetB,
                interaction.interactionNonce,
                uint64(interaction.auxInputData)
            );
            .......
}

在Element bridge合约中,其finalise函数将执行以下操作:

  • 1)将principal token兑换为基础抵押品。
  • 2)向提供此服务的block.coinbasemsg.sender支付fee。可计算为a gas multiplier and paid from the collateral.redemption。
  • 3)返回 基础抵押品 + 利息 到aztec rollup合约中,返回outputAssetA的正确值。
// Element Bridge合约
// serves as the 'off ramp' for the transaction
  // converts the principal tokens back to the underlying asset
  function finalise(
    AztecTypes.AztecAsset calldata,
    AztecTypes.AztecAsset calldata,
    AztecTypes.AztecAsset calldata outputAssetA,
    AztecTypes.AztecAsset calldata,
    uint256 interactionNonce,
    uint64
  ) external payable returns (uint256 outputValueA, uint256 outputValueB, bool interactionComplete) {

    require(msg.sender == rollupProcessor, "ElementBridge: INVALID_CALLER");
    // retrieve the interaction and verify it's ready for finalising
    Interaction storage interaction = interactions[interactionNonce];
    require(interaction.expiry != 0, "ElementBridge: UNKNOWN_NONCE");
    require(
      interaction.expiry             
关注
打赏
1664532908
查看更多评论
0.0399s