EatTheBlocks Forum

Going from Eth -> token

I have two questions when going from Eth to erc20 token…

If I borrow Weth on dydx and buy an erc20 token swap then sell back to Eth would my pricing I’m polling for be in terms of Eth or would they need to be in terms of the token. So for AMPL would the price need to be 279.72 AMPL(current price) or .00357 Eth?

Second question. If I start with Weth and go UniswaptoKyber I would be ending with Eth on kyber network. Would I then need to do anything to swap it back to Weth to repay the loan or would it still work from Eth?

If I borrow Weth on dydx and buy an erc20 token swap then sell back to Eth would my pricing I’m > polling for be in terms of Eth or would they need to be in terms of the token. So for AMPL would the > price need to be 279.72 AMPL(current price) or .00357 Eth?

It’s the same because 1 WETH = 1 ETH.

Second question. If I start with Weth and go UniswaptoKyber I would be ending with Eth on kyber network. Would I then need to do anything to swap it back to Weth to repay the loan or would it still work from Eth?

If you borrow WETH, you need to reimburse WETH. You can easily swap back and forth between ETH and WETH with the deposit() and withdraw() functions of WETH.

Could you give an example of how it needs to be written out. Would the repayAmount in the flashloan need to be: IWeth(_token).approve(_solo, repayAmount)? Or would it be something different? I’m under the understanding that this is the only part that needs to be changed not quite sure what it should look like.

Not sure to understand what you mean by

Would the repayAmount in the flashloan need to be: IWeth(_token).approve(_solo, repayAmount)

The repayAmount is… the repayAmount, that’s it :slight_smile: It’s whatever DyDx has lent you + 2 token wei.

You only need to make sure that ETH is converted to WETH after the end of your arbitrage, using the functions I mentioned in the previous message.

Here is a video I made about Wrapped Ether.

Okay. So if we are going KybertoUniswap then we end with Weth if we borrowed Weth since uniswap deals with Weth. But going UniswapToKyber we would end in Eth. So then at the end of the below part of the contract would we Then deposit the Eth and withdraw the Weth like I put at the end of the function?

//Sell ETH on Kyber ( Sell token on kyber for Eth)
(uint expectedRate, ) = kyber.getExpectedRate(
IERC20(KYBER_ETH_ADDRESS),
dai,
address(this).balance
);
kyber.swapEtherToToken.value(address(this).balance)(
dai,
expectedRate
);

IWeth {
function deposit(Eth) external payable;
function withdraw(uint wad) external;
function balanceOf(address owner) external view returns(uint);
}

    }

No, you cant he copy paste the interface IWETH in the body of the function. It will be something like weth.deposit(amount); or weth.withdraw(amount) depending on wheter you want to go from eth to weth or from weth to eth. Make sure to watch the video I suggested in my previous message, it will help you to understand how to use Wrapped Ether :).

I still need to rewatch the Weth video, but I had a question. Is Weth not technically an erc20 token? So couldn’t you just use the kyber protocol to swapEtherToToken function to switch back from Eth to Weth?

Okay so I rewatched the Weth video and it’s making a little more since. So if traded Weth-> AMPL or something, all the code would be the same except for the obvious address changes and the kyber and uniswap function would be different for swapping etc, my point being the only snippet that would really need to be added is

weth.deposit(amount);

This would need to be added after swapping the AMPL back to Eth on kyber only, in order to swap it back to Weth to pay back the loan.

Does this sound correct?

WETH is an ERC20 token yes.
No you don’t need to use Kyber to swap between ETH and WETH.
You can already do this with the WETH token itself, as I mentioned in my previous message.
Also as I mentioned before, the order is

weth.deposit(amount) is for ETH -> WETH, so if you get ETH at the end of the arbitrage and need WETH to pay back flashloan, it seems correct yes.

In order to swap the Exact amount of ether I ended with on kyber back to Weth would it be…

Weth.deposit.value(address(this).balance

??

Thanks

Sorry for all the questions on this

sounds correct, yes.