如果我们需要使用在协议中使用原生 ETH(等 native token),则必须首先将其 wrapped 成 WETH 中。WETH Gateway 是一个辅助合约,在与协议交互时根据需要轻松包装和解包 ETH,因为协议交互中只能使用 ERC20.

源码:https://github.com/aave/protocol-v2/blob/master/contracts/misc/WETHGateway.sol

Methods

depositETH()

function depositETH(address lendingPool, address onBehalfOf, uint16 referralCode)

msg.value数量的 ETH 存入协议中,mint 等量对应的 aWETH,并转入onBehalfOf地址。

有关 referralCode 输入说明,请参阅文档的推荐计划部分。在测试期间,您可以使用推荐代码:0。

交易还包括在 msg.value中存入的 ETH 金额

参数名称 类型 描述
LendingPool address 目标底层借贷池的地址
onBehalfOf address 接收 aWETH 的地址 当将 aToken 发送给调用者时,请使用 msg.sender
referralCode uint16 0 表示没有使用推荐码

withdrawETH()

function withdrawETH(address lendingPool, uint256 amount, addresss to)

要取出的 WETH 的金额,将其解包为 ETH,并将 ETH 转移到 to 地址。

确保在调用此函数之前设置了 aWETH 的相关 ERC20 allowance,以便 WETHGateway合约可以销毁相关的 aWETH

参数名称 类型 描述
lendingPool address 目标基础借贷池的地址
amount uint256 存款金额,以 wei 表示。 使用 type(uint).max表示提取全部余额
to address 解包的 ETH 的接收地址

repayETH()

function repayETH(address lendingPool, uint256 amount, uint256 rateMode, address onBehalfOf)

偿还 onBehalfOf``rateMode利率模式下amount金额的 ETH 债务。

确保repayETH()交易还包括 msg.value中偿还的 ETH 金额。

参数名称 类型 描述
lendingPool address 目标基础借贷池的地址
amount uint256 借款金额,用 wei 作为单位 使用 uint(-1)偿还全部债务。(仅当为代表第三方还款时) 如果代表其他用户还款,建议发送略高于当前借款金额的 _amount
rateMode uint256 借款的债务类型 Stable:1,Variable:2
onBehalfOf address 偿还债务用户的地址 当不代替其他用户调用时,请使用msg.sender

borrowETH()

function borrowETH(address lendingPool, uint256 amount, uint256 interestRateMode, uint16 referralCode)

使用 interestRateMode利率模式借入 amount数量的 WETH,并将amount数量的未打包的 WETH 发送给 msg.sender

参数名称 类型 描述
lendingPool address 目标基础借贷池地址
amount uint256 借入的金额,以 wei 作为单位
interestRateMode uint256 借款的类型 Stable:1,Variable:2
referralCode uint16 0 表示没有使用推荐码

View Methods

getWETHAddress()

function getWETHAddress()

返回 WETHGateway 使用的 WETH 地址。

ABI

[
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "weth",
                "type": "address"
            }
        ],
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "internalType": "address",
                "name": "previousOwner",
                "type": "address"
            },
            {
                "indexed": true,
                "internalType": "address",
                "name": "newOwner",
                "type": "address"
            }
        ],
        "name": "OwnershipTransferred",
        "type": "event"
    },
    {
        "stateMutability": "payable",
        "type": "fallback"
    },
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "lendingPool",
                "type": "address"
            }
        ],
        "name": "authorizeLendingPool",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "lendingPool",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            },
            {
                "internalType": "uint256",
                "name": "interesRateMode",
                "type": "uint256"
            },
            {
                "internalType": "uint16",
                "name": "referralCode",
                "type": "uint16"
            }
        ],
        "name": "borrowETH",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "lendingPool",
                "type": "address"
            },
            {
                "internalType": "address",
                "name": "onBehalfOf",
                "type": "address"
            },
            {
                "internalType": "uint16",
                "name": "referralCode",
                "type": "uint16"
            }
        ],
        "name": "depositETH",
        "outputs": [],
        "stateMutability": "payable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "to",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            }
        ],
        "name": "emergencyEtherTransfer",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "token",
                "type": "address"
            },
            {
                "internalType": "address",
                "name": "to",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            }
        ],
        "name": "emergencyTokenTransfer",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "getWETHAddress",
        "outputs": [
            {
                "internalType": "address",
                "name": "",
                "type": "address"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "owner",
        "outputs": [
            {
                "internalType": "address",
                "name": "",
                "type": "address"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "name": "renounceOwnership",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "lendingPool",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            },
            {
                "internalType": "uint256",
                "name": "rateMode",
                "type": "uint256"
            },
            {
                "internalType": "address",
                "name": "onBehalfOf",
                "type": "address"
            }
        ],
        "name": "repayETH",
        "outputs": [],
        "stateMutability": "payable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "newOwner",
                "type": "address"
            }
        ],
        "name": "transferOwnership",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "lendingPool",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            },
            {
                "internalType": "address",
                "name": "to",
                "type": "address"
            }
        ],
        "name": "withdrawETH",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "stateMutability": "payable",
        "type": "receive"
    }
]

IWETHGateway

// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.12;

interface IWETHGateway {
  function depositETH(
    address lendingPool,
    address onBehalfOf,
    uint16 referralCode
  ) external payable;

  function withdrawETH(
    address lendingPool,
    uint256 amount,
    address onBehalfOf
  ) external;

  function repayETH(
    address lendingPool,
    uint256 amount,
    uint256 rateMode,
    address onBehalfOf
  ) external payable;

  function borrowETH(
    address lendingPool,
    uint256 amount,
    uint256 interesRateMode,
    uint16 referralCode
  ) external;

}