Skip to content

Commit

Permalink
Refactor contract interaction to use instance directly instead of get…
Browse files Browse the repository at this point in the history
…Address
  • Loading branch information
yavrsky committed Nov 13, 2024
1 parent 7feb173 commit 9a0e9a0
Show file tree
Hide file tree
Showing 40 changed files with 1,813 additions and 1,783 deletions.
4 changes: 2 additions & 2 deletions contracts/test/MessageProxyForSchainTester.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface IMessageProxyForSchainTester {
}


contract MessageProxyForSchainTester is MessageProxyForSchain, IMessageProxyForSchainTester {
contract MessageProxyForSchainTester is MessageProxyForSchain, IMessageProxyForSchainTester {

IEtherbaseUpgradeable public etherbase = ETHERBASE;

Expand Down Expand Up @@ -82,4 +82,4 @@ contract MessageProxyForSchainTester is MessageProxyForSchain, IMessageProxyForS
function _getEtherbase() internal view override returns (IEtherbaseUpgradeable) {
return etherbase;
}
}
}
18 changes: 9 additions & 9 deletions test/CommunityLocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ describe("CommunityLocker", () => {

beforeEach(async () => {
messages = await deployMessages();
fakeCommunityPool = await messages.getAddress();
fakeCommunityPool = user.address;
const messageProxyForSchainWithoutSignatureFactory = await ethers.getContractFactory("MessageProxyForSchainWithoutSignature");
messageProxyForSchain = await messageProxyForSchainWithoutSignatureFactory.deploy("MyChain") as MessageProxyForSchainWithoutSignature;
tokenManagerLinker = await deployTokenManagerLinker(messageProxyForSchain, deployer.address);
communityLocker = await deployCommunityLocker(schainName, await messageProxyForSchain.getAddress(), tokenManagerLinker, fakeCommunityPool);
communityLocker = await deployCommunityLocker(schainName, messageProxyForSchain, tokenManagerLinker, fakeCommunityPool);
})

it("should activate user", async () => {
Expand All @@ -79,29 +79,29 @@ describe("CommunityLocker", () => {
await communityLocker.postMessage(mainnetHash, fakeCommunityPool, data)
.should.be.eventually.rejectedWith("Sender is not a message proxy");

await messageProxyForSchain.postMessage(await communityLocker.getAddress(), mainnetHash, deployer.address, data)
await messageProxyForSchain.postMessage(communityLocker, mainnetHash, deployer.address, data)
.should.be.eventually.rejectedWith("Sender must be CommunityPool");

await messageProxyForSchain.postMessage(await communityLocker.getAddress(), schainHash, fakeCommunityPool, data)
await messageProxyForSchain.postMessage(communityLocker, schainHash, fakeCommunityPool, data)
.should.be.eventually.rejectedWith("Source chain name must be Mainnet");

await messageProxyForSchain.postMessage(await communityLocker.getAddress(), mainnetHash, fakeCommunityPool, fakeData)
await messageProxyForSchain.postMessage(communityLocker, mainnetHash, fakeCommunityPool, fakeData)
.should.be.eventually.rejectedWith("The message should contain a status of user");

expect(await communityLocker.activeUsers(deployer.address)).to.be.equal(false);

await messageProxyForSchain.postMessage(await communityLocker.getAddress(), mainnetHash, fakeCommunityPool, data);
await messageProxyForSchain.postMessage(await communityLocker.getAddress(), mainnetHash, fakeCommunityPool, data)
await messageProxyForSchain.postMessage(communityLocker, mainnetHash, fakeCommunityPool, data);
await messageProxyForSchain.postMessage(communityLocker, mainnetHash, fakeCommunityPool, data)
.should.be.eventually.rejectedWith("Active user statuses must be different");
expect(await communityLocker.activeUsers(deployer.address)).to.be.equal(true);
});

it("should activate and then lock user", async () => {
const activateData = await messages.encodeActivateUserMessage(user.address);
const lockData = await messages.encodeLockUserMessage(user.address);
await messageProxyForSchain.postMessage(await communityLocker.getAddress(), mainnetHash, fakeCommunityPool, activateData);
await messageProxyForSchain.postMessage(communityLocker, mainnetHash, fakeCommunityPool, activateData);
expect(await communityLocker.activeUsers(user.address)).to.be.equal(true);
await messageProxyForSchain.postMessage(await communityLocker.getAddress(), mainnetHash, fakeCommunityPool, lockData);
await messageProxyForSchain.postMessage(communityLocker, mainnetHash, fakeCommunityPool, lockData);
expect(await communityLocker.activeUsers(user.address)).to.be.equal(false);
});

Expand Down
22 changes: 11 additions & 11 deletions test/CommunityPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,26 @@ describe("CommunityPool", () => {
it("should not allow to withdraw from user wallet if CommunityPool is not registered for all chains", async () => {
const extraContractRegistrarRole = await messageProxy.EXTRA_CONTRACT_REGISTRAR_ROLE();
await messageProxy.grantRole(extraContractRegistrarRole, deployer.address);
await messageProxy.registerExtraContractForAll(await communityPool.getAddress());
await messageProxy.registerExtraContractForAll(communityPool);
const tx = await messageProxy.addConnectedChain(schainName);
const wei = BigInt(minTransactionGas) * tx.gasPrice;
await communityPool.connect(user).rechargeUserWallet(schainName, user.address, { value: wei.toString() });
await messageProxy.removeExtraContractForAll(await communityPool.getAddress());
await messageProxy.removeExtraContractForAll(communityPool);
await communityPool.connect(user).withdrawFunds(schainName, wei.toString())
.should.be.eventually.rejectedWith("Sender contract is not registered");
});

describe("when chain connected and contract registered", async () => {
let gasPrice: BigNumberish;
beforeEach(async () => {
await messageProxy.registerExtraContract(schainName, await communityPool.getAddress());
await messageProxy.registerExtraContract(schainName, communityPool);
gasPrice = ((await messageProxy.addConnectedChain(schainName)).gasPrice) as BigNumberish;
});

it("should not allow to withdraw from user wallet if CommunityPool is not registered", async () => {
const amount = BigInt(minTransactionGas) * BigInt(gasPrice);
await communityPool.connect(user).rechargeUserWallet(schainName, user.address, { value: amount.toString() });
await messageProxy.removeExtraContract(schainName, await communityPool.getAddress());
await messageProxy.removeExtraContract(schainName, communityPool);
await communityPool.connect(user).withdrawFunds(schainName, amount.toString())
.should.be.eventually.rejectedWith("Sender contract is not registered");
});
Expand Down Expand Up @@ -232,7 +232,7 @@ describe("CommunityPool", () => {

await communityPool.addSchainContract(schainName2, mockContractOnSchain);
for (const schain of [schainName, schainName2]) {
await messageProxy.registerExtraContract(schain, await communityPool.getAddress());
await messageProxy.registerExtraContract(schain, communityPool);
await messageProxy.addConnectedChain(schain);

}
Expand All @@ -248,12 +248,12 @@ describe("CommunityPool", () => {

await expect(res1)
.to.emit(messageProxy, "OutgoingMessage")
.withArgs(schainHash, 0, await communityPool.getAddress(), mockContractOnSchain, activateUserData);
.withArgs(schainHash, 0, communityPool, mockContractOnSchain, activateUserData);


await expect(res2)
.to.emit(messageProxy, "OutgoingMessage")
.withArgs(schainHash2, 0, await communityPool.getAddress(), mockContractOnSchain, activateUserData);
.withArgs(schainHash2, 0, communityPool, mockContractOnSchain, activateUserData);

const res3 = await communityPool.connect(user).rechargeUserWallet(
schainName,
Expand Down Expand Up @@ -321,7 +321,7 @@ describe("CommunityPool", () => {

it("should be rejected with Node address must be set", async () => {
const tx = await messageProxyTester.addConnectedChain(schainNameRGBU);
await messageProxyTester.registerExtraContract(schainNameRGBU, await communityPoolTester.getAddress());
await messageProxyTester.registerExtraContract(schainNameRGBU, communityPoolTester);
const gasPrice = tx.gasPrice as BigNumberish;
const wei = BigInt(minTransactionGas) * BigInt(gasPrice) * 2n;
await communityPoolTester.connect(user).rechargeUserWallet(schainNameRGBU, user.address, { value: wei.toString() });
Expand All @@ -332,7 +332,7 @@ describe("CommunityPool", () => {
it("should refund node", async () => {
const balanceBefore = await getBalance(node.address);
const tx = await messageProxyTester.addConnectedChain(schainNameRGBU);
await messageProxyTester.registerExtraContract(schainNameRGBU, await communityPoolTester.getAddress());
await messageProxyTester.registerExtraContract(schainNameRGBU, communityPoolTester);
const gasPrice = tx.gasPrice as BigNumberish;
const wei = BigInt(minTransactionGas) * BigInt(gasPrice) * 2n;
await communityPoolTester.connect(user).rechargeUserWallet(schainNameRGBU, user.address, { value: wei.toString() });
Expand All @@ -344,7 +344,7 @@ describe("CommunityPool", () => {

it("should lock user", async () => {
const tx = await messageProxyTester.addConnectedChain(schainNameRGBU);
await messageProxyTester.registerExtraContract(schainNameRGBU, await communityPoolTester.getAddress());
await messageProxyTester.registerExtraContract(schainNameRGBU, communityPoolTester);
const gasPrice = tx.gasPrice as BigNumberish;
const wei = BigInt(minTransactionGas) * BigInt(gasPrice);
expect(await communityPoolTester.activeUsers(user.address, schainHashRGBU)).to.be.false;
Expand All @@ -356,7 +356,7 @@ describe("CommunityPool", () => {

it("should lock user with extra low balance", async () => {
const tx = await messageProxyTester.addConnectedChain(schainNameRGBU);
await messageProxyTester.registerExtraContract(schainNameRGBU, await communityPoolTester.getAddress());
await messageProxyTester.registerExtraContract(schainNameRGBU, communityPoolTester);
const gasPrice = tx.gasPrice as BigNumberish;
const wei = BigInt(minTransactionGas) * BigInt(gasPrice);
const gasPriceDuringGasSpikes = BigInt(gasPrice) * 2n;
Expand Down
Loading

0 comments on commit 9a0e9a0

Please sign in to comment.