Skip to content

Commit

Permalink
refactor to use require statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Junior authored and Mauricio Junior committed Apr 18, 2023
1 parent 9376d32 commit 4d8bdc7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 50 deletions.
34 changes: 18 additions & 16 deletions contracts/v-testnet-launch/GrtLiquidityWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable {
}

function withdrawNative(uint256 amount) external onlyOwner returns (bool) {
if(address(this).balance < amount)
revert InsufficientBalance();
require(
address(this).balance >= amount,
"Grindery wallet: insufficient balance."
);
(bool sent, ) = msg.sender.call{value: amount}("");
if(!sent)
revert FailedToSendNativeTokens();
require(sent, "Grindery wallet: failed to send native tokens.");
return sent;
}

Expand All @@ -57,10 +58,10 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable {
address to,
uint256 amount
) external returns (bool) {
if(msg.sender != owner())
revert NotAllowedToPayTheOffer();
if(msg.sender == _bot)
revert NotAllowedToPayTheOffer();
require(
msg.sender == owner() || msg.sender == _bot,
"Grindery wallet: not allowed to pay the offer."
);
IERC20(token).safeTransfer(to, amount);
emit LogOfferPaid(offerId, token, to, amount);
return true;
Expand All @@ -71,15 +72,16 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable {
address to,
uint256 amount
) external returns (bool) {
if(msg.sender != owner())
revert NotAllowedToPayTheOffer();
if(msg.sender == _bot)
revert NotAllowedToPayTheOffer();
if(address(this).balance > amount)
revert InsufficientBalance();
require(
msg.sender == owner() || msg.sender == _bot,
"Grindery wallet: not allowed to pay the offer."
);
require(
address(this).balance >= amount,
"Grindery wallet: insufficient balance."
);
(bool sent, ) = to.call{value: amount}("");
if(!sent)
revert FailedToSendNativeTokens();
require(sent, "Grindery wallet: failed to send native tokens.");
emit LogOfferPaid(offerId, address(0), to, amount);
return true;
}
Expand Down
18 changes: 12 additions & 6 deletions contracts/v-testnet-launch/GrtOffer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ contract GrtOffer is GrtOfferUtils {
error ZeroAddressNotAllowed();

modifier isOwner(Offer memory offer) {
if(msg.sender != offer.user)
revert NotAllowedToModifyOffer();
require(
msg.sender == offer.user,
"Grindery offer: you are not allowed to modify this offer."
);
_;
}

Expand All @@ -41,8 +43,10 @@ contract GrtOffer is GrtOfferUtils {
}

function setTokenOffer(bytes32 offerId, address token) external isOwner(_offers[offerId]) {
if(token == address(0))
revert ZeroAddressNotAllowed();
require(
msg.sender == _offers[offerId].user,
"Grindery offer: you are not allowed to modify this offer."
);
Offer storage offer = _offers[offerId];
offer.token = token;
emit LogSetTokenOffer(offerId, token);
Expand Down Expand Up @@ -80,8 +84,10 @@ contract GrtOffer is GrtOfferUtils {
bytes calldata minPriceLimit,
bytes calldata maxPriceLimit
) external returns (bytes32) {
if(msg.sender == address(0))
revert ZeroAddressNotAllowed();
require(
msg.sender != address(0),
"Grindery offer: setOffer from zero address is not allowed"
);
bytes32 offerId = keccak256(
abi.encodePacked(msg.sender, _noncesOffer[msg.sender])
);
Expand Down
26 changes: 13 additions & 13 deletions contracts/v-testnet-launch/GrtPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ contract GrtPool is OwnableUpgradeable, GrtOffer, UUPSUpgradeable {

function _authorizeUpgrade(address) internal override onlyOwner onlyProxy {}

error TransferedAmountMustBePositive();
error ZeroAddressIsNotAllowed();
error OfferInactive();
error FailedToSendNativeTokens();

function depositETHAndAcceptOffer(
bytes32 offerId,
address destAddr
) external payable returns (bytes32) {
if(msg.value <= 0)
revert TransferedAmountMustBePositive();
if(destAddr == address(0))
revert ZeroAddressIsNotAllowed();
if(!_offers[offerId].isActive)
revert OfferInactive();
require(
msg.value > 0,
"Grindery Pool: transfered amount must be positive."
);
require(
destAddr != address(0),
"Grindery Pool: zero address as destination address is not allowed."
);
require(
_offers[offerId].isActive,
"Grindery Pool: the offer is inactive."
);
(bool sent, ) = address(this).call{value: msg.value}("");
if(!sent)
revert FailedToSendNativeTokens();
require(sent, "Grindery Pool: failed to send native tokens.");
bytes32 tradeId = keccak256(
abi.encodePacked(msg.sender, _noncesDeposit[msg.sender])
);
Expand Down
6 changes: 3 additions & 3 deletions test/v-testnet-launch/GrtLiquidityWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe("Grindery Liquidity Wallet", () => {
grtLiquidityWallet
.connect(owner)
.withdrawNative(ethers.utils.parseEther("200"))
).to.be.revertedWithCustomError(grtLiquidityWallet, "InsufficientBalance");
).to.be.revertedWith("Grindery wallet: insufficient balance.");
});
});

Expand All @@ -243,7 +243,7 @@ describe("Grindery Liquidity Wallet", () => {
owner.address,
100
)
).to.be.revertedWithCustomError(grtLiquidityWallet, "NotAllowedToPayTheOffer");
).to.be.revertedWith("Grindery wallet: not allowed to pay the offer.");
});

