Skip to content

Instantly share code, notes, and snippets.

@vinhjaxt
Forked from tienshaoku/swap.js
Created September 13, 2020 03:47
Show Gist options
  • Save vinhjaxt/84d722e25214db6ed3816371016d9627 to your computer and use it in GitHub Desktop.
Save vinhjaxt/84d722e25214db6ed3816371016d9627 to your computer and use it in GitHub Desktop.
function swapETHToDai() public payable returns(uint[] memory) {
// static array: address[k] memory array;
// The following is the dynamic array way of initialization
address[] memory _paths = new address[](2);
// Also, push() is for storage array.
_paths[0] = WETHAddress;
_paths[1] = DaiAddress;
return uniswapV2Router01.swapExactETHForTokens{value: msg.value}(0, _paths, msg.sender, now + 120);
}
function swapDaiForETH(uint _DaiAmount) public returns(uint[] memory) {
require(Dai.transferFrom(msg.sender, address(this), _DaiAmount));
address[] memory _paths = new address[](2);
_paths[0] = DaiAddress;
_paths[1] = WETHAddress;
Dai.approve(address(uniswapV2Router01), _DaiAmount);
return uniswapV2Router01.swapExactTokensForETH(_DaiAmount, 0, _paths, msg.sender, now + 120);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment