github.com/MetalBlockchain/subnet-evm@v0.4.9/contract-examples/test/contract_deployer_allow_list.ts (about) 1 // (c) 2019-2022, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; 5 import { expect } from "chai"; 6 import { 7 Contract, 8 ContractFactory, 9 } from "ethers" 10 import { ethers } from "hardhat" 11 12 // make sure this is always an admin for minter precompile 13 const adminAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC" 14 const ALLOWLIST_ADDRESS = "0x0200000000000000000000000000000000000000"; 15 16 const ROLES = { 17 NONE: 0, 18 DEPLOYER: 1, 19 ADMIN: 2 20 }; 21 22 describe("ExampleDeployerList", function () { 23 let owner: SignerWithAddress 24 let contract: Contract 25 let deployer: SignerWithAddress 26 before(async function () { 27 owner = await ethers.getSigner(adminAddress); 28 const contractF: ContractFactory = await ethers.getContractFactory("ExampleDeployerList", { signer: owner }) 29 contract = await contractF.deploy() 30 await contract.deployed() 31 const contractAddress: string = contract.address 32 console.log(`Contract deployed to: ${contractAddress}`) 33 34 const signers: SignerWithAddress[] = await ethers.getSigners() 35 deployer = signers.slice(-1)[0] 36 37 // Fund deployer address 38 await owner.sendTransaction({ 39 to: deployer.address, 40 value: ethers.utils.parseEther("1") 41 }) 42 43 }); 44 45 it("should add contract deployer as owner", async function () { 46 const contractOwnerAddr: string = await contract.owner() 47 expect(owner.address).to.equal(contractOwnerAddr) 48 }); 49 50 it("precompile should see owner address has admin role", async function () { 51 // test precompile first 52 const allowList = await ethers.getContractAt("IAllowList", ALLOWLIST_ADDRESS, owner); 53 let adminRole = await allowList.readAllowList(owner.address); 54 expect(adminRole).to.be.equal(ROLES.ADMIN) 55 }); 56 57 it("precompile should see test address has no role", async function () { 58 // test precompile first 59 const allowList = await ethers.getContractAt("IAllowList", ALLOWLIST_ADDRESS, owner); 60 let noRole = await allowList.readAllowList(deployer.address); 61 expect(noRole).to.be.equal(ROLES.NONE) 62 }); 63 64 it("contract should report test address has no admin role", async function () { 65 const result = await contract.isAdmin(deployer.address); 66 expect(result).to.be.false 67 }); 68 69 70 it("contract should report owner address has admin role", async function () { 71 const result = await contract.isAdmin(owner.address); 72 expect(result).to.be.true 73 }); 74 75 it("should not let test address to deploy", async function () { 76 const Token: ContractFactory = await ethers.getContractFactory("ERC20NativeMinter", { signer: deployer }) 77 let token: Contract 78 try { 79 token = await Token.deploy(11111) 80 } 81 catch (err) { 82 expect(err.message).contains("is not authorized to deploy a contract"); 83 return 84 } 85 expect.fail("should have errored") 86 }); 87 88 it("should not allow deployer to enable itself", async function () { 89 try { 90 await contract.connect(deployer).addDeployer(deployer.address); 91 } 92 catch (err) { 93 return 94 } 95 expect.fail("should have errored") 96 }); 97 98 it("should not allow admin to enable deployer without enabling contract", async function () { 99 const allowList = await ethers.getContractAt("IAllowList", ALLOWLIST_ADDRESS, owner); 100 let role = await allowList.readAllowList(contract.address); 101 expect(role).to.be.equal(ROLES.NONE) 102 const result = await contract.isEnabled(contract.address); 103 expect(result).to.be.false 104 try { 105 await contract.addDeployer(deployer.address); 106 } 107 catch (err) { 108 return 109 } 110 expect.fail("should have errored") 111 }); 112 113 it("should allow admin to add contract as admin", async function () { 114 const allowList = await ethers.getContractAt("IAllowList", ALLOWLIST_ADDRESS, owner); 115 let role = await allowList.readAllowList(contract.address); 116 expect(role).to.be.equal(ROLES.NONE) 117 let tx = await allowList.setAdmin(contract.address) 118 await tx.wait() 119 role = await allowList.readAllowList(contract.address); 120 expect(role).to.be.equal(ROLES.ADMIN) 121 const result = await contract.isAdmin(contract.address); 122 expect(result).to.be.true 123 }); 124 125 it("should allow admin to add deployer address as deployer through contract", async function () { 126 let tx = await contract.setEnabled(deployer.address) 127 await tx.wait() 128 const result = await contract.isEnabled(deployer.address); 129 expect(result).to.be.true 130 }); 131 132 it("should let deployer address to deploy", async function () { 133 const Token: ContractFactory = await ethers.getContractFactory("ERC20NativeMinter", { signer: deployer }) 134 let token: Contract 135 token = await Token.deploy(11111) 136 await token.deployed() 137 expect(token.address).not.null 138 }); 139 140 it("should not let deployer add another deployer", async function () { 141 try { 142 const signers: SignerWithAddress[] = await ethers.getSigners() 143 const testAddress = signers.slice(-2)[0] 144 await contract.connect(deployer).addDeployer(testAddress.address); 145 } 146 catch (err) { 147 return 148 } 149 expect.fail("should have errored") 150 }); 151 152 it("should not let deployer to revoke admin", async function () { 153 try { 154 await contract.connect(deployer).revoke(owner.address); 155 } 156 catch (err) { 157 return 158 } 159 expect.fail("should have errored") 160 }); 161 162 163 it("should not let deployer to revoke itself", async function () { 164 try { 165 await contract.connect(deployer).revoke(deployer.address); 166 } 167 catch (err) { 168 return 169 } 170 expect.fail("should have errored") 171 }); 172 173 it("should let admin to revoke deployer", async function () { 174 let tx = await contract.revoke(deployer.address); 175 await tx.wait() 176 const allowList = await ethers.getContractAt("IAllowList", ALLOWLIST_ADDRESS, owner); 177 let noRole = await allowList.readAllowList(deployer.address); 178 expect(noRole).to.be.equal(ROLES.NONE) 179 }); 180 181 182 it("should not let admin to revoke itself", async function () { 183 try { 184 await contract.revoke(owner.address); 185 } 186 catch (err) { 187 return 188 } 189 expect.fail("should have errored") 190 }); 191 })