it("Should transfer ERC20 funds to user", async () => {
Expand Down Expand Up @@ -281,7 +281,7 @@ describe("Grindery Liquidity Wallet", () => {
grtLiquidityWallet
.connect(user2)
.payOfferWithNativeTokens(offerId, owner.address, 100)
).to.be.revertedWithCustomError(grtLiquidityWallet, "NotAllowedToPayTheOffer");
).to.be.revertedWith("Grindery wallet: not allowed to pay the offer.");
});

it("Should transfer Native funds to user", async () => {
Expand Down
12 changes: 5 additions & 7 deletions test/v-testnet-launch/GrtOffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ describe("Grindery Offer testings", function () {
it("Should fail if the sender is not the creator of the offer", async function () {
await expect(
grtOffer.connect(user2).setChainIdOffer(offerId, 34)
).to.be.revertedWithCustomError(grtOffer,
"NotAllowedToModifyOffer"
);
).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.")
});

it("Should modify the chainID", async function () {
Expand All @@ -236,7 +234,7 @@ describe("Grindery Offer testings", function () {
it("Should fail if the sender is not the creator of the offer", async function () {
await expect(
grtOffer.connect(user2).setTokenOffer(offerId, token1.address)
).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer")
).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.")
});

it("Should modify the token address", async function () {
Expand Down Expand Up @@ -267,7 +265,7 @@ describe("Grindery Offer testings", function () {
["FIRA", "50"]
)
)
).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer")
).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.")
});

it("Should modify the Min price limit options", async function () {
Expand Down Expand Up @@ -337,7 +335,7 @@ describe("Grindery Offer testings", function () {
["FIRA", "2000"]
)
)
).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer")
).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.")
});

it("Should modify the Max price limit options", async function () {
Expand Down Expand Up @@ -399,7 +397,7 @@ describe("Grindery Offer testings", function () {
it("Should fail if the sender is not the creator of the offer", async function () {
await expect(
grtOffer.connect(user2).setIsActive(offerId, false)
).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer")
).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.")
});

it("Should modify the status", async function () {
Expand Down
9 changes: 4 additions & 5 deletions test/v-testnet-launch/GrtPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe("Grindery Offer testings", function () {
.depositETHAndAcceptOffer(offerId, user3.address, {
value: 0,
})
).to.be.revertedWithCustomError(grtPool,"TransferedAmountMustBePositive");
).to.be.revertedWith("Grindery Pool: transfered amount must be positive.");
});

it("Should fail if the offer is inactive", async function () {
Expand All @@ -87,7 +87,7 @@ describe("Grindery Offer testings", function () {
.depositETHAndAcceptOffer(offerId, user3.address, {
value: 100,
})
).to.be.revertedWithCustomError(grtPool, "OfferInactive");
).to.be.revertedWith("Grindery Pool: the offer is inactive.");
});

it("Should fail if the destination address is the zero address", async function () {
Expand All @@ -98,9 +98,8 @@ describe("Grindery Offer testings", function () {
.depositETHAndAcceptOffer(offerId, ethers.constants.AddressZero, {
value: 100,
})
).to.be.revertedWithCustomError(
grtPool,
"ZeroAddressIsNotAllowed"
).to.be.revertedWith(
"Grindery Pool: zero address as destination address is not allowed."
);
});

Expand Down

0 comments on commit 4d8bdc7

Please sign in to comment.