From 4dd9abe212b2f2959942ede2ebbd01f16653a11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lz=2Esir=CE=94rthurmoney=28=29?= Date: Tue, 7 Nov 2023 18:09:30 -0800 Subject: [PATCH] adding in unit test --- contracts/token/oft/v2/NativeOFTV2.sol | 3 +-- .../token/oft/v2/fee/NativeOFTWithFee.sol | 3 +-- test/oft/v2/NativeOFTV2.test.js | 11 +++++++++++ test/oft/v2/NativeOFTWithFee.test.js | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/contracts/token/oft/v2/NativeOFTV2.sol b/contracts/token/oft/v2/NativeOFTV2.sol index 76a067b2..9e84fab3 100644 --- a/contracts/token/oft/v2/NativeOFTV2.sol +++ b/contracts/token/oft/v2/NativeOFTV2.sol @@ -112,12 +112,12 @@ contract NativeOFTV2 is OFTV2, ReentrancyGuard { } function _debitFromNative(address _from, uint _amount) internal returns (uint messageFee) { + outboundAmount += _amount; messageFee = msg.sender == _from ? _debitMsgSender(_amount) : _debitMsgFrom(_from, _amount); } function _debitMsgSender(uint _amount) internal returns (uint messageFee) { uint msgSenderBalance = balanceOf(msg.sender); - outboundAmount += _amount; if (msgSenderBalance < _amount) { require(msgSenderBalance + msg.value >= _amount, "NativeOFTV2: Insufficient msg.value"); @@ -138,7 +138,6 @@ contract NativeOFTV2 is OFTV2, ReentrancyGuard { function _debitMsgFrom(address _from, uint _amount) internal returns (uint messageFee) { uint msgFromBalance = balanceOf(_from); - outboundAmount += _amount; if (msgFromBalance < _amount) { require(msgFromBalance + msg.value >= _amount, "NativeOFTV2: Insufficient msg.value"); diff --git a/contracts/token/oft/v2/fee/NativeOFTWithFee.sol b/contracts/token/oft/v2/fee/NativeOFTWithFee.sol index 0d9cf62c..88e37ebc 100644 --- a/contracts/token/oft/v2/fee/NativeOFTWithFee.sol +++ b/contracts/token/oft/v2/fee/NativeOFTWithFee.sol @@ -83,12 +83,12 @@ contract NativeOFTWithFee is OFTWithFee, ReentrancyGuard { (amount,) = _removeDust(_amount); require(amount > 0, "NativeOFTWithFee: amount too small"); + outboundAmount += amount; messageFee = msg.sender == _from ? _debitMsgSender(amount, newMsgValue) : _debitMsgFrom(_from, amount, newMsgValue); } function _debitMsgSender(uint _amount, uint currentMsgValue) internal returns (uint messageFee) { uint msgSenderBalance = balanceOf(msg.sender); - outboundAmount += _amount; if (msgSenderBalance < _amount) { require(msgSenderBalance + currentMsgValue >= _amount, "NativeOFTWithFee: Insufficient msg.value"); @@ -110,7 +110,6 @@ contract NativeOFTWithFee is OFTWithFee, ReentrancyGuard { function _debitMsgFrom(address _from, uint _amount, uint currentMsgValue) internal returns (uint messageFee) { uint msgFromBalance = balanceOf(_from); - outboundAmount += _amount; if (msgFromBalance < _amount) { require(msgFromBalance + currentMsgValue >= _amount, "NativeOFTWithFee: Insufficient msg.value"); diff --git a/test/oft/v2/NativeOFTV2.test.js b/test/oft/v2/NativeOFTV2.test.js index 253f5a20..b991a313 100644 --- a/test/oft/v2/NativeOFTV2.test.js +++ b/test/oft/v2/NativeOFTV2.test.js @@ -86,6 +86,9 @@ describe("NativeOFTV2: ", function () { expect(await nativeOFTV2.balanceOf(nativeOFTV2.address)).to.be.equal(totalAmount) expect(await nativeOFTV2.balanceOf(owner.address)).to.be.equal(leftOverAmount) expect(await remoteOFTV2.balanceOf(owner.address)).to.be.equal(totalAmount) + expect(await nativeOFTV2.outboundAmount()).to.be.equal(totalAmount) + expect(await remoteOFTV2.totalSupply()).to.be.equal(totalAmount) + let ownerBalance2 = await ethers.provider.getBalance(owner.address) @@ -106,6 +109,8 @@ describe("NativeOFTV2: ", function () { expect(await ethers.provider.getBalance(owner.address)).to.be.equal(ownerBalance2.sub(nativeFee).sub(transFee)) expect(await nativeOFTV2.balanceOf(owner.address)).to.equal(leftOverAmount) expect(await remoteOFTV2.balanceOf(owner.address)).to.equal(0) + expect(await remoteOFTV2.totalSupply()).to.be.equal(leftOverAmount) + expect(await nativeOFTV2.outboundAmount()).to.be.equal(leftOverAmount) }) it("sendFrom() - with enough native", async function () { @@ -142,6 +147,8 @@ describe("NativeOFTV2: ", function () { expect(await nativeOFTV2.balanceOf(nativeOFTV2.address)).to.be.equal(totalAmountMinusDust) expect(await nativeOFTV2.balanceOf(owner.address)).to.be.equal(leftOverAmount) expect(await remoteOFTV2.balanceOf(owner.address)).to.be.equal(totalAmountMinusDust) + expect(await nativeOFTV2.outboundAmount()).to.be.equal(totalAmountMinusDust) + expect(await remoteOFTV2.totalSupply()).to.be.equal(totalAmountMinusDust) }) it("sendFrom() - from != sender with addition msg.value", async function () { @@ -180,6 +187,8 @@ describe("NativeOFTV2: ", function () { expect(await nativeOFTV2.balanceOf(nativeOFTV2.address)).to.be.equal(totalAmount) expect(await nativeOFTV2.balanceOf(owner.address)).to.be.equal(leftOverAmount) expect(await remoteOFTV2.balanceOf(owner.address)).to.be.equal(totalAmount) + expect(await nativeOFTV2.outboundAmount()).to.be.equal(totalAmount) + expect(await remoteOFTV2.totalSupply()).to.be.equal(totalAmount) }) it("sendFrom() - from != sender with not enough native", async function () { @@ -286,6 +295,8 @@ describe("NativeOFTV2: ", function () { // verify tokens burned on source chain and minted on destination chain expect(await nativeOFTV2.balanceOf(nativeOFTV2.address)).to.be.equal(amount) expect(await remoteOFTV2.balanceOf(owner.address)).to.be.equal(amount) + expect(await nativeOFTV2.outboundAmount()).to.be.equal(amount) + expect(await remoteOFTV2.totalSupply()).to.be.equal(amount) }) it("setMinDstGas() - when type is not set on destination chain", async function () { diff --git a/test/oft/v2/NativeOFTWithFee.test.js b/test/oft/v2/NativeOFTWithFee.test.js index 49e117f5..18e1ba61 100644 --- a/test/oft/v2/NativeOFTWithFee.test.js +++ b/test/oft/v2/NativeOFTWithFee.test.js @@ -97,6 +97,8 @@ describe("NativeOFTWithFee: ", function () { expect(await nativeOFTWithFee.balanceOf(nativeOFTWithFee.address)).to.be.equal(totalAmount) expect(await nativeOFTWithFee.balanceOf(owner.address)).to.be.equal(leftOverAmount) expect(await remoteOFTWithFee.balanceOf(owner.address)).to.be.equal(totalAmount) + expect(await nativeOFTWithFee.outboundAmount()).to.be.equal(totalAmount) + expect(await remoteOFTWithFee.totalSupply()).to.be.equal(totalAmount) let ownerBalance2 = await ethers.provider.getBalance(owner.address) @@ -119,6 +121,8 @@ describe("NativeOFTWithFee: ", function () { expect(await ethers.provider.getBalance(owner.address)).to.be.equal(ownerBalance2.sub(nativeFee).sub(transFee)) expect(await nativeOFTWithFee.balanceOf(owner.address)).to.equal(leftOverAmount) expect(await remoteOFTWithFee.balanceOf(owner.address)).to.equal(0) + expect(await nativeOFTWithFee.outboundAmount()).to.be.equal(leftOverAmount) + expect(await remoteOFTWithFee.totalSupply()).to.be.equal(leftOverAmount) }) it("sendFrom() w/ fee change - tokens from main to other chain", async function () { @@ -160,6 +164,8 @@ describe("NativeOFTWithFee: ", function () { expect(await nativeOFTWithFee.balanceOf(alice.address)).to.be.equal(leftOverAmount) expect(await nativeOFTWithFee.balanceOf(bob.address)).to.be.equal(fee) expect(await nativeOFTWithFee.balanceOf(nativeOFTWithFee.address)).to.be.equal(totalAmount.sub(fee)) + expect(await nativeOFTWithFee.outboundAmount()).to.be.equal(totalAmount.sub(fee)) + expect(await remoteOFTWithFee.totalSupply()).to.be.equal(totalAmount.sub(fee)) }) it("sendFrom() w/ fee change - tokens from main to other chain without taking dust", async function () { @@ -207,6 +213,8 @@ describe("NativeOFTWithFee: ", function () { expect(await nativeOFTWithFee.balanceOf(bob.address)).to.be.equal(fee) expect(await nativeOFTWithFee.balanceOf(nativeOFTWithFee.address)).to.be.equal(totalMintAmount) expect(await remoteOFTWithFee.balanceOf(alice.address)).to.be.equal(totalMintAmount) + expect(await nativeOFTWithFee.outboundAmount()).to.be.equal(totalMintAmount) + expect(await remoteOFTWithFee.totalSupply()).to.be.equal(totalMintAmount) }) it("sendFrom() w/ fee change - deposit before send", async function () { @@ -251,6 +259,8 @@ describe("NativeOFTWithFee: ", function () { expect(await nativeOFTWithFee.balanceOf(nativeOFTWithFee.address)).to.be.equal(0) expect(await nativeOFTWithFee.balanceOf(owner.address)).to.be.equal(depositAmount) expect(await remoteOFTWithFee.balanceOf(owner.address)).to.be.equal(0) + expect(await nativeOFTWithFee.outboundAmount()).to.be.equal(leftOverAmount) + expect(await remoteOFTWithFee.totalSupply()).to.be.equal(leftOverAmount) const aliceAddressBytes32 = ethers.utils.defaultAbiCoder.encode(["address"], [alice.address]) // estimate nativeFees @@ -272,6 +282,8 @@ describe("NativeOFTWithFee: ", function () { expect(await nativeOFTWithFee.balanceOf(alice.address)).to.be.equal(leftOverAmount) expect(await nativeOFTWithFee.balanceOf(bob.address)).to.be.equal(fee) expect(await nativeOFTWithFee.balanceOf(nativeOFTWithFee.address)).to.be.equal(totalAmount.sub(fee)) + expect(await nativeOFTWithFee.outboundAmount()).to.be.equal(totalAmount.div(2)) + expect(await remoteOFTWithFee.totalSupply()).to.be.equal(totalAmount.div(2)) }) it("quote oft fee", async function () { @@ -334,6 +346,8 @@ describe("NativeOFTWithFee: ", function () { expect(await nativeOFTWithFee.balanceOf(nativeOFTWithFee.address)).to.be.equal(totalAmountMinusDust) expect(await nativeOFTWithFee.balanceOf(owner.address)).to.be.equal(leftOverAmount) expect(await remoteOFTWithFee.balanceOf(owner.address)).to.be.equal(totalAmountMinusDust) + expect(await nativeOFTWithFee.outboundAmount()).to.be.equal(totalAmountMinusDust) + expect(await remoteOFTWithFee.totalSupply()).to.be.equal(totalAmountMinusDust) }) it("sendFrom() - from != sender with addition msg.value", async function () { @@ -373,6 +387,8 @@ describe("NativeOFTWithFee: ", function () { expect(await nativeOFTWithFee.balanceOf(nativeOFTWithFee.address)).to.be.equal(totalAmount) expect(await nativeOFTWithFee.balanceOf(owner.address)).to.be.equal(leftOverAmount) expect(await remoteOFTWithFee.balanceOf(owner.address)).to.be.equal(totalAmount) + expect(await nativeOFTWithFee.outboundAmount()).to.be.equal(totalAmount) + expect(await remoteOFTWithFee.totalSupply()).to.be.equal(totalAmount) }) it("sendFrom() - from != sender with not enough native", async function () { @@ -483,6 +499,8 @@ describe("NativeOFTWithFee: ", function () { // verify tokens burned on source chain and minted on destination chain expect(await nativeOFTWithFee.balanceOf(nativeOFTWithFee.address)).to.be.equal(amount) expect(await remoteOFTWithFee.balanceOf(owner.address)).to.be.equal(amount) + expect(await nativeOFTWithFee.outboundAmount()).to.be.equal(amount) + expect(await remoteOFTWithFee.totalSupply()).to.be.equal(amount) }) it("setMinDstGas() - when type is not set on destination chain", async function () {