github.com/MetalBlockchain/subnet-evm@v0.4.9/contract-examples/test/reward_manager.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 BigNumber, 8 Contract, 9 ContractFactory, 10 } from "ethers" 11 import { ethers } from "hardhat" 12 import ts = require("typescript"); 13 14 // make sure this is always an admin for reward manager precompile 15 const adminAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC" 16 const REWARD_MANAGER_ADDRESS = "0x0200000000000000000000000000000000000004"; 17 const BLACKHOLE_ADDRESS = "0x0100000000000000000000000000000000000000"; 18 19 const ROLES = { 20 NONE: 0, 21 ENABLED: 1, 22 ADMIN: 2 23 }; 24 25 describe("ExampleRewardManager", function () { 26 this.timeout("30s") 27 let owner: SignerWithAddress 28 let contract: Contract 29 let signer1: SignerWithAddress 30 let signer2: SignerWithAddress 31 let precompile: Contract 32 33 before(async function () { 34 owner = await ethers.getSigner(adminAddress); 35 signer1 = (await ethers.getSigners())[1] 36 signer2 = (await ethers.getSigners())[2] 37 const Contract: ContractFactory = await ethers.getContractFactory("ExampleRewardManager", { signer: owner }) 38 contract = await Contract.deploy() 39 await contract.deployed() 40 const contractAddress: string = contract.address 41 console.log(`Contract deployed to: ${contractAddress}`) 42 43 precompile = await ethers.getContractAt("IRewardManager", REWARD_MANAGER_ADDRESS, owner); 44 45 // Send a transaction to mine a new block 46 const tx = await owner.sendTransaction({ 47 to: signer1.address, 48 value: ethers.utils.parseEther("10") 49 }) 50 await tx.wait() 51 }); 52 53 it("should add contract deployer as owner", async function () { 54 const contractOwnerAddr: string = await contract.owner() 55 expect(owner.address).to.equal(contractOwnerAddr) 56 }); 57 58 // this contract is not selected as the reward address yet, so should not be able to receive fees 59 it("should send fees to blackhole", async function () { 60 let rewardAddress = await contract.currentRewardAddress(); 61 expect(rewardAddress).to.be.equal(BLACKHOLE_ADDRESS) 62 63 let firstBHBalance = await ethers.provider.getBalance(BLACKHOLE_ADDRESS) 64 65 // Send a transaction to mine a new block 66 const tx = await owner.sendTransaction({ 67 to: signer1.address, 68 value: ethers.utils.parseEther("0.0001") 69 }) 70 await tx.wait() 71 72 let secondBHBalance = await ethers.provider.getBalance(BLACKHOLE_ADDRESS) 73 expect(secondBHBalance.gt(firstBHBalance)).to.be.true 74 }) 75 76 it("should not appoint reward address before enabled", async function () { 77 let contractRole = await precompile.readAllowList(contract.address); 78 expect(contractRole).to.be.equal(ROLES.NONE) 79 try { 80 let tx = await contract.setRewardAddress(signer1.address); 81 await tx.wait() 82 } 83 catch (err) { 84 return 85 } 86 expect.fail("should have errored") 87 }); 88 89 90 it("contract should be added to enabled list", async function () { 91 let contractRole = await precompile.readAllowList(contract.address); 92 expect(contractRole).to.be.equal(ROLES.NONE) 93 94 let enableTx = await precompile.setEnabled(contract.address); 95 await enableTx.wait() 96 contractRole = await precompile.readAllowList(contract.address); 97 expect(contractRole).to.be.equal(ROLES.ENABLED) 98 }); 99 100 101 it("should be appointed as reward address", async function () { 102 let tx = await contract.setRewardAddress(contract.address); 103 await tx.wait() 104 let rewardAddress = await contract.currentRewardAddress(); 105 expect(rewardAddress).to.be.equal(contract.address) 106 }); 107 108 it("should be able to receive fees", async function () { 109 let previousBalance = await ethers.provider.getBalance(contract.address) 110 111 // Send a transaction to mine a new block 112 const tx = await owner.sendTransaction({ 113 to: signer1.address, 114 value: ethers.utils.parseEther("0.0001") 115 }) 116 await tx.wait() 117 118 let balance = await ethers.provider.getBalance(contract.address) 119 expect(balance.gt(previousBalance)).to.be.true 120 }) 121 122 it("signer1 should be appointed as reward address", async function () { 123 let tx = await contract.setRewardAddress(signer1.address); 124 await tx.wait() 125 let rewardAddress = await contract.currentRewardAddress(); 126 expect(rewardAddress).to.be.equal(signer1.address) 127 }); 128 129 it("signer1 should be able to receive fees", async function () { 130 let previousBalance = await ethers.provider.getBalance(signer1.address) 131 132 // Send a transaction to mine a new block 133 const tx = await owner.sendTransaction({ 134 to: signer2.address, 135 value: ethers.utils.parseEther("0.0001") 136 }) 137 await tx.wait() 138 139 let balance = await ethers.provider.getBalance(signer1.address) 140 expect(balance.gt(previousBalance)).to.be.true 141 }) 142 143 144 it("should return false for allowFeeRecipients check", async function () { 145 let res = await contract.areFeeRecipientsAllowed(); 146 expect(res).to.be.false 147 }) 148 149 it("should enable allowFeeRecipients", async function () { 150 let tx = await contract.allowFeeRecipients(); 151 await tx.wait() 152 let res = await contract.areFeeRecipientsAllowed(); 153 expect(res).to.be.true 154 }) 155 156 it("should disable reward address", async function () { 157 let tx = await contract.disableRewards(); 158 await tx.wait() 159 160 let rewardAddress = await contract.currentRewardAddress(); 161 expect(rewardAddress).to.be.equal(BLACKHOLE_ADDRESS) 162 163 let res = await contract.areFeeRecipientsAllowed(); 164 expect(res).to.be.false 165 }) 166 });