Skip to content

Commit

Permalink
Update method to set POL token
Browse files Browse the repository at this point in the history
  • Loading branch information
evercoinx committed Sep 8, 2024
1 parent 3a634ac commit ead090d
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 43 deletions.
19 changes: 7 additions & 12 deletions contracts/MaticX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ contract MaticX is
);
}

/**
* @dev Initializes the POL related flow.
*/
function initializePOL() external onlyRole(DEFAULT_ADMIN_ROLE) {
require(polToken != address(0), "Zero POL token address");
IERC20Upgradeable(polToken).safeApprove(
stakeManager,
type(uint256).max
);
}

/**
* @dev Sets the BOT's admin role.
* @notice Callable by the admin only.
Expand Down Expand Up @@ -709,9 +698,15 @@ contract MaticX is
override
onlyRole(DEFAULT_ADMIN_ROLE)
{
polToken = _address;
require(_address != address(0), "Zero POL token address");

polToken = _address;
emit SetPOLToken(_address);

IERC20Upgradeable(polToken).safeApprove(
stakeManager,
type(uint256).max
);
}

////////////////////////////////////////////////////////////
Expand Down
179 changes: 148 additions & 31 deletions test/MaticX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,35 @@ import {
IPolygonMigration,
IStakeManager,
MaticX,
ValidatorRegistry,
} from "../typechain";

describe("MaticX", function () {
const stakeAmount = ethers.utils.parseUnits("100", 18);

async function deployFixture() {
// EOA setups
const maticXManager = await impersonateAccount(
"0x75db63125A4f04E59A1A2Ab4aCC4FC1Cd5Daddd5"
// EOAs
const manager = await impersonateAccount(
"0x80A43dd35382C4919991C5Bca7f46Dd24Fde4C67"
);
const maticHolder = await impersonateAccount(
"0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0"
);
const fxStateRootTunnelManager = await impersonateAccount(
"0x80A43dd35382C4919991C5Bca7f46Dd24Fde4C67"
);
const [staker] = await ethers.getSigners();

// Contract setups
const validatorRegistry = await ethers.getContractAt(
// Contracts
const validatorRegistry = (await ethers.getContractAt(
"ValidatorRegistry",
"0xf556442D5B77A4B0252630E15d8BbE2160870d77"
);
"0xf556442D5B77A4B0252630E15d8BbE2160870d77",
manager
)) as ValidatorRegistry;

const fxStateRootTunnel = (await ethers.getContractAt(
"IFxStateRootTunnel",
"0x40FB804Cc07302b89EC16a9f8d040506f64dFe29",
manager
)) as IFxStateRootTunnel;

const stakeManager = (await ethers.getContractAt(
"IStakeManager",
"0x5e3Ef299fDDf15eAa0432E6e66473ace8c13D908"
Expand All @@ -54,31 +60,33 @@ describe("MaticX", function () {
"0x29e7DF7b6A1B2b07b731457f499E1696c60E2C4e"
)) as IPolygonMigration;

const fxStateRootTunnel = (await ethers.getContractAt(
"IFxStateRootTunnel",
"0x40FB804Cc07302b89EC16a9f8d040506f64dFe29",
fxStateRootTunnelManager
)) as IFxStateRootTunnel;

const MaticX = await ethers.getContractFactory("MaticX");
const maticX = (await upgrades.deployProxy(MaticX, [
validatorRegistry.address,
stakeManager.address,
matic.address,
maticXManager.address,
maticXManager.address,
manager.address,
manager.address,
])) as MaticX;

// Contract initializations
await fxStateRootTunnel
.connect(fxStateRootTunnelManager)
.setMaticX(maticX.address);
const validators = await validatorRegistry.getValidators();
const preferredDepositValidatorId = validators[0];
validatorRegistry.setPreferredDepositValidatorId(
preferredDepositValidatorId
);

const preferredWithdrawalValidatorId = validators[1];
validatorRegistry.setPreferredWithdrawalValidatorId(
preferredWithdrawalValidatorId
);

await fxStateRootTunnel.connect(manager).setMaticX(maticX.address);

await maticX
.connect(maticXManager)
.connect(manager)
.setFxStateRootTunnel(fxStateRootTunnel.address);
await maticX.connect(maticXManager).setPOLToken(pol.address);
await maticX.connect(maticXManager).initializePOL();
await maticX.connect(manager).setPOLToken(pol.address);

// ERC20 transfers
await matic.connect(maticHolder).transfer(staker.address, stakeAmount);
Expand All @@ -97,8 +105,10 @@ describe("MaticX", function () {
pol,
polygonMigration,
fxStateRootTunnel,
maticXManager,
manager,
staker,
preferredDepositValidatorId,
preferredWithdrawalValidatorId,
};
}

Expand All @@ -109,9 +119,43 @@ describe("MaticX", function () {
return await ethers.getImpersonatedSigner(address);
}

describe("Deploy the contract", function () {
describe("Submit Matic", function () {
describe("Positive", function () {
it("Should emit the Submit event for the Matic token", async function () {
it("Should emit the Submit and Delegate events", async function () {
const { maticX, matic, staker, preferredDepositValidatorId } =
await loadFixture(deployFixture);

await matic
.connect(staker)
.approve(maticX.address, stakeAmount);

const promise = maticX.connect(staker).submit(stakeAmount);
await expect(promise)
.to.emit(maticX, "Submit")
.withArgs(staker.address, stakeAmount)
.and.to.emit(maticX, "Delegate")
.withArgs(preferredDepositValidatorId, stakeAmount);
});

it("Should emit the Transfer event", async function () {
const { maticX, matic, staker } =
await loadFixture(deployFixture);

await matic
.connect(staker)
.approve(maticX.address, stakeAmount);

const promise = maticX.connect(staker).submit(stakeAmount);
await expect(promise)
.to.emit(maticX, "Transfer")
.withArgs(
ethers.constants.AddressZero,
staker.address,
stakeAmount
);
});

it("Should return the right MaticX balance", async function () {
const { maticX, matic, staker } =
await loadFixture(deployFixture);

Expand All @@ -120,21 +164,94 @@ describe("MaticX", function () {
.approve(maticX.address, stakeAmount);

const promise = maticX.connect(staker).submit(stakeAmount);
await expect(promise).to.changeTokenBalance(
maticX,
staker,
stakeAmount
);
});

it("Should the right Matic and POL balances", async function () {
const { maticX, stakeManager, matic, pol, staker } =
await loadFixture(deployFixture);

await matic
.connect(staker)
.approve(maticX.address, stakeAmount);

const promise = maticX.connect(staker).submit(stakeAmount);
await expect(promise).to.changeTokenBalances(
matic,
[staker, maticX],
[stakeAmount.mul(-1), 0]
);
await expect(promise).to.changeTokenBalances(
pol,
[stakeManager],
[stakeAmount]
);
});
});
});

describe("Submit POL", function () {
describe("Positive", function () {
it("Should emit the Submit and Delegate events", async function () {
const { maticX, pol, staker, preferredDepositValidatorId } =
await loadFixture(deployFixture);

await pol.connect(staker).approve(maticX.address, stakeAmount);

const promise = maticX.connect(staker).submitPOL(stakeAmount);
await expect(promise)
.to.emit(maticX, "Submit")
.withArgs(staker.address, stakeAmount);
.withArgs(staker.address, stakeAmount)
.and.to.emit(maticX, "Delegate")
.withArgs(preferredDepositValidatorId, stakeAmount);
});

it("Should emit the Submit event for the POL token", async function () {
it("Should emit the Transfer event", async function () {
const { maticX, pol, staker } =
await loadFixture(deployFixture);

await pol.connect(staker).approve(maticX.address, stakeAmount);

const promise = maticX.connect(staker).submitPOL(stakeAmount);
await expect(promise)
.to.emit(maticX, "Submit")
.withArgs(staker.address, stakeAmount);
.to.emit(maticX, "Transfer")
.withArgs(
ethers.constants.AddressZero,
staker.address,
stakeAmount
);
});

it("Should return the right MaticX balance", async function () {
const { maticX, pol, staker } =
await loadFixture(deployFixture);

await pol.connect(staker).approve(maticX.address, stakeAmount);

const promise = maticX.connect(staker).submitPOL(stakeAmount);
await expect(promise).to.changeTokenBalance(
maticX,
staker,
stakeAmount
);
});

it("Should the right POL balances", async function () {
const { maticX, stakeManager, pol, staker } =
await loadFixture(deployFixture);

await pol.connect(staker).approve(maticX.address, stakeAmount);

const promise = maticX.connect(staker).submitPOL(stakeAmount);
await expect(promise).to.changeTokenBalances(
pol,
[staker, maticX, stakeManager],
[stakeAmount.mul(-1), 0, stakeAmount]
);
});
});
});
Expand Down

0 comments on commit ead090d

Please sign in to comment.