Skip to content

Commit

Permalink
APEX-326 Remove TotalSupply from Nexus
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavStefanovic authored Sep 16, 2024
1 parent 6655c85 commit 752ed3e
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 161 deletions.
23 changes: 3 additions & 20 deletions contracts/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@ contract Gateway is
// Revert the transaction if the transfer fails
if (!success) revert TransferFailed();

nativeTokenPredicate.withdraw(
emit Withdraw(
_destinationChainId,
msg.sender,
_receivers,
_feeAmount,
msg.sender,
amountSum,
msg.value
amountSum
);
}

Expand All @@ -101,22 +100,6 @@ contract Gateway is
emit Deposit(_data);
}

function withdrawEvent(
uint8 _destinationChainId,
address _sender,
ReceiverWithdraw[] calldata _receivers,
uint256 _feeAmount,
uint256 _value
) external onlyPredicate {
emit Withdraw(
_destinationChainId,
_sender,
_receivers,
_feeAmount,
_value
);
}

function ttlEvent(
bytes calldata _data
) external onlyPredicate maxLengthExceeded(_data) {
Expand Down
25 changes: 0 additions & 25 deletions contracts/NativeTokenPredicate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,6 @@ contract NativeTokenPredicate is
gateway.depositEvent(_data);
}

/**
* @notice Function to withdraw tokens from the withdrawer to receiver on the destination chain
* @param _destinationChainId id of the destination chain
* @param _receivers array of ReceiverWithdraw structs on the destination chain
* @param _feeAmount amount to cover the fees
*/
function withdraw(
uint8 _destinationChainId,
ReceiverWithdraw[] calldata _receivers,
uint256 _feeAmount,
address _caller,
uint256 _amountSum,
uint256 _value
) external {
nativeTokenWallet.withdraw(_amountSum);

gateway.withdrawEvent(
_destinationChainId,
_caller,
_receivers,
_feeAmount,
_value
);
}

