Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kiseln committed May 23, 2024
1 parent 0b989fa commit 85230cf
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 72 deletions.
1 change: 1 addition & 0 deletions erc20-bridge-token/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("dotenv").config();
require("@openzeppelin/hardhat-upgrades");
require("@nomicfoundation/hardhat-verify");
require("@nomicfoundation/hardhat-chai-matchers");

const ALCHEMY_API_KEY = process.env.ALCHEMY_API_KEY;
const INFURA_API_KEY = process.env.INFURA_API_KEY;
Expand Down
3 changes: 2 additions & 1 deletion erc20-bridge-token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"rainbow-bridge-utils": "https://gitpkg.now.sh/aurora-is-near/rainbow-bridge/utils?327ff937bda3e3a99386fe488994f3c8e35f8aa0"
},
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^2.0.6",
"@nomicfoundation/hardhat-ethers": "^3.0.6",
"@nomiclabs/hardhat-etherscan": "^3.1.0",
"@nomicfoundation/hardhat-verify": "^2.0.7",
"@nomiclabs/hardhat-etherscan": "^3.1.0",
"@openzeppelin/hardhat-upgrades": "^3.1.0",
"@openzeppelin/test-helpers": "^0.5.5",
"chai": "^4.3.6",
Expand Down
92 changes: 35 additions & 57 deletions erc20-bridge-token/test/BridgeToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,27 @@ describe('BridgeToken', () => {

let BridgeTokenInstance
let BridgeTokenFactory
let ProofConsumer
let adminAccount
let user

beforeEach(async function () {
[adminAccount, user] = await ethers.getSigners()

BridgeTokenInstance = await ethers.getContractFactory('BridgeToken')
const brigeToken = await (await BridgeTokenInstance.deploy()).deployed()
const ProverMock = await (await (await ethers.getContractFactory('NearProverMock')).deploy()).deployed()
ProofConsumer = await (await (await ethers.getContractFactory('ProofConsumer')).deploy(Buffer.from('nearfuntoken', 'utf-8'), ProverMock.address, minBlockAcceptanceHeight)).deployed()
const bridgeToken = await BridgeTokenInstance.deploy()
await bridgeToken.waitForDeployment()

const proverMock = await (await ethers.getContractFactory('NearProverMock')).deploy()
await proverMock.waitForDeployment()

BridgeTokenFactory = await ethers.getContractFactory('BridgeTokenFactory')
BridgeTokenFactory = await upgrades.deployProxy(BridgeTokenFactory, [ProofConsumer.address, brigeToken.address], { initializer: 'initialize' })
await BridgeTokenFactory.deployed();
await ProofConsumer.transferOwnership(BridgeTokenFactory.address);
BridgeTokenFactory = await upgrades.deployProxy(BridgeTokenFactory, [
await bridgeToken.getAddress(),
Buffer.from('nearfuntoken', 'utf-8'),
await proverMock.getAddress(),
minBlockAcceptanceHeight
], { initializer: 'initialize' });
await BridgeTokenFactory.waitForDeployment();
})

function getProofTemplate() {
Expand Down Expand Up @@ -66,7 +72,7 @@ describe('BridgeToken', () => {
prefix: RESULT_PREFIX_LOCK,
token: nearTokenId,
amount: amountToLock,
recipient: ethers.utils.arrayify(recipientAddress),
recipient: ethers.getBytes(recipientAddress),
}
).toString("base64");
lockResultProof.outcome_proof.outcome.receipt_ids[0] = generateRandomBase58(64);
Expand All @@ -89,11 +95,7 @@ describe('BridgeToken', () => {
await createEmptyToken(nearTokenId, BridgeTokenFactory, BridgeTokenInstance)
await expect(
createEmptyToken(nearTokenId, BridgeTokenFactory, BridgeTokenInstance)

)
.to
.be
.revertedWith('ERR_TOKEN_EXIST')
).to.be.revertedWith('ERR_TOKEN_EXIST')
})

it("can update token's metadata", async function() {
Expand All @@ -113,21 +115,15 @@ describe('BridgeToken', () => {

await expect(
BridgeTokenFactory.setMetadata('non-existing', 'Circle USDC', 'USDC')
)
.to
.be
.revertedWith('ERR_NOT_BRIDGE_TOKEN');
).to.be.revertedWith('ERR_NOT_BRIDGE_TOKEN');
})

it('can\'t update metadata as a normal user', async function () {
await createEmptyToken(nearTokenId, BridgeTokenFactory, BridgeTokenInstance)

await expect(
BridgeTokenFactory.connect(user).setMetadata(nearTokenId, 'Circle USDC', 'USDC')
)
.to
.be
.revertedWith('AccessControlUnauthorizedAccount');
).to.be.revertedWithCustomError(BridgeTokenFactory, 'AccessControlUnauthorizedAccount');
})

it('deposit token', async function () {
Expand All @@ -145,7 +141,7 @@ describe('BridgeToken', () => {
prefix: RESULT_PREFIX_LOCK,
token: nearTokenId,
amount: amountToTransfer,
recipient: ethers.utils.arrayify(user.address),
recipient: ethers.getBytes(user.address),
}
)
.toString('base64');
Expand Down Expand Up @@ -179,7 +175,7 @@ describe('BridgeToken', () => {
prefix: RESULT_PREFIX_LOCK,
token: nearTokenId,
amount: amountToTransfer,
recipient: ethers.utils.arrayify(adminAccount.address),
recipient: ethers.getBytes(adminAccount.address),
}).toString('base64');
lockResultProof.outcome_proof.outcome.receipt_ids[0] = 'C'.repeat(44);
await expect (
Expand All @@ -206,7 +202,7 @@ describe('BridgeToken', () => {
prefix: RESULT_PREFIX_LOCK,
token: nearTokenId,
amount: amountToTransfer,
recipient: ethers.utils.arrayify(user.address),
recipient: ethers.getBytes(user.address),
}).toString('base64');
lockResultProof.outcome_proof.outcome.receipt_ids[0] = 'D'.repeat(44);

Expand Down Expand Up @@ -246,7 +242,7 @@ describe('BridgeToken', () => {
prefix: RESULT_PREFIX_LOCK,
token: nearTokenId,
amount: amountToTransfer,
recipient: ethers.utils.arrayify(user.address),
recipient: ethers.getBytes(user.address),
}).toString('base64');
lockResultProof.outcome_proof.outcome.receipt_ids[0] = 'F'.repeat(44);

Expand Down Expand Up @@ -279,7 +275,7 @@ describe('BridgeToken', () => {
prefix: RESULT_PREFIX_LOCK,
token: nearTokenId,
amount: amountToTransfer,
recipient: ethers.utils.arrayify(user.address),
recipient: ethers.getBytes(user.address),
}).toString('base64');
lockResultProof.outcome_proof.outcome.receipt_ids[0] = 'G'.repeat(44);

Expand Down Expand Up @@ -324,17 +320,18 @@ describe('BridgeToken', () => {
prefix: RESULT_PREFIX_LOCK,
token: nearTokenId,
amount: amountToTransfer,
recipient: ethers.utils.arrayify(user.address),
recipient: ethers.getBytes(user.address),
}).toString('base64');
lockResultProof.outcome_proof.outcome.receipt_ids[0] = 'B'.repeat(44);
await BridgeTokenFactory.deposit(borshifyOutcomeProof(lockResultProof), proofBlockHeight);

expect((await token.balanceOf(user.address)).toString()).to.be.equal(amountToTransfer.toString())

const BridgeTokenV2Instance = await ethers.getContractFactory("TestBridgeToken");
const BridgeTokenV2 = await (await BridgeTokenV2Instance.deploy()).deployed();
const BridgeTokenV2 = await BridgeTokenV2Instance.deploy();
await BridgeTokenV2.waitForDeployment();

await BridgeTokenFactory.upgradeToken(nearTokenId, BridgeTokenV2.address)
await BridgeTokenFactory.upgradeToken(nearTokenId, await BridgeTokenV2.getAddress())
const BridgeTokenV2Proxied = BridgeTokenV2Instance.attach(tokenProxyAddress)
expect(await BridgeTokenV2Proxied.returnTestString()).to.equal('test')
expect(await BridgeTokenV2Proxied.name()).to.equal('NEAR ERC20')
Expand All @@ -351,20 +348,19 @@ describe('BridgeToken', () => {
prefix: RESULT_PREFIX_LOCK,
token: nearTokenId,
amount: amountToTransfer,
recipient: ethers.utils.arrayify(user.address),
recipient: ethers.getBytes(user.address),
}).toString('base64');
lockResultProof.outcome_proof.outcome.receipt_ids[0] = 'C'.repeat(44);
await BridgeTokenFactory.deposit(borshifyOutcomeProof(lockResultProof), proofBlockHeight);

expect((await token.balanceOf(user.address)).toString()).to.be.equal(amountToTransfer.toString())

const BridgeTokenV2Instance = await ethers.getContractFactory("TestBridgeToken");
const BridgeTokenV2 = await (await BridgeTokenV2Instance.deploy()).deployed();
const BridgeTokenV2 = await BridgeTokenV2Instance.deploy();
await BridgeTokenV2.waitForDeployment();

await expect(BridgeTokenFactory.connect(user).upgradeToken(nearTokenId, BridgeTokenV2.address))
.to
.be
.revertedWith('AccessControlUnauthorizedAccount');
await expect(BridgeTokenFactory.connect(user).upgradeToken(nearTokenId, await BridgeTokenV2.getAddress()))
.to.be.revertedWithCustomError(BridgeTokenFactory, 'AccessControlUnauthorizedAccount');
})

it('Test selective pause', async function () {
Expand Down Expand Up @@ -447,24 +443,6 @@ describe('BridgeToken', () => {
expect(await BridgeTokenFactory.paused(PauseMode.PausedWithdraw)).to.be.equal(true);
})

it("Test setProofConsumer ", async function() {
expect(
(await BridgeTokenFactory.proofConsumerAddress()).toLowerCase()
)
.to
.be
.equal(ProofConsumer.address.toLowerCase());

const newProofConsumerAddress = "0x0123456789abcdefdeadbeef0123456789abcdef";
await BridgeTokenFactory.setProofConsumer(newProofConsumerAddress);
expect(
(await BridgeTokenFactory.proofConsumerAddress()).toLowerCase()
)
.to
.be
.equal(newProofConsumerAddress);
});

it("Test grant admin role", async function() {
await BridgeTokenFactory.connect(adminAccount).disableWhitelistMode();
expect(await BridgeTokenFactory.isWhitelistModeEnabled()).to.be.false;
Expand All @@ -477,12 +455,12 @@ describe('BridgeToken', () => {
const DEFAULT_ADMIN_ROLE = "0x0000000000000000000000000000000000000000000000000000000000000000";
await expect(
BridgeTokenFactory.connect(newAdminAccount).disableWhitelistMode()
).to.be.revertedWith('AccessControlUnauthorizedAccount');
).to.be.revertedWithCustomError(BridgeTokenFactory, 'AccessControlUnauthorizedAccount');
expect(await BridgeTokenFactory.isWhitelistModeEnabled()).to.be.true;

await expect(
BridgeTokenFactory.connect(newAdminAccount).enableWhitelistMode()
).to.be.revertedWith('AccessControlUnauthorizedAccount');
).to.be.revertedWithCustomError(BridgeTokenFactory, 'AccessControlUnauthorizedAccount');
expect(await BridgeTokenFactory.isWhitelistModeEnabled()).to.be.true;

// Grant DEFAULT_ADMIN_ROLE to newAdminAccount
Expand Down Expand Up @@ -522,12 +500,12 @@ describe('BridgeToken', () => {
// Check tx reverted on call from revoked adminAccount
await expect(
BridgeTokenFactory.connect(adminAccount).disableWhitelistMode()
).to.be.revertedWith('AccessControlUnauthorizedAccount');
).to.be.revertedWithCustomError(BridgeTokenFactory, 'AccessControlUnauthorizedAccount');
expect(await BridgeTokenFactory.isWhitelistModeEnabled()).to.be.true;

await expect(
BridgeTokenFactory.connect(adminAccount).enableWhitelistMode()
).to.be.revertedWith('AccessControlUnauthorizedAccount');
).to.be.revertedWithCustomError(BridgeTokenFactory, 'AccessControlUnauthorizedAccount');
expect(await BridgeTokenFactory.isWhitelistModeEnabled()).to.be.true;

// Check newAdminAccount can perform admin calls
Expand Down
2 changes: 1 addition & 1 deletion erc20-bridge-token/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const generateRandomBase58 = (rawSize) => {
rawInput += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
}

return ethers.utils.base58.encode(rawInput);
return ethers.encodeBase58(rawInput);
}

module.exports = { SCHEMA, createEmptyToken, createDefaultERC20Metadata, generateRandomBase58, ADMIN_ROLE, RESULT_PREFIX_LOCK, RESULT_PREFIX_METADATA };
Loading

0 comments on commit 85230cf

Please sign in to comment.