|
| 1 | +import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { ethers } from 'hardhat' |
| 4 | +import { RIFToken, StRIFToken } from '../typechain-types' |
| 5 | +import { ContractTransactionResponse, parseEther } from 'ethers' |
| 6 | +import { deployContracts } from './deployContracts' |
| 7 | + |
| 8 | +describe('stRIF token: Function transferAndDelegate', () => { |
| 9 | + let deployer: SignerWithAddress |
| 10 | + let alice: SignerWithAddress |
| 11 | + let bob: SignerWithAddress |
| 12 | + let john: SignerWithAddress |
| 13 | + let rif: RIFToken |
| 14 | + let stRif: StRIFToken |
| 15 | + let transferAndDelegateTx: ContractTransactionResponse |
| 16 | + const votingPower = parseEther('100') // RIF tokens |
| 17 | + |
| 18 | + const enfranchiseUser = async (user: SignerWithAddress, amount: bigint) => { |
| 19 | + await (await rif.transfer(user.address, amount)).wait() |
| 20 | + await (await rif.connect(user).approve(await stRif.getAddress(), amount)).wait() |
| 21 | + await (await stRif.connect(user).depositAndDelegate(user.address, amount)).wait() |
| 22 | + } |
| 23 | + |
| 24 | + before(async () => { |
| 25 | + ;[deployer, alice, bob, john] = await ethers.getSigners() |
| 26 | + ;({ rif, stRif } = await deployContracts(deployer)) |
| 27 | + await enfranchiseUser(alice, votingPower) |
| 28 | + await enfranchiseUser(john, votingPower) |
| 29 | + }) |
| 30 | + |
| 31 | + describe('Before transfer', () => { |
| 32 | + it('Alice should own 100 stRIFs tokens', async () => { |
| 33 | + expect(await stRif.balanceOf(alice.address)).to.equal(votingPower) |
| 34 | + }) |
| 35 | + it('Bob should not have stRIFs tokens', async () => { |
| 36 | + expect(await stRif.balanceOf(alice.address)).to.equal(votingPower) |
| 37 | + }) |
| 38 | + it('Alice should be her own delegate', async () => { |
| 39 | + expect(await stRif.delegates(alice.address)).to.equal(alice.address) |
| 40 | + }) |
| 41 | + it('Bob shouldn`t have delegates', async () => { |
| 42 | + expect(await stRif.delegates(bob.address)).to.equal(ethers.ZeroAddress) |
| 43 | + }) |
| 44 | + it('Alice should have voting power', async () => { |
| 45 | + expect(await stRif.getVotes(alice.address)).to.equal(votingPower) |
| 46 | + }) |
| 47 | + it('Bob shouldn`t have voting power', async () => { |
| 48 | + expect(await stRif.getVotes(bob.address)).to.equal(0n) |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + describe('Transfer and delegate', () => { |
| 53 | + describe('Sad path', () => { |
| 54 | + it('should not change balances after transfer of zero tokens', async () => { |
| 55 | + const tx = await stRif.connect(alice).transferAndDelegate(bob.address, 0n) |
| 56 | + await expect(() => tx).to.changeTokenBalances(stRif, [alice, bob], [0n, 0n]) |
| 57 | + }) |
| 58 | + it('Bob should`t be delegated to vote after transfer of 0 tokens', async () => { |
| 59 | + expect(await stRif.delegates(bob.address)).to.equal(ethers.ZeroAddress) |
| 60 | + }) |
| 61 | + it('Alice shouldn`t be able to transfer and delegate to zero address', async () => { |
| 62 | + const tx = stRif.connect(alice).transferAndDelegate(ethers.ZeroAddress, votingPower) |
| 63 | + await expect(tx) |
| 64 | + .to.be.revertedWithCustomError(stRif, 'ERC20InvalidReceiver') |
| 65 | + .withArgs(ethers.ZeroAddress) |
| 66 | + }) |
| 67 | + }) |
| 68 | + describe('Happy path', () => { |
| 69 | + before(async () => { |
| 70 | + transferAndDelegateTx = await stRif.connect(alice).transferAndDelegate(bob.address, parseEther('25')) |
| 71 | + }) |
| 72 | + it('Alice should use `transferAndDelegate` function to transfer 1/4 tokens to Bob', async () => { |
| 73 | + await expect(transferAndDelegateTx) |
| 74 | + .to.emit(stRif, 'Transfer') |
| 75 | + .withArgs(alice.address, bob.address, parseEther('25')) |
| 76 | + }) |
| 77 | + it('Voting power should be delegated to Bob within the SAME transaction', async () => { |
| 78 | + await expect(transferAndDelegateTx) |
| 79 | + .to.emit(stRif, 'DelegateChanged') |
| 80 | + .withArgs(bob.address, ethers.ZeroAddress, bob.address) |
| 81 | + }) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + describe('After transfer', () => { |
| 86 | + it('Alice should own 3/4 tokens', async () => { |
| 87 | + expect(await stRif.balanceOf(alice.address)).to.equal(parseEther('75')) |
| 88 | + }) |
| 89 | + it('Bob should own 1/4 tokens', async () => { |
| 90 | + expect(await stRif.balanceOf(bob.address)).to.equal(parseEther('25')) |
| 91 | + }) |
| 92 | + it('Alice should STILL be her own delegate', async () => { |
| 93 | + expect(await stRif.delegates(alice.address)).to.equal(alice.address) |
| 94 | + }) |
| 95 | + it('Bob should be his own delegate (the delegation was from Bob to Bob)', async () => { |
| 96 | + expect(await stRif.delegates(bob.address)).to.equal(bob.address) |
| 97 | + }) |
| 98 | + it('Alice`s voting power should equal her balance (3/4)', async () => { |
| 99 | + expect(await stRif.getVotes(alice.address)).to.equal(parseEther('75')) |
| 100 | + }) |
| 101 | + it('Bob`s voting power should equal his balance (1/4)', async () => { |
| 102 | + expect(await stRif.getVotes(bob.address)).to.equal(parseEther('25')) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + describe('Receiving tokens from a third source after the delegation', () => { |
| 107 | + let johnsTransferTx: ContractTransactionResponse |
| 108 | + |
| 109 | + it('John should own 100 stRIFs tokens', async () => { |
| 110 | + expect(await stRif.balanceOf(john.address)).to.equal(votingPower) |
| 111 | + }) |
| 112 | + it('John should transfer his tokens to Bob', async () => { |
| 113 | + johnsTransferTx = await stRif.connect(john).transferAndDelegate(bob.address, votingPower) |
| 114 | + await expect(() => johnsTransferTx).to.changeTokenBalances( |
| 115 | + stRif, |
| 116 | + [john, bob], |
| 117 | + [-votingPower, votingPower], |
| 118 | + ) |
| 119 | + }) |
| 120 | + it('John`s transfer tx should NOT initiate another delegation', async () => { |
| 121 | + await expect(johnsTransferTx).not.to.emit(stRif, 'DelegateChanged') |
| 122 | + }) |
| 123 | + it('Bob should remain his own delegate', async () => { |
| 124 | + expect(await stRif.delegates(bob.address)).to.equal(bob.address) |
| 125 | + }) |
| 126 | + it('Bob`s voting power should include Alice`s and John`s tokens', async () => { |
| 127 | + expect(await stRif.getVotes(bob.address)).to.equal(parseEther('25') + votingPower) |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + describe('transferFromAndDelegate: a variation with approval', () => { |
| 132 | + const amount = parseEther('25') |
| 133 | + let transferTx: ContractTransactionResponse |
| 134 | + |
| 135 | + it('should reset Bob`s delegate back to zero address', async () => { |
| 136 | + const tx = await stRif.connect(bob).delegate(ethers.ZeroAddress) |
| 137 | + await expect(tx) |
| 138 | + .to.emit(stRif, 'DelegateChanged') |
| 139 | + .withArgs(bob.address, bob.address, ethers.ZeroAddress) |
| 140 | + }) |
| 141 | + it('Bob should no longer have delegate', async () => { |
| 142 | + expect(await stRif.delegates(bob.address)).to.equal(ethers.ZeroAddress) |
| 143 | + }) |
| 144 | + it('Alice should approve Bob to transfer 25 stRIFs', async () => { |
| 145 | + const tx = await stRif.connect(alice).approve(bob.address, amount) |
| 146 | + await expect(tx).to.emit(stRif, 'Approval').withArgs(alice.address, bob.address, amount) |
| 147 | + }) |
| 148 | + it('Bob now has approval to transfer stRIFs from Alice`s balance', async () => { |
| 149 | + expect(await stRif.allowance(alice.address, bob.address)).to.equal(amount) |
| 150 | + }) |
| 151 | + it('Bob should transfer Alice`s stRIFs to himself ', async () => { |
| 152 | + transferTx = await stRif.connect(bob).transferFromAndDelegate(alice.address, bob.address, amount) |
| 153 | + await expect(() => transferTx).to.changeTokenBalances(stRif, [bob, alice], [amount, -amount]) |
| 154 | + }) |
| 155 | + it('Bob`s tx should emit Transfer event', async () => { |
| 156 | + await expect(transferTx).to.emit(stRif, 'Transfer').withArgs(alice.address, bob.address, amount) |
| 157 | + }) |
| 158 | + it('Bob should become his own delegate within the same tx', async () => { |
| 159 | + await expect(transferTx) |
| 160 | + .to.emit(stRif, 'DelegateChanged') |
| 161 | + .withArgs(bob.address, ethers.ZeroAddress, bob.address) |
| 162 | + }) |
| 163 | + it('Bob should be his own delegate', async () => { |
| 164 | + expect(await stRif.delegates(bob.address)).to.equal(bob.address) |
| 165 | + }) |
| 166 | + }) |
| 167 | +}) |
0 commit comments