modifier onlyGateway() {
if (msg.sender != address(gateway)) revert NotGateway();
_;
Expand Down
13 changes: 1 addition & 12 deletions contracts/NativeTokenWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ contract NativeTokenWallet is
INativeTokenWallet
{
address public predicate;
uint256 public totalSupply;

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
Expand All @@ -38,13 +37,9 @@ contract NativeTokenWallet is
address newImplementation
) internal override onlyOwner {}

function setDependencies(
address _predicate,
uint256 _tokenSupply
) external onlyOwner {
function setDependencies(address _predicate) external onlyOwner {
if (_predicate == address(0)) revert ZeroAddress();
predicate = _predicate;
totalSupply = _tokenSupply;
}

/**
Expand All @@ -58,8 +53,6 @@ contract NativeTokenWallet is
address _account,
uint256 _amount
) external onlyPredicateOrOwner returns (bool) {
totalSupply += _amount;

(bool success, ) = _account.call{value: _amount}("");

// Revert the transaction if the transfer fails
Expand All @@ -68,10 +61,6 @@ contract NativeTokenWallet is
return true;
}

function withdraw(uint256 _amount) external override onlyPredicateOrOwner {
totalSupply -= _amount;
}

receive() external payable {}

modifier onlyPredicateOrOwner() {
Expand Down
8 changes: 0 additions & 8 deletions contracts/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,5 @@ interface IGateway is IGatewayStructs {

function depositEvent(bytes calldata data) external;

function withdrawEvent(
uint8 destinationChainId,
address sender,
ReceiverWithdraw[] calldata receivers,
uint256 feeAmount,
uint256 value
) external;

function ttlEvent(bytes calldata data) external;
}
9 changes: 0 additions & 9 deletions contracts/interfaces/INativeTokenPredicate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,4 @@ import "./IGatewayStructs.sol";

interface INativeTokenPredicate is IGatewayStructs {
function deposit(bytes calldata data, address relayer) external;

function withdraw(
uint8 _destinationChainId,
ReceiverWithdraw[] calldata _receivers,
uint256 _feeAmount,
address _caller,
uint256 _amountSum,
uint256 _value
) external;
}
7 changes: 0 additions & 7 deletions contracts/interfaces/INativeTokenWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@ interface INativeTokenWallet {
* @return bool Returns true if function call is successful
*/
function deposit(address account, uint256 amount) external returns (bool);

/**
* @notice Withdraw an amount of tokens from a particular address
* @dev Can only be called by the predicate address
* @param amount Amount of tokens to burn from the account
*/
function withdraw(uint256 amount) external;
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"@nomicfoundation/hardhat-network-helpers": "^1.0.11",
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"ethers": "^6.13.1",
"hardhat": "^2.22.8"
"hardhat": "^2.22.10"
},
"dependencies": {
"@openzeppelin/contracts": "^5.0.2",
Expand Down
9 changes: 0 additions & 9 deletions test/Gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,15 @@ describe("Gateway Contract", function () {
data
);

const totalSupplyBefore = await nativeTokenWallet.totalSupply();
const nativeTokenWalletBefore = await ethers.provider.getBalance(nativeTokenWalletAddress);

const value = { value: ethers.parseUnits("200", "wei") };
const withdrawTx = await gateway.connect(receiver).withdraw(1, receiverWithdraw, 100, value);
const withdrawReceipt = await withdrawTx.wait();
const withdrawEvent = withdrawReceipt.logs.find((log) => log.fragment && log.fragment.name === "Withdraw");

const totalSupplyAfter = await nativeTokenWallet.totalSupply();
const nativeTokenWalletAfter = await ethers.provider.getBalance(nativeTokenWalletAddress);

expect(totalSupplyAfter).to.equal(totalSupplyBefore - BigInt(200));
expect(nativeTokenWalletAfter).to.equal(nativeTokenWalletBefore + BigInt(200));

expect(withdrawEvent?.args?.destinationChainId).to.equal(1);
Expand Down Expand Up @@ -137,18 +134,15 @@ describe("Gateway Contract", function () {

const value = { value: ethers.parseUnits("200", "wei") };

const totalSupplyBefore = await nativeTokenWallet.totalSupply();
const nativeTokenWalletBefore = await ethers.provider.getBalance(nativeTokenWalletAddress);

for (let i = 0; i < 100; i++) {
const withdrawTx = await gateway.connect(receiver).withdraw(1, receiverWithdraw, 100, value);
const withdrawReceipt = await withdrawTx.wait();
const withdrawEvent = withdrawReceipt.logs.find((log) => log.fragment && log.fragment.name === "Withdraw");

let totalSupplyAfter = await nativeTokenWallet.totalSupply();
let nativeTokenWalletAfter = await ethers.provider.getBalance(nativeTokenWalletAddress);

expect(totalSupplyAfter).to.equal(totalSupplyBefore - BigInt(200 * (i + 1)));
expect(nativeTokenWalletAfter).to.equal(nativeTokenWalletBefore + BigInt(200 * (i + 1)));

expect(withdrawEvent?.args?.destinationChainId).to.equal(1);
Expand Down Expand Up @@ -196,7 +190,6 @@ describe("Gateway Contract", function () {

const value = { value: ethers.parseUnits("200", "wei") };

let totalSupplyBefore = await nativeTokenWallet.totalSupply();
let nativeTokenWalletBefore = await ethers.provider.getBalance(nativeTokenWalletAddress);

for (let i = 0; i < 100; i++) {
Expand All @@ -209,10 +202,8 @@ describe("Gateway Contract", function () {
const withdrawReceipt = await withdrawTx.wait();
const withdrawEvent = withdrawReceipt.logs.find((log) => log.fragment && log.fragment.name === "Withdraw");

let totalSupplyAfter = await nativeTokenWallet.totalSupply();
let nativeTokenWalletAfter = await ethers.provider.getBalance(nativeTokenWalletAddress);

expect(totalSupplyAfter).to.equal(totalSupplyBefore - BigInt(200 * (i + 1)));
expect(nativeTokenWalletAfter).to.equal(nativeTokenWalletBefore + BigInt(200 * (i + 1)));

expect(withdrawEvent?.args?.destinationChainId).to.equal(1);
Expand Down
25 changes: 0 additions & 25 deletions test/NativeTokenPredicate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,4 @@ describe("NativeTokenPredicate Contract", function () {

expect(depositEvent?.args?.data).to.equal(data);
});

it("Withdraw sucess", async () => {
const { owner, receiver, gateway, nativeTokenWallet, nativeTokenPredicate, receiverWithdraw } = await loadFixture(
deployGatewayFixtures
);

const randomAmount = Math.floor(Math.random() * 1000000 + 1);

await nativeTokenWallet.deposit(owner.address, randomAmount);

const gatewayContract = await impersonateAsContractAndMintFunds(await gateway.getAddress());

const withdrawTx = await nativeTokenPredicate
.connect(gatewayContract)
.withdraw(1, receiverWithdraw, 100, receiver, 200, 200);
const withdrawReceipt = await withdrawTx.wait();
const withdrawEvent = withdrawReceipt.logs.find((log) => log.fragment && log.fragment.name === "Withdraw");

expect(withdrawEvent?.args?.destinationChainId).to.equal(1);
expect(withdrawEvent?.args?.sender).to.equal(receiver);
expect(withdrawEvent?.args?.receivers[0].receiver).to.equal("something");
expect(withdrawEvent?.args?.receivers[0].amount).to.equal(100);
expect(withdrawEvent?.args?.feeAmount).to.equal(100);
expect(withdrawEvent?.args?.value).to.equal(200);
});
});
38 changes: 3 additions & 35 deletions test/NativeTokenWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("NativeTokenWallet Contract", function () {
it("SetDependencies should fail if Predicate is Zero Address", async () => {
const { owner, nativeTokenWallet } = await loadFixture(deployGatewayFixtures);

await expect(nativeTokenWallet.connect(owner).setDependencies(ethers.ZeroAddress, 0)).to.be.revertedWithCustomError(
await expect(nativeTokenWallet.connect(owner).setDependencies(ethers.ZeroAddress)).to.be.revertedWithCustomError(
nativeTokenWallet,
"ZeroAddress"
);
Expand All @@ -17,14 +17,14 @@ describe("NativeTokenWallet Contract", function () {
const { receiver, nativeTokenWallet, nativeTokenPredicate } = await loadFixture(deployGatewayFixtures);

await expect(
nativeTokenWallet.connect(receiver).setDependencies(nativeTokenPredicate.target, 0)
nativeTokenWallet.connect(receiver).setDependencies(nativeTokenPredicate.target)
).to.be.revertedWithCustomError(nativeTokenPredicate, "OwnableUnauthorizedAccount");
});

it("SetDependencies and validate initialization", async () => {
const { owner, nativeTokenWallet, nativeTokenPredicate } = await loadFixture(deployGatewayFixtures);

await expect(nativeTokenWallet.connect(owner).setDependencies(nativeTokenPredicate.target, 0)).to.not.be.reverted;
await expect(nativeTokenWallet.connect(owner).setDependencies(nativeTokenPredicate.target)).to.not.be.reverted;
expect(await nativeTokenWallet.predicate()).to.equal(nativeTokenPredicate.target);
expect(await nativeTokenWallet.owner()).to.equal(owner.address);
});
Expand All @@ -45,47 +45,15 @@ describe("NativeTokenWallet Contract", function () {

const randomAmount = Math.floor(Math.random() * 1000000 + 1);

const totalSupplyBefore = await nativeTokenWallet.totalSupply();
const receiverBalanceBefore = await ethers.provider.getBalance(receiver);
const nativeTokenWalletBefore = await ethers.provider.getBalance(nativeTokenWalletAddress);

await nativeTokenWallet.deposit(receiver.address, randomAmount);

const totalSupplyAfter = await nativeTokenWallet.totalSupply();
const receiverBalanceAfter = await ethers.provider.getBalance(receiver);
const nativeTokenWalletAfter = await ethers.provider.getBalance(nativeTokenWalletAddress);

expect(totalSupplyAfter).to.equal(totalSupplyBefore + BigInt(randomAmount));
expect(receiverBalanceAfter).to.equal(receiverBalanceBefore + BigInt(randomAmount));
expect(nativeTokenWalletAfter).to.equal(nativeTokenWalletBefore - BigInt(randomAmount));
});

it("Withdraw will fail in not called by Predicate or Owner", async function () {
const { nativeTokenWallet, receiver } = await loadFixture(deployGatewayFixtures);

const randomAmount = Math.floor(Math.random() * 1000000 + 1);

await nativeTokenWallet.deposit(receiver.address, randomAmount);

await expect(nativeTokenWallet.connect(receiver).withdraw(1)).to.be.revertedWithCustomError(
nativeTokenWallet,
"NotPredicateOrOwner"
);
});

it("Withdraw success", async function () {
const { nativeTokenWallet, owner } = await loadFixture(deployGatewayFixtures);

const randomAmount = Math.floor(Math.random() * 1000000 + 1);

await nativeTokenWallet.deposit(owner.address, randomAmount);

const totalSupplyBefore = await nativeTokenWallet.totalSupply();

await nativeTokenWallet.connect(owner).withdraw(100);

const totalSupplyAfter = await nativeTokenWallet.totalSupply();

expect(totalSupplyAfter).to.equal(totalSupplyBefore - BigInt(100));
});
});
12 changes: 6 additions & 6 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ export async function deployGatewayFixtures() {

await nativeTokenPredicate.setDependencies(gateway.target, nativeTokenWallet.target);

await nativeTokenWallet.setDependencies(nativeTokenPredicate.target, 0);
await nativeTokenWallet.setDependencies(nativeTokenPredicate.target);

const validatorsCardanoData = [
{
key: ["0x1", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish]
key: ["0x1", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish],
},
{
key: ["0x4", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish]
key: ["0x4", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish],
},
{
key: ["0x5", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish]
key: ["0x5", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish],
},
{
key: ["0x3", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish]
key: ["0x3", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish],
},
{
key: ["0x2", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish]
key: ["0x2", "0x2", "0x3", "0x4"] as [BigNumberish, BigNumberish, BigNumberish, BigNumberish],
},
];

Expand Down

0 comments on commit 752ed3e

Please sign in to comment.