Created
May 19, 2020 07:03
-
-
Save tienshaoku/0d99ea04104f2e47f6b0b25d647a0d31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Author
tienshaoku
commented
Jun 16, 2020
via email
Dear Aleks,
The two versions have the same functionality.
The difference lies in the occasions.
If the instance of IUniswapV2Router01 will be used multiple times in a
smart contract, then we should declare it in the global scope as the first
approach.
By contrast, if you are sure that there will be only one function using the
instance of IUniswapV2Router01, then the second approach is for sure
cleaner.
Shao
alberentsen <[email protected]> 於 2020年6月16日 週二 下午5:50寫道:
… ***@***.**** commented on this gist.
------------------------------
I have indeed a question! Below you find to version of the same contract.
The first one I learnt from you and the second would be a shortcut but I'm
not sure whether I miss something.
pragma solidity ^0.5.0;
import "./IUniswapV2Router01.sol";
contract ExactEthForTokenV2 {
address internal constant UNISWAP_ROUTER_ADDRESS =
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
IUniswapV2Router01 public uniswaprouter;
constructor () public {
uniswaprouter = IUniswapV2Router01(UNISWAP_ROUTER_ADDRESS);
}
function swapETHToNBT() public payable {
address[] memory _paths = new address <http://2>;
_paths[0] = WETHAddress;
_paths[1] = TokenAddress;
uniswaprouter.swapExactETHForTokens.value(msg.value)(0, _paths,
address(this), now + 15);
//Question: Is there any benefit of doing it as you do:
//First, IUniswapV2Router01 public uniswaprouter;
//Second, uniswaprouter = IUniswapV2Router01(UNISWAP_ROUTER_ADDRESS);
//Third, uniswaprouter.swapExactETHForTokens.value(msg.value)(0, _paths,
address(this), now + 15);
//Is it also possible to use the Interface address directly (see the last
line below) to call swapExactETHForTokens.
pragma solidity ^0.5.0;
import "./IUniswapV2Router01.sol";
contract ExactEthForTokenV2 {
address internal constant UNISWAP_ROUTER_ADDRESS =
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
constructor () public {
}
function swapETHToNBT() public payable {
address[] memory _paths = new address <http://2>;
_paths[0] = WETHAddress;
_paths[1] = TokenAddress;
IUniswapV2Router01(UNISWAP_ROUTER_ADDRESS).swapExactETHForTokens.value(msg.value)(0,
_paths, address(this), now + 15);
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/0d99ea04104f2e47f6b0b25d647a0d31#gistcomment-3343219>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKV3KR324BM2EXFOADN6XHTRW455VANCNFSM4NUNZ4FQ>
.
Hi Shao
I figured the problem out in the meantime.
Thanks again
aleks
Hi Aleks,
Sorry for the late reply, as I have been quite busy recently.
I happen to be majoring in Economics before, now in Computer Science :)
I'm glad that you have solved it!
But, allow me to paste this:
https://github.com/Uniswap/uniswap-v1/blob/master/contracts/uniswap_exchange.vy
You can find the source code of Uniswap V1 in its GitHub, and then deploy a
copy of it on testnets so that you do not have to pay for anything!
(Though it is in another smart contract language: Vyper.
It's easier to understand than Solidity, but has far less attention and
thus less documentation on the Internet.)
Also, I think I do find the testnet address of smart contracts of Uniswap
V1:
*// mainnet const factory = '0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95' //
testnets const ropsten = '0x9c83dCE8CA20E9aAF9D3efc003b2ea62aBC08351' const
rinkeby = '0xf5D915570BC477f9B8D6C0E980aA81757A3AaC36' const kovan =
'0xD3E51Ef092B2845f10401a0159B2B96e8B6c3D30' const görli =
'0x6Ce570d02D73d4c384b46135E87f8C592A8c86dA' *
They are hidden here:
https://uniswap.org/docs/v1/frontend-integration/connect-to-uniswap/
In fact, most blockchain projects are open-source, so over 90% of the time,
you can find the source code on Github.
Additionally, there is actually a way to interact with Mainnet with some
limitations: https://studydefi.com/testing-on-mainnet/
Feel free to ask!
It's my pleasure to help more people get to know about blockchain and
Ethereum :)
I have actually written a Uniswap V2 coding tutorial, but it's in Chinese :(
If you're interested, perhaps you can try to google translate it:
https://medium.com/taipei-ethereum-meetup/uniswap-v2-implementation-and-combination-with-compound-262ff338efa
Shao
alberentsen <[email protected]> 於 2020年6月20日 週六 上午5:21寫道:
… ***@***.**** commented on this gist.
------------------------------
Hi Shao
I figured the problem out in the meantime.
Thanks again
aleks
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/0d99ea04104f2e47f6b0b25d647a0d31#gistcomment-3348021>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKV3KR5OQ53BUQQ2UPBYRA3RXPJEPANCNFSM4NUNZ4FQ>
.
Hi Shao
Thanks for all this. I will try it out!
Best wishes
aleks
From: Tien Shao Ku <[email protected]>
Sent: Sonntag, 21. Juni 2020 15:19
To: tienshaoku <[email protected]>
Cc: Aleksander Berentsen <[email protected]>; Comment <[email protected]>
Subject: Re: tienshaoku/swap.js
@tienshaoku commented on this gist.
________________________________
Hi Aleks,
Sorry for the late reply, as I have been quite busy recently.
I happen to be majoring in Economics before, now in Computer Science :)
I'm glad that you have solved it!
But, allow me to paste this:
https://github.com/Uniswap/uniswap-v1/blob/master/contracts/uniswap_exchange.vy
You can find the source code of Uniswap V1 in its GitHub, and then deploy a
copy of it on testnets so that you do not have to pay for anything!
(Though it is in another smart contract language: Vyper.
It's easier to understand than Solidity, but has far less attention and
thus less documentation on the Internet.)
Also, I think I do find the testnet address of smart contracts of Uniswap
V1:
*// mainnet const factory = '0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95' //
testnets const ropsten = '0x9c83dCE8CA20E9aAF9D3efc003b2ea62aBC08351' const
rinkeby = '0xf5D915570BC477f9B8D6C0E980aA81757A3AaC36' const kovan =
'0xD3E51Ef092B2845f10401a0159B2B96e8B6c3D30' const görli =
'0x6Ce570d02D73d4c384b46135E87f8C592A8c86dA' *
They are hidden here:
https://uniswap.org/docs/v1/frontend-integration/connect-to-uniswap/
In fact, most blockchain projects are open-source, so over 90% of the time,
you can find the source code on Github.
Additionally, there is actually a way to interact with Mainnet with some
limitations: https://studydefi.com/testing-on-mainnet/
Feel free to ask!
It's my pleasure to help more people get to know about blockchain and
Ethereum :)
I have actually written a Uniswap V2 coding tutorial, but it's in Chinese :(
If you're interested, perhaps you can try to google translate it:
https://medium.com/taipei-ethereum-meetup/uniswap-v2-implementation-and-combination-with-compound-262ff338efa
Shao
alberentsen <[email protected]<mailto:[email protected]>> 於 2020年6月20日 週六 上午5:21寫道:
***@***.**** commented on this gist.
------------------------------
Hi Shao
I figured the problem out in the meantime.
Thanks again
aleks
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/0d99ea04104f2e47f6b0b25d647a0d31#gistcomment-3348021>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKV3KR5OQ53BUQQ2UPBYRA3RXPJEPANCNFSM4NUNZ4FQ>
.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<https://gist.github.com/0d99ea04104f2e47f6b0b25d647a0d31#gistcomment-3349431>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AF5N66QAR3UNDB3ZRXZNY33RXYCD3ANCNFSM4NUNZ4FQ>.
Hi Aleks,
No problem :)
Shao
alberentsen <[email protected]> 於 2020年6月24日 週三 下午3:03寫道:
… ***@***.**** commented on this gist.
------------------------------
Hi Shao
Thanks for all this. I will try it out!
Best wishes
aleks
From: Tien Shao Ku ***@***.***>
Sent: Sonntag, 21. Juni 2020 15:19
To: tienshaoku ***@***.***>
Cc: Aleksander Berentsen ***@***.***>; Comment <
***@***.***>
Subject: Re: tienshaoku/swap.js
@tienshaoku commented on this gist.
________________________________
Hi Aleks,
Sorry for the late reply, as I have been quite busy recently.
I happen to be majoring in Economics before, now in Computer Science :)
I'm glad that you have solved it!
But, allow me to paste this:
https://github.com/Uniswap/uniswap-v1/blob/master/contracts/uniswap_exchange.vy
You can find the source code of Uniswap V1 in its GitHub, and then deploy
a
copy of it on testnets so that you do not have to pay for anything!
(Though it is in another smart contract language: Vyper.
It's easier to understand than Solidity, but has far less attention and
thus less documentation on the Internet.)
Also, I think I do find the testnet address of smart contracts of Uniswap
V1:
*// mainnet const factory = '0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95'
//
testnets const ropsten = '0x9c83dCE8CA20E9aAF9D3efc003b2ea62aBC08351'
const
rinkeby = '0xf5D915570BC477f9B8D6C0E980aA81757A3AaC36' const kovan =
'0xD3E51Ef092B2845f10401a0159B2B96e8B6c3D30' const görli =
'0x6Ce570d02D73d4c384b46135E87f8C592A8c86dA' *
They are hidden here:
https://uniswap.org/docs/v1/frontend-integration/connect-to-uniswap/
In fact, most blockchain projects are open-source, so over 90% of the
time,
you can find the source code on Github.
Additionally, there is actually a way to interact with Mainnet with some
limitations: https://studydefi.com/testing-on-mainnet/
Feel free to ask!
It's my pleasure to help more people get to know about blockchain and
Ethereum :)
I have actually written a Uniswap V2 coding tutorial, but it's in Chinese
:(
If you're interested, perhaps you can try to google translate it:
https://medium.com/taipei-ethereum-meetup/uniswap-v2-implementation-and-combination-with-compound-262ff338efa
Shao
alberentsen ***@***.******@***.***>> 於
2020年6月20日 週六 上午5:21寫道:
> ***@***.**** commented on this gist.
> ------------------------------
>
> Hi Shao
> I figured the problem out in the meantime.
> Thanks again
> aleks
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <
https://gist.github.com/0d99ea04104f2e47f6b0b25d647a0d31#gistcomment-3348021>,
> or unsubscribe
> <
https://github.com/notifications/unsubscribe-auth/AKV3KR5OQ53BUQQ2UPBYRA3RXPJEPANCNFSM4NUNZ4FQ>
> .
>
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<
https://gist.github.com/0d99ea04104f2e47f6b0b25d647a0d31#gistcomment-3349431>,
or unsubscribe<
https://github.com/notifications/unsubscribe-auth/AF5N66QAR3UNDB3ZRXZNY33RXYCD3ANCNFSM4NUNZ4FQ>.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/0d99ea04104f2e47f6b0b25d647a0d31#gistcomment-3352559>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKV3KR3Y6KE37UWA6MNCEE3RYGQLPANCNFSM4NUNZ4FQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment