Skip to content

Commit

Permalink
Changed minting of reward tokens in L2MessageReceiver (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanProgrammer authored Jan 23, 2024
1 parent b211516 commit 7564d56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
16 changes: 1 addition & 15 deletions contracts/L2MessageReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,10 @@ contract L2MessageReceiver is ILayerZeroReceiver, IL2MessageReceiver, OwnableUpg

(address user_, uint256 amount_) = abi.decode(payload_, (address, uint256));

_mintRewardTokens(user_, amount_);
IMOR(rewardToken).mint(user_, amount_);

isNonceUsed[senderChainId_][nonce_] = true;
}

function _mintRewardTokens(address user_, uint256 amount_) private {
uint256 maxAmount_ = IMOR(rewardToken).cap() - IMOR(rewardToken).totalSupply();

if (amount_ == 0 || maxAmount_ == 0) {
return;
}

if (amount_ > maxAmount_) {
amount_ = maxAmount_;
}

IMOR(rewardToken).mint(user_, amount_);
}

function _authorizeUpgrade(address) internal view override onlyOwner {}
}
30 changes: 20 additions & 10 deletions test/L2MessageReceiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ describe('L2MessageReceiver', () => {
expect(await l2MessageReceiver.isNonceUsed(2, 5)).to.be.equal(true);
payload = ethers.AbiCoder.defaultAbiCoder().encode(['address', 'uint256'], [await SECOND.getAddress(), wei(2)]);
tx = await l2MessageReceiver.connect(THIRD).lzReceive(2, address, 6, payload);
await expect(tx).to.changeTokenBalance(mor, SECOND, wei(1));
expect(await l2MessageReceiver.isNonceUsed(2, 6)).to.be.equal(true);
payload = ethers.AbiCoder.defaultAbiCoder().encode(['address', 'uint256'], [await SECOND.getAddress(), wei(2)]);
tx = await l2MessageReceiver.connect(THIRD).lzReceive(2, address, 7, payload);
await expect(tx).to.changeTokenBalance(mor, SECOND, wei(0));
expect(await l2MessageReceiver.isNonceUsed(2, 7)).to.be.equal(true);
payload = ethers.AbiCoder.defaultAbiCoder().encode(['address', 'uint256'], [await SECOND.getAddress(), wei(0)]);
tx = await l2MessageReceiver.connect(THIRD).lzReceive(2, address, 8, payload);
await expect(tx).to.changeTokenBalance(mor, SECOND, wei(0));
expect(await l2MessageReceiver.isNonceUsed(2, 8)).to.be.equal(true);
});
it('should revert if provided wrong lzEndpoint', async () => {
await expect(l2MessageReceiver.lzReceive(0, '0x', 1, '0x')).to.be.revertedWith('L2MR: invalid gateway');
Expand All @@ -155,6 +145,26 @@ describe('L2MessageReceiver', () => {
await l2MessageReceiver.connect(THIRD).lzReceive(2, address, 5, payload);
expect(await l2MessageReceiver.failedMessages(2, address, 5)).to.eq(ethers.keccak256(payload));
});
it('should fail if provided wrong mint amount', async () => {
const address = ethers.solidityPacked(
['address', 'address'],
[await OWNER.getAddress(), await l2MessageReceiver.getAddress()],
);
const payload = ethers.AbiCoder.defaultAbiCoder().encode(
['address', 'uint256'],
[await SECOND.getAddress(), wei(100)],
);

await l2MessageReceiver.connect(THIRD).lzReceive(2, address, 5, payload);

expect(await l2MessageReceiver.failedMessages(2, address, 5)).to.eq(ZERO_BYTES32);
await l2MessageReceiver.connect(THIRD).lzReceive(2, address, 5, payload);
expect(await l2MessageReceiver.failedMessages(2, address, 5)).to.eq(ethers.keccak256(payload));

await mor.connect(SECOND).burn(wei(100));

await l2MessageReceiver.connect(THIRD).lzReceive(2, address, 5, payload);
});
});

describe('#nonblockingLzReceive', () => {
Expand Down

0 comments on commit 7564d56

Please sign in to comment.