-
Notifications
You must be signed in to change notification settings - Fork 10
transfer native token to sourceTokenAddress #4
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
Open
cyl19970726
wants to merge
8
commits into
QuarkChain:main
Choose a base branch
from
cyl19970726:develop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0a0e1f1
add sourceTokenAddress
cyl19970726 bfe443f
Add the function body to the getLPFee function
cyl19970726 3612af1
Add the function body to the getLPFee function
cyl19970726 140050d
modify the function body to the getLPFee function
cyl19970726 68e7593
BridgeDestination.withdraw() will support native token transfer
cyl19970726 8abe711
format code
cyl19970726 1445451
Update contracts/app/Bridge.sol
cyl19970726 bff4d07
update contracts/app/Bridge.sol
cyl19970726 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ pragma solidity ^0.8.0; | |
|
||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | ||
|
||
import "../DynamicMerkleTree.sol"; | ||
|
||
|
@@ -11,6 +12,7 @@ library BridgeLib { | |
address tokenAddress; | ||
address destination; | ||
uint256 amount; | ||
uint256 fee; | ||
uint256 startTime; | ||
uint256 feeRampup; | ||
} | ||
|
@@ -33,17 +35,24 @@ contract BridgeSource { | |
constructor() {} | ||
|
||
function withdraw( | ||
address sourceTokenAddress, | ||
BridgeLib.TransferData memory transferData, | ||
bytes32[] memory proof | ||
) public { | ||
) public payable { | ||
// safemath not needed for solidity 8 | ||
uint256 amountPlusFee = (transferData.amount * | ||
(10000 + CONTRACT_FEE_BASIS_POINTS)) / 10000; | ||
IERC20(transferData.tokenAddress).safeTransferFrom( | ||
msg.sender, | ||
address(this), | ||
amountPlusFee | ||
); | ||
|
||
if (sourceTokenAddress == address(0)) { | ||
require(msg.value >= amountPlusFee, "not enough msg.value"); | ||
payable(address(this)).transfer(amountPlusFee); | ||
} else { | ||
IERC20(sourceTokenAddress).safeTransferFrom( | ||
msg.sender, | ||
address(this), | ||
amountPlusFee | ||
); | ||
} | ||
|
||
BridgeLib.TransferInitiated memory transferInitiated = BridgeLib | ||
.TransferInitiated({data: transferData, self: address(this)}); | ||
|
@@ -61,7 +70,7 @@ contract BridgeSource { | |
} | ||
} | ||
|
||
contract BridgeDestination { | ||
contract BridgeDestination is ReentrancyGuard { | ||
using SafeERC20 for IERC20; | ||
|
||
struct TransferKey { | ||
|
@@ -99,18 +108,32 @@ contract BridgeDestination { | |
view | ||
returns (uint256) | ||
{ | ||
// TODO: | ||
return 0; | ||
uint256 currentTime = block.timestamp; | ||
if (currentTime < transferData.startTime) { | ||
return 0; | ||
} else if ( | ||
currentTime >= transferData.startTime + transferData.feeRampup | ||
) { | ||
return transferData.fee; | ||
} else { | ||
return transferData.fee * (currentTime - transferData.startTime); | ||
} | ||
} | ||
|
||
function buy(TransferKey memory tkey) public { | ||
function buy(TransferKey memory tkey) public payable nonReentrant { | ||
uint256 amount = tkey.transferData.amount - getLPFee(tkey.transferData); | ||
// TODO: another token address on dest. chain? | ||
IERC20(tkey.transferData.tokenAddress).safeTransferFrom( | ||
msg.sender, | ||
tkey.transferData.destination, | ||
amount | ||
); | ||
|
||
if (tkey.transferData.tokenAddress == address(0)) { | ||
require(msg.value >= amount, "not enough msg.value"); | ||
payable(tkey.transferData.destination).transfer(amount); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may need nonReentrant to avoid reentrancy attack? |
||
} else { | ||
// TODO: another token address on dest. chain? | ||
IERC20(tkey.transferData.tokenAddress).safeTransferFrom( | ||
msg.sender, | ||
tkey.transferData.destination, | ||
amount | ||
); | ||
} | ||
|
||
bytes32 key = keccak256(abi.encode(tkey)); | ||
require(ownerMap[key] == address(0), "already bought or withdrawn"); | ||
|
@@ -124,7 +147,7 @@ contract BridgeDestination { | |
address sourceContract, | ||
uint256 transferLen, | ||
bytes32[] memory recordProof | ||
) public { | ||
) public nonReentrant { | ||
if (!validatedStateRoots[stateRoot]) { | ||
// TODO: prove stateRoot is in stateRootProof | ||
validatedStateRoots[stateRoot] = true; | ||
|
@@ -143,16 +166,27 @@ contract BridgeDestination { | |
|
||
bytes32 key = keccak256(abi.encode(tkey)); | ||
require(ownerMap[key] != address(2**160 - 1), "already withdrawn"); | ||
if (ownerMap[key] == address(0)) { | ||
IERC20(tkey.transferData.tokenAddress).safeTransfer( | ||
tkey.transferData.destination, | ||
tkey.transferData.amount | ||
); | ||
|
||
if (tkey.transferData.tokenAddress != address(0)) { | ||
if (ownerMap[key] == address(0)) { | ||
IERC20(tkey.transferData.tokenAddress).safeTransfer( | ||
tkey.transferData.destination, | ||
tkey.transferData.amount | ||
); | ||
} else { | ||
IERC20(tkey.transferData.tokenAddress).safeTransfer( | ||
ownerMap[key], | ||
tkey.transferData.amount | ||
); | ||
} | ||
} else { | ||
IERC20(tkey.transferData.tokenAddress).safeTransfer( | ||
ownerMap[key], | ||
tkey.transferData.amount | ||
); | ||
if (ownerMap[key] == address(0)) { | ||
payable(tkey.transferData.destination).transfer( | ||
cyl19970726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tkey.transferData.amount | ||
); | ||
} else { | ||
payable(ownerMap[key]).transfer(tkey.transferData.amount); | ||
} | ||
} | ||
|
||
ownerMap[key] = address(2**160 - 1); // -1, not used any more | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
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.
Not yet fixed?