-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(trading-proto-upgrade): PoC #6
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only one comment at this stage to understand the proposed workflow better :)
64b99db
to
9ec27e2
Compare
I suppose we can remove https://github.com/KomodoPlatform/etomic-swap/blob/trading-proto-upgrade-v2-poc/package.json#L8-L9 |
@shamardy in dm with @artemii235 we discussed that I will add nft swap V2 methods in trading proto upgrade V2 branch. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please change your tests ordering according current order in dev?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for contract methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for moving V2 ops to the new smart contract! Its better.
I have the first brief notes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more notes
@laruh All notes are fixed, please review one more time 🙂 |
Thanks for the fixes! First is to use
Second is to add The use of So you will have this: import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract EtomicSwapV2 is ReentrancyGuard {
using SafeERC20 for IERC20;
function erc20MakerPayment(
bytes32 id,
uint256 amount,
address tokenAddress,
address taker,
bytes20 takerSecretHash,
bytes20 makerSecretHash,
uint32 paymentLockTime
) external nonReentrant {
// code
}
function erc20TakerPayment(
bytes32 id,
uint256 amount,
uint256 dexFee,
address tokenAddress,
address receiver,
bytes20 takerSecretHash,
bytes20 makerSecretHash,
uint32 immediateRefundLockTime,
uint32 paymentLockTime
) external nonReentrant {
// code
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non blocking notes
await etomicSwapRunner0.erc20MakerPayment(...payment_params).should.be.rejectedWith("Maker payment is already initialized"); | ||
}); | ||
|
||
it('should allow taker to spend ETH maker payment', async function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you create etomicSwapRunners
in this test please
await etomicSwapV2.connect(accounts[1]).spendMakerPayment(...spendParams).should.be.rejectedWith(INVALID_PAYMENT_STATE_SENT); | ||
}); | ||
|
||
it('should allow taker to spend ERC20 maker payment', async function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too, use etomicSwap runners for 0 and 1 account, please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have questions about Taker / Maker
methods and their taker / maker secret
params.
contracts/EtomicSwapV2.sol
Outdated
function refundMakerPaymentTimelock( | ||
bytes32 id, | ||
uint256 amount, | ||
address taker, | ||
bytes20 takerSecretHash, | ||
bytes20 makerSecretHash, | ||
address tokenAddress | ||
) external { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q1: Why in refundMakerPaymentTimelock
function we use bytes20 takerSecretHash
parameter, but in refundMakerPaymentSecret
we provide bytes32 takerSecret
(not hash)?
Q2: and actually both refundMakerPaymentSecret
and refundTakerPaymentSecret
utilize bytes32 takerSecret
. why we dont use makerSecret
in refund methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
takerSecret
and makerSecret
are generated by Taker and Maker, respectively. takerSecret
is used for immediate refund path, while makerSecret
is used for spending path.
If timelock
refund path is used, it typically means that taker didn't reveal his secret. So, takerSecretHash
is used just to calculate paymentHash
in the function.
@laruh Could you please leave your notes in a single review? I don't really see a reason to create 3 review iterations in a day when it could be easily one. |
@laruh As per |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the changes! Next review iteration where I have some notes/questions that might optimize the gas used by the contract a bit, I guess there can be other places for optimizations that I missed.
contracts/EtomicSwapV2.sol
Outdated
msg.sender, | ||
maker, | ||
takerSecretHash, | ||
ripemd160(abi.encodePacked(sha256(abi.encodePacked(makerSecret)))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we start using sha256
only to reduce gas consumption. We already support sha256
secret hash in the defi framework.
P.S. we should start taking gas reduction into consideration before deploying the new contract ref. KomodoPlatform/komodo-defi-framework#1857 (comment) . Also, if this issue KomodoPlatform/komodo-defi-framework#1857 can be solved without additional gas cost it would be good to do it in v2 contract since we couldn't do it for backward compatibility issues before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good note, I will test a case without additional ripemd160
call and post results here later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, with ripemd160, spendMakerPayment
gas usage is 50697 (ERC-20), with 32 bytes secret hash (sha256-only): 49271. It comes with ~200 gas increase of erc20MakerPayment
, but overall it should be beneficial. I pushed the changes.
msg.sender, | ||
takerSecretHash, | ||
makerSecretHash, | ||
address(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need to make zero address part of eth payment hash? I guess if removed we would need to duplicate a lot of functions, e.g. 2 spendMakerPayment functions, one for erc20 and one for eth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, having this address(0)
allows using a single spend/refund function. So, if we remove it, we will have more boilerplate code with questionable benefit, IMHO 🙂
Hmmm I realized that adding nft swap V2 methods in |
@laruh Yeah, I propose to create a separate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Next review iteration
event TakerPaymentSent(bytes32 id); | ||
event TakerPaymentApproved(bytes32 id); | ||
event TakerPaymentSpent(bytes32 id, bytes32 secret); | ||
event TakerPaymentRefundedSecret(bytes32 id, bytes32 secret); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: you removed takerSecret from event MakerPaymentRefundedSecret
, but left it in event TakerPaymentRefundedSecret
. Is it in purpose? or you just missed it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related #6 (comment), we only need it for taker refund so that the maker can use it to refund their payment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last review from my side! Just some nits and one question/suggestion!
@shamardy Review notes are fixed 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥
if (takerPayments[id].state == TakerPaymentState.TakerApproved) { | ||
require( | ||
block.timestamp >= takerPayments[id].paymentLockTime, | ||
"Current timestamp didn't exceed payment refund lock time" | ||
); | ||
} | ||
|
||
if (takerPayments[id].state == TakerPaymentState.PaymentSent) { | ||
require( | ||
block.timestamp >= takerPayments[id].preApproveLockTime, | ||
"Current timestamp didn't exceed payment pre-approve lock time" | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ironic that after adding this change I can see that there is no actual need for timelock refund if TakerPaymentState
is PaymentSent
since refund using taker secret can work anytime when TakerPaymentState
is PaymentSent
, this at least adds a mitigation in case the taker loses their secret due to any problem which was one of the reasons we added timelocked refund for the funding transaction when designing the protocol IIRC. Just a note and not a change request :)
Implemented EtomicSwapV2 contract containing functions needed to support Trading Protocol Upgrade V2.