From 6643a4757a2c2fe7c93e1cd790b2a1171d6c3aa4 Mon Sep 17 00:00:00 2001 From: mul53 Date: Thu, 18 Nov 2021 14:43:08 +0200 Subject: [PATCH 1/2] fix delegation bug --- contracts/JoeToken.sol | 25 +++++++++++++++++++++++++ test/JoeToken.test.ts | 12 ++++++++++++ 2 files changed, 37 insertions(+) diff --git a/contracts/JoeToken.sol b/contracts/JoeToken.sol index 78a8322..776d723 100644 --- a/contracts/JoeToken.sol +++ b/contracts/JoeToken.sol @@ -63,6 +63,31 @@ contract JoeToken is ERC20("JoeToken", "JOE"), Ownable { return _delegates[delegator]; } + /** + * @notice Transfer `rawAmount` tokens from `msg.sender` to `dst` + * @param dst The address of the destination account + * @param rawAmount The number of tokens to transfer + * @return Whether or not the transfer succeeded + */ + function transfer(address dst, uint rawAmount) public override returns (bool) { + super.transfer(dst, rawAmount); + _moveDelegates(_delegates[msg.sender], _delegates[dst], rawAmount); + return true; + } + + /** + * @notice Transfer `rawAmount` tokens from `src` to `dst` + * @param src The address of the source account + * @param dst The address of the destination account + * @param rawAmount The number of tokens to transfer + * @return Whether or not the transfer succeeded + */ + function transferFrom(address src, address dst, uint rawAmount) public override returns (bool) { + super.transferFrom(src, dst, rawAmount); + _moveDelegates(_delegates[src], _delegates[dst], rawAmount); + return true; + } + /** * @notice Delegate votes from `msg.sender` to `delegatee` * @param delegatee The address to delegate votes to diff --git a/test/JoeToken.test.ts b/test/JoeToken.test.ts index c739c20..8486f0c 100644 --- a/test/JoeToken.test.ts +++ b/test/JoeToken.test.ts @@ -70,6 +70,18 @@ describe("JoeToken", function () { await this.joe.mint(this.alice.address, "500000000000000000000000000") }) + it('should not double spend delegation votes', async function () { + await this.joe.mint(this.alice.address, '100') + await this.joe.delegate(this.bob.address) + + await this.joe.transfer(this.carol.address, '100') + await this.joe.connect(this.carol).delegate(this.bob.address) + + const votes = await this.joe.getCurrentVotes(this.bob.address) + + expect(votes).to.equal('100') // bob should have 100 votes instead of 200 + }) + after(async function () { await network.provider.request({ method: "hardhat_reset", From 9464683ae64ec434d2fe49bba09ff90895fca31c Mon Sep 17 00:00:00 2001 From: Lint Action Date: Thu, 18 Nov 2021 12:46:04 +0000 Subject: [PATCH 2/2] Fix code style issues with Prettier --- contracts/JoeToken.sol | 8 ++++++-- test/JoeToken.test.ts | 10 +++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/contracts/JoeToken.sol b/contracts/JoeToken.sol index 776d723..3f7b04f 100644 --- a/contracts/JoeToken.sol +++ b/contracts/JoeToken.sol @@ -69,7 +69,7 @@ contract JoeToken is ERC20("JoeToken", "JOE"), Ownable { * @param rawAmount The number of tokens to transfer * @return Whether or not the transfer succeeded */ - function transfer(address dst, uint rawAmount) public override returns (bool) { + function transfer(address dst, uint256 rawAmount) public override returns (bool) { super.transfer(dst, rawAmount); _moveDelegates(_delegates[msg.sender], _delegates[dst], rawAmount); return true; @@ -82,7 +82,11 @@ contract JoeToken is ERC20("JoeToken", "JOE"), Ownable { * @param rawAmount The number of tokens to transfer * @return Whether or not the transfer succeeded */ - function transferFrom(address src, address dst, uint rawAmount) public override returns (bool) { + function transferFrom( + address src, + address dst, + uint256 rawAmount + ) public override returns (bool) { super.transferFrom(src, dst, rawAmount); _moveDelegates(_delegates[src], _delegates[dst], rawAmount); return true; diff --git a/test/JoeToken.test.ts b/test/JoeToken.test.ts index 8486f0c..1e0153f 100644 --- a/test/JoeToken.test.ts +++ b/test/JoeToken.test.ts @@ -70,16 +70,16 @@ describe("JoeToken", function () { await this.joe.mint(this.alice.address, "500000000000000000000000000") }) - it('should not double spend delegation votes', async function () { - await this.joe.mint(this.alice.address, '100') + it("should not double spend delegation votes", async function () { + await this.joe.mint(this.alice.address, "100") await this.joe.delegate(this.bob.address) - await this.joe.transfer(this.carol.address, '100') + await this.joe.transfer(this.carol.address, "100") await this.joe.connect(this.carol).delegate(this.bob.address) const votes = await this.joe.getCurrentVotes(this.bob.address) - - expect(votes).to.equal('100') // bob should have 100 votes instead of 200 + + expect(votes).to.equal("100") // bob should have 100 votes instead of 200 }) after(async function () {