github.com/MetalBlockchain/subnet-evm@v0.4.9/contract-examples/test/tx_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 TX_ALLOW_LIST_ADDRESS = "0x0200000000000000000000000000000000000002" 15 16 const ROLES = { 17 NONE: 0, 18 ALLOWED: 1, 19 ADMIN: 2 20 } 21 22 describe("ExampleTxAllowList", function () { 23 let admin: SignerWithAddress 24 let contract: Contract 25 let allowed: SignerWithAddress 26 let noRole: SignerWithAddress 27 before(async function () { 28 admin = await ethers.getSigner(adminAddress) 29 const contractF: ContractFactory = await ethers.getContractFactory("ExampleTxAllowList", { signer: admin }) 30 contract = await contractF.deploy() 31 await contract.deployed() 32 const contractAddress: string = contract.address 33 console.log(`Contract deployed to: ${contractAddress}`) 34 35 ;[, allowed, noRole] = await ethers.getSigners() 36 37 // Fund allowed address 38 await admin.sendTransaction({ 39 to: allowed.address, 40 value: ethers.utils.parseEther("10") 41 }) 42 43 // Fund no role address 44 let tx = await admin.sendTransaction({ 45 to: noRole.address, 46 value: ethers.utils.parseEther("10") 47 }) 48 await tx.wait() 49 }) 50 51 it("should add contract deployer as admin", async function () { 52 const contractOwnerAdmin: string = await contract.isAdmin(contract.owner()) 53 expect(contractOwnerAdmin).to.be.true 54 }) 55 56 it("precompile should see admin address has admin role", async function () { 57 // test precompile first 58 const allowList = await ethers.getContractAt("IAllowList", TX_ALLOW_LIST_ADDRESS, admin) 59 let adminRole = await allowList.readAllowList(admin.address) 60 expect(adminRole).to.be.equal(ROLES.ADMIN) 61 }) 62 63 it("precompile should see test address has no role", async function () { 64 // test precompile first 65 const allowList = await ethers.getContractAt("IAllowList", TX_ALLOW_LIST_ADDRESS, admin) 66 let role = await allowList.readAllowList(noRole.address) 67 expect(role).to.be.equal(ROLES.NONE) 68 }) 69 70 it("contract should report test address has no admin role", async function () { 71 const result = await contract.isAdmin(noRole.address) 72 expect(result).to.be.false 73 }) 74 75 76 it("contract should report admin address has admin role", async function () { 77 const result = await contract.isAdmin(admin.address) 78 expect(result).to.be.true 79 }) 80 81 it("should not let test address submit txs", async function () { 82 const Token: ContractFactory = await ethers.getContractFactory("ERC20NativeMinter", { signer: noRole }) 83 let token: Contract 84 try { 85 token = await Token.deploy(11111) 86 } 87 catch (err) { 88 expect(err.message).contains("cannot issue transaction from non-allow listed address") 89 return 90 } 91 expect.fail("should have errored") 92 }) 93 94 it("should not allow noRole to enable itself", async function () { 95 try { 96 await contract.connect(noRole).addDeployer(noRole.address) 97 } 98 catch (err) { 99 return 100 } 101 expect.fail("should have errored") 102 }) 103 104 it("should not allow admin to enable noRole without enabling contract", async function () { 105 const allowList = await ethers.getContractAt("IAllowList", TX_ALLOW_LIST_ADDRESS, admin) 106 let role = await allowList.readAllowList(contract.address) 107 expect(role).to.be.equal(ROLES.NONE) 108 const result = await contract.isEnabled(contract.address) 109 expect(result).to.be.false 110 try { 111 await contract.setEnabled(noRole.address) 112 } 113 catch (err) { 114 return 115 } 116 expect.fail("should have errored") 117 }) 118 119 it("should allow admin to add contract as admin", async function () { 120 const allowList = await ethers.getContractAt("IAllowList", TX_ALLOW_LIST_ADDRESS, admin) 121 let role = await allowList.readAllowList(contract.address) 122 expect(role).to.be.equal(ROLES.NONE) 123 let tx = await allowList.setAdmin(contract.address) 124 await tx.wait() 125 role = await allowList.readAllowList(contract.address) 126 expect(role).to.be.equal(ROLES.ADMIN) 127 const result = await contract.isAdmin(contract.address) 128 expect(result).to.be.true 129 }) 130 131 it("should allow admin to add allowed address as allowed through contract", async function () { 132 let result = await contract.isEnabled(allowed.address) 133 expect(result).to.be.false 134 let tx = await contract.setEnabled(allowed.address) 135 await tx.wait() 136 result = await contract.isEnabled(allowed.address) 137 expect(result).to.be.true 138 }) 139 140 it("should let allowed address deploy", async function () { 141 const Token: ContractFactory = await ethers.getContractFactory("ERC20NativeMinter", { signer: allowed }) 142 let token: Contract 143 token = await Token.deploy(11111) 144 await token.deployed() 145 expect(token.address).not.null 146 }) 147 148 it("should not let allowed add another allowed", async function () { 149 try { 150 const signers: SignerWithAddress[] = await ethers.getSigners() 151 const testAddress = signers.slice(-2)[0] 152 await contract.connect(allowed).setEnabled(noRole) 153 } 154 catch (err) { 155 return 156 } 157 expect.fail("should have errored") 158 }) 159 160 it("should not let allowed to revoke admin", async function () { 161 try { 162 await contract.connect(allowed).revoke(admin.address) 163 } 164 catch (err) { 165 return 166 } 167 expect.fail("should have errored") 168 }) 169 170 171 it("should not let allowed to revoke itself", async function () { 172 try { 173 await contract.connect(allowed).revoke(allowed.address) 174 } 175 catch (err) { 176 return 177 } 178 expect.fail("should have errored") 179 }) 180 181 it("should let admin to revoke allowed", async function () { 182 let tx = await contract.revoke(allowed.address) 183 await tx.wait() 184 const allowList = await ethers.getContractAt("IAllowList", TX_ALLOW_LIST_ADDRESS, admin) 185 let noRole = await allowList.readAllowList(allowed.address) 186 expect(noRole).to.be.equal(ROLES.NONE) 187 }) 188 189 190 it("should not let admin to revoke itself", async function () { 191 try { 192 await contract.revoke(admin.address) 193 } 194 catch (err) { 195 return 196 } 197 expect.fail("should have errored") 198 }) 199 })