Skip to content

Instantly share code, notes, and snippets.

@gorgos
Last active October 31, 2021 01:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorgos/2e0b24f59289bd7629797c9cb3a92620 to your computer and use it in GitHub Desktop.
Save gorgos/2e0b24f59289bd7629797c9cb3a92620 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.7.2+commit.51b20bc0.js&optimize=true&gist=
// SPDX-License-Identifier: MIT
pragma solidity 0.7.2;
interface PoolInterface {
function swapExactAmountIn(address, uint, address, uint, uint) external returns (uint, uint);
function swapExactAmountOut(address, uint, address, uint, uint) external returns (uint, uint);
}
interface TokenInterface {
function balanceOf(address) external returns (uint);
function allowance(address, address) external returns (uint);
function approve(address, uint) external returns (bool);
function transfer(address, uint) external returns (bool);
function transferFrom(address, address, uint) external returns (bool);
function deposit() external payable;
function withdraw(uint) external;
}
contract BalancerTrader {
PoolInterface public bPool;
TokenInterface public daiToken;
TokenInterface public weth;
constructor(PoolInterface bPool_, TokenInterface daiToken_, TokenInterface weth_) {
bPool = bPool_;
daiToken = daiToken_;
weth = weth_;
}
function pay(uint paymentAmountInDai) public payable {
if (msg.value > 0) {
_swapEthForDai(paymentAmountInDai);
} else {
require(daiToken.transferFrom(msg.sender, address(this), paymentAmountInDai));
}
}
function _swapEthForDai(uint daiAmount) private {
_wrapEth(); // wrap ETH and approve to balancer pool
PoolInterface(bPool).swapExactAmountOut(
address(weth),
type(uint).max, // maxAmountIn, set to max -> use all sent ETH
address(daiToken),
daiAmount,
type(uint).max // maxPrice, set to max -> accept any swap prices
);
require(daiToken.transfer(msg.sender, daiToken.balanceOf(address(this))), "ERR_TRANSFER_FAILED");
_refundLeftoverEth();
}
function _wrapEth() private {
weth.deposit{ value: msg.value }();
if (weth.allowance(address(this), address(bPool)) < msg.value) {
weth.approve(address(bPool), type(uint).max);
}
}
function _refundLeftoverEth() private {
uint wethBalance = weth.balanceOf(address(this));
if (wethBalance > 0) {
// refund leftover ETH
weth.withdraw(wethBalance);
(bool success,) = msg.sender.call{ value: wethBalance }("");
require(success, "ERR_ETH_FAILED");
}
}
receive() external payable {}
}
@kavofa
Copy link

kavofa commented Oct 2, 2020

This isn't a working example.

The things I listed below will throw an error on compilation.

  1. The two interfaces: PoolInterface and TokenInterface are already defiend in the BalancerProxy contract.
  2. The imported BalancerProxy contract has a different solidity version.
  3. The variables daiToken, weth and bPool are not difiend.

Please fix or remove from your blog post, as the code is stated as working.

@gorgos
Copy link
Author

gorgos commented Oct 2, 2020

@kavofa I had copied the wrong code, fixed now. The code in the blog post was correct by the way. Keep in mind that the blog post is not a full code example.

@kavofa
Copy link

kavofa commented Oct 4, 2020

@gorgos Thanks, great!

@ElementalBrian
Copy link

putting the balancer addresses in here would have been too practical

@gorgos
Copy link
Author

gorgos commented Oct 23, 2020

putting the balancer addresses in here would have been too practical

Well, the addresses depend on the network and pool you want to use.

@tigermumu
Copy link

How to call it ?
I have deployed it and set the function pay parameter is 10.
BPOOL is Core Pool Factory: 0x8f7F78080219d4066A8036ccD30D588B416a40DB refer to link : https://docs.balancer.finance/smart-contracts/addresses.
kovan weth : 0xd0A1E359811322d97991E03f863a0C30C2cF029C
kovan dai : 0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD
I make the transaction but fail. tx: https://kovan.etherscan.io/tx/0xba5879f01570b56595ab18f872de84cf97c7761a958c47a864a544ca19c6dcd5
Do I make some parameter mistake?

@gorgos
Copy link
Author

gorgos commented Oct 31, 2020

@tigermumu Yes the pool address is incorrect. As mentioned in the blog post, you can find valid pool addresses via https://kovan.pools.balancer.exchange/#//.

The factory address you used it for creating new pools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment