add conversion rate in a fallback and receive, no need for unfair conversion
Signed-off-by: Uncle Fatso <uncle.fatso@ghostchain.io>
This commit is contained in:
parent
8e9374b2d0
commit
8746bc747a
@ -20,11 +20,13 @@ contract Reserve is ERC20Permit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fallback() external payable {
|
fallback() external payable {
|
||||||
_mint(msg.sender, msg.value);
|
_accumulateDonation(msg.value);
|
||||||
|
_mint(msg.sender, msg.value * conversionRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
receive() external payable {
|
receive() external payable {
|
||||||
_mint(msg.sender, msg.value);
|
_accumulateDonation(msg.value);
|
||||||
|
_mint(msg.sender, msg.value * conversionRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeRate(uint256 rate) external {
|
function changeRate(uint256 rate) external {
|
||||||
@ -52,8 +54,7 @@ contract Reserve is ERC20Permit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function mint(address account) external payable {
|
function mint(address account) external payable {
|
||||||
uint256 donation = msg.value * donationRate / 1e5;
|
_accumulateDonation(msg.value);
|
||||||
accumulatedDonation += donation;
|
|
||||||
_mint(account, msg.value * conversionRate);
|
_mint(account, msg.value * conversionRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,4 +71,9 @@ contract Reserve is ERC20Permit {
|
|||||||
uint256 valueDiff = address(this).balance - accumulatedDonation;
|
uint256 valueDiff = address(this).balance - accumulatedDonation;
|
||||||
return amount * valueDiff / totalSupply();
|
return amount * valueDiff / totalSupply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _accumulateDonation(uint256 value) private {
|
||||||
|
uint256 donation = value * donationRate / 1e5;
|
||||||
|
accumulatedDonation += donation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,4 +270,30 @@ contract ReserveTest is Test {
|
|||||||
assertEq(reserve.accumulatedDonation(), sendAmount);
|
assertEq(reserve.accumulatedDonation(), sendAmount);
|
||||||
assertEq(address(reserve).balance, sendAmount);
|
assertEq(address(reserve).balance, sendAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_reserveFallback() public {
|
||||||
|
assertEq(address(reserve).balance, 0);
|
||||||
|
assertEq(reserve.balanceOf(aliceAddress), 0);
|
||||||
|
|
||||||
|
vm.prank(aliceAddress);
|
||||||
|
(bool success, ) = address(reserve).call(abi.encodeWithSignature("nonExistentFunction()"));
|
||||||
|
require(success, "Fallback call failed");
|
||||||
|
|
||||||
|
assertEq(address(reserve).balance, 0);
|
||||||
|
assertEq(reserve.balanceOf(aliceAddress), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_reserveReceive() public {
|
||||||
|
assertEq(address(reserve).balance, 0);
|
||||||
|
assertEq(reserve.balanceOf(aliceAddress), 0);
|
||||||
|
|
||||||
|
deal(aliceAddress, sendAmount);
|
||||||
|
vm.prank(aliceAddress);
|
||||||
|
(bool success, ) = address(reserve).call{value: sendAmount}("");
|
||||||
|
require(success, "Transfer of native failed");
|
||||||
|
|
||||||
|
uint256 estimatedReceiveAmount = sendAmount * reserve.conversionRate();
|
||||||
|
assertEq(address(reserve).balance, sendAmount);
|
||||||
|
assertEq(reserve.balanceOf(aliceAddress), estimatedReceiveAmount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user