Skip to content

Commit

Permalink
WETH Proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNe0x1 committed Dec 19, 2023
1 parent 72b4bc8 commit 132956b
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 7 deletions.
43 changes: 43 additions & 0 deletions contracts/DePayWETHExchangeV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: BUSL-1.1

pragma solidity 0.8.18;

import './interfaces/IWETH.sol';

/// @title DePayWETHExchangeV1
/// @notice This contract limits access to WETH to the functions wrap & unwrap only.
contract DePayWETHExchangeV1 {

/// @notice Address of WETH
IWETH public immutable WETH;

/// @dev Initializes the contract with WETH address.
/// @param _WETH The address of the WETH contract.
constructor (address _WETH) {
WETH = IWETH(_WETH);
}

/// @notice Accepts NATIVE transfers, required for unwrapping.
receive() external payable {}

/// @notice Deposits native currency and wraps it into WETH.
/// @dev Wraps sent value into WETH and transfers it back to the sender.
/// @return success Status of the deposit operation.
function deposit() external payable returns(bool){
WETH.deposit{value: msg.value}();
(bool success) = WETH.transfer(msg.sender, msg.value);
return success;
}

/// @notice Withdraws specified amount of WETH and unwraps it into native currency.
/// @dev Unwraps WETH into native currency and sends it back to the sender.
/// @param wad Amount of WETH to withdraw.
/// @return success Status of the withdrawal operation.
function withdraw(
uint wad
) external returns(bool){
WETH.withdraw(wad);
(bool success,) = msg.sender.call{value: wad}(new bytes(0));
return success;
}
}
10 changes: 10 additions & 0 deletions contracts/interfaces/IWETH.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.18;

interface IWETH {

function deposit() payable external;
function transfer(address dst, uint wad) external returns (bool);
function withdraw(uint wad) external;
}
58 changes: 58 additions & 0 deletions flatten/DePayWETHExchangeV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Dependency file: contracts/interfaces/IWETH.sol

// SPDX-License-Identifier: MIT

// pragma solidity 0.8.18;

interface IWETH {

function deposit() payable external;
function transfer(address dst, uint wad) external returns (bool);
function withdraw(uint wad) external;
}


// Root file: contracts/DePayWETHExchangeV1.sol


pragma solidity 0.8.18;

// import 'contracts/interfaces/IWETH.sol';

/// @title DePayWETHExchangeV1
/// @notice This contract limits access to WETH to the functions wrap & unwrap only.
contract DePayWETHExchangeV1 {

/// @notice Address of WETH
IWETH public immutable WETH;

/// @dev Initializes the contract with WETH address.
/// @param _WETH The address of the WETH contract.
constructor (address _WETH) {
WETH = IWETH(_WETH);
}

/// @notice Accepts NATIVE transfers, required for unwrapping.
receive() external payable {}

/// @notice Deposits native currency and wraps it into WETH.
/// @dev Wraps sent value into WETH and transfers it back to the sender.
/// @return success Status of the deposit operation.
function deposit() external payable returns(bool){
WETH.deposit{value: msg.value}();
(bool success) = WETH.transfer(msg.sender, msg.value);
return success;
}

/// @notice Withdraws specified amount of WETH and unwraps it into native currency.
/// @dev Unwraps WETH into native currency and sends it back to the sender.
/// @param wad Amount of WETH to withdraw.
/// @return success Status of the withdrawal operation.
function withdraw(
uint wad
) external returns(bool){
WETH.withdraw(wad);
(bool success,) = msg.sender.call{value: wad}(new bytes(0));
return success;
}
}
12 changes: 12 additions & 0 deletions flatten/IWETH.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Root file: contracts/interfaces/IWETH.sol

// SPDX-License-Identifier: MIT

pragma solidity 0.8.18;

interface IWETH {

function deposit() payable external;
function transfer(address dst, uint wad) external returns (bool);
function withdraw(uint wad) external;
}
20 changes: 13 additions & 7 deletions test/_pay-with-wrapped-conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default ({ blockchain })=>{
let router
let deadline
let wrapperContract
let exchange

beforeEach(async ()=>{
wallets = await ethers.getSigners()
Expand All @@ -32,16 +33,21 @@ export default ({ blockchain })=>{
router = await deploy()
})

it('approves WRAPPER contract as exchange to convert payments', async ()=> {
await router.connect(wallets[0]).enable(WRAPPED, true)
it('deploys WETHExchange successfully', async ()=> {
const DePayWETHExchange = await ethers.getContractFactory('DePayWETHExchangeV1')
exchange = await DePayWETHExchange.deploy(wrapperContract.address)
})

it('approves DePayWETHExchange contract as exchange to convert payments', async ()=> {
await router.connect(wallets[0]).enable(exchange.address, true)
})

it('wraps NATIVE to WRAPPED and pays out WRAPPED', async ()=>{
const amountIn = 1000000000
const paymentAmount = 900000000
const feeAmount = 100000000

const callData = wrapperContract.interface.encodeFunctionData("deposit", [])
const callData = exchange.interface.encodeFunctionData("deposit", [])

const paymentReceiverBalanceBefore = await wrapperContract.balanceOf(wallets[1].address)
const feeReceiverBalanceBefore = await wrapperContract.balanceOf(wallets[2].address)
Expand All @@ -51,7 +57,7 @@ export default ({ blockchain })=>{
paymentAmount: paymentAmount,
feeAmount: feeAmount,
tokenInAddress: NATIVE,
exchangeAddress: WRAPPED,
exchangeAddress: exchange.address,
tokenOutAddress: WRAPPED,
paymentReceiverAddress: wallets[1].address,
feeReceiverAddress: wallets[2].address,
Expand All @@ -75,7 +81,7 @@ export default ({ blockchain })=>{
const paymentAmount = 900000000
const feeAmount = 100000000

const callData = wrapperContract.interface.encodeFunctionData("withdraw", [amountIn])
const callData = exchange.interface.encodeFunctionData("withdraw", [amountIn])

const paymentReceiverBalanceBefore = await provider.getBalance(wallets[1].address)
const feeReceiverBalanceBefore = await provider.getBalance(wallets[2].address)
Expand All @@ -88,11 +94,11 @@ export default ({ blockchain })=>{
paymentAmount: paymentAmount,
feeAmount: feeAmount,
tokenInAddress: WRAPPED,
exchangeAddress: WRAPPED,
exchangeAddress: exchange.address,
tokenOutAddress: NATIVE,
paymentReceiverAddress: wallets[1].address,
feeReceiverAddress: wallets[2].address,
exchangeType: 0,
exchangeType: 2,
receiverType: 0,
exchangeCallData: callData,
receiverCallData: ZERO,
Expand Down

0 comments on commit 132956b

Please sign in to comment.