Skip to content

Commit

Permalink
adding in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sirarthurmoney committed Nov 8, 2023
1 parent 5c1632d commit 4dd9abe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
3 changes: 1 addition & 2 deletions contracts/token/oft/v2/NativeOFTV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions contracts/token/oft/v2/fee/NativeOFTWithFee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
11 changes: 11 additions & 0 deletions test/oft/v2/NativeOFTV2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down
18 changes: 18 additions & 0 deletions test/oft/v2/NativeOFTWithFee.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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
Expand All @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 4dd9abe

Please sign in to comment.