DPay, is a cryptocurrency payment service that facilitates cryptocurrency transactions. In its current state, DPay takes a quantity of any ERC-20 cryptocurrency from the user, converts it to the type expected by the recipient of the payment, and sends the converted cryptocurrency to the target wallet address given by the merchant at the checkout of a transaction. Cutting out the extra steps of a user having to go to a third-party exchange and manually swap their cryptocurrency before returning to the transaction will make the shopping experience of an online retailer much more streamlined and convenient for the user.
The smart contract has two functions:
swapTokenSendToken
– takes in ERC20 tokens from the user, sends ERC20 tokens to the recipient.swapEthSendToken
– takes in ETH from the user, sends ERC20 tokens to the recipient.
The function swapTokenSendToken
has 5 parameters:
outCoin
the contract address of the coin the recipient wants.outAmount
the amount1 ofoutCoin
which the recipient is expecting.inCoin
the contract address of the coin the sender wants to spend.maxInAmount
the maximum amount1 ofinCoin
the user is willing to spend. (This could be omitted2)recipient
the address of the recipient who will recieveoutAmount
ofoutCoin
.
Note: this function is payable
and an appropriate amount of ETH (essentially maxInAmount
) must be sent to get converted into outCoin
for the recipient.
WARNING: Any surplus ETH gets refunded as WETH not ETH (see issue 19).
The function swapEthSendToken
has 3 parameters:
outCoin
the contract address of the coin the recipient wants.outAmount
the amount1 ofoutCoin
which the recipient is expecting.recipient
the address of the recipient who will recieveoutAmount
ofoutCoin
.
1 - Be careful with specifying amounts, for example in WETH9.sol
you'll see the word wad everywhere. So if inCoin
is the contract address of WETH and you want maxInAmount
to be 1 WETH you must put 1,000,000,000,000,000,000.
2 - maxInAmount
could be replaced with say
1.1*(outAmount * price_of_outCoin_denomenated_in_inCoin)
i.e. 10% over market price. (Note: the surplus gets refunded)
This contract is pretty simple and just makes testing DPay easier.
Simply a copy of the contract found on etherscan.io at the WETH9 contract address.
This is useful because;
- DPay does not yet support native ETH (only ERC20 tokens), so having the contract to hand lets you "wrap" the test ether ganache gives you to obtain WETH for testing,
- before you can send an ERC20 token the user must first call the function
approve
from the token contract.
WARNING: this section has not been updated since native ethereum support was added. However, it is left as is because it might still be helpful.
Taken from uniswap guide: "When building and testing integrations with on chain protocols, developers often hit a problem: the liquidity on the live chain is critical to thoroughly testing their code but testing against a live network like Mainnet can be extremely expensive."
- To fork the Ethereum mainnet first make an account on alchemy.com to get an API key.
- Start a new workspace on Ganache - on the server settings enable
chain forking
paste inhttps://eth-mainnet.g.alchemy.com/v2/{YOUR API KEY}
under custom url and choose a relatively recent block height (e.g. 17,000,000). - I use the Remix VSCode plugin to compile, deploy and interact with the smart contracts.
- To interact with the WETH9 contract compile its interface but don't deploy it! Enter the contract address (0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2) of the actual live WETH9 contract on mainnet next to the button
At Address
. - Compile and deploy TokenTools and DPay (you might need to change the compiler version).
- You first need to get some WETH by interacting with the WETH9 contract (use
DEPOSIT
) - Check that your account has WETH using TokenTools
getSenderBalance
which returns a list of(dai_balance, weth_balance, wbtc_balance)
. - Intereact withe the WETH9 contract using
APPROVE
,guy
should be the contract address of DPay. - Now you can spend the WETH using DPay's
swapSend
.