From 8843aab8a3677fab98c2ebc6b42c4d331e47d6d5 Mon Sep 17 00:00:00 2001 From: aggre Date: Wed, 19 Oct 2022 14:41:06 +0900 Subject: [PATCH 1/5] add patching --- contracts/src/withdraw/Withdraw.sol | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contracts/src/withdraw/Withdraw.sol b/contracts/src/withdraw/Withdraw.sol index 583bcb01..f86fb039 100644 --- a/contracts/src/withdraw/Withdraw.sol +++ b/contracts/src/withdraw/Withdraw.sol @@ -202,7 +202,9 @@ contract Withdraw is InitializableUsingRegistry, IWithdraw { IERC20 property = IERC20(_property); uint256 balance = property.balanceOf(_user); uint256 totalSupply = property.totalSupply(); - uint256 unitPriceCap = (_cap - _lastRewardCap) / totalSupply; + uint256 unitPriceCap = _cap >= _lastRewardCap + ? (_cap - _lastRewardCap) / totalSupply + : _cap / totalSupply; // If this user has held this tokens since before this tokens got its first staking, _lastRewardCap is expected to bigger than _cap. In this case, it can treat _cap as the latest range of the value. return (unitPriceCap * balance).divBasis(); } From d19c53eeba983123a4e544456d46ac07842cf951 Mon Sep 17 00:00:00 2001 From: aggre Date: Wed, 19 Oct 2022 14:52:34 +0900 Subject: [PATCH 2/5] typo --- test/withdraw/withdraw-scenario.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/withdraw/withdraw-scenario.ts b/test/withdraw/withdraw-scenario.ts index 8cbbdbc1..36b32d2c 100644 --- a/test/withdraw/withdraw-scenario.ts +++ b/test/withdraw/withdraw-scenario.ts @@ -350,7 +350,7 @@ contract('WithdrawTest', ([deployer, user1, user2, user3, user4]) => { }) }) - describe('Withdraw; calculateonlyRewardAmount', () => { + describe('Withdraw; calculateRewardAmount', () => { type calcResult = { readonly value: BigNumber readonly reword: BigNumber From ae7701ba7de4a4c6e3469c99da86d19cd5e99436 Mon Sep 17 00:00:00 2001 From: aggre Date: Wed, 19 Oct 2022 15:26:47 +0900 Subject: [PATCH 3/5] update the test cases --- test/withdraw/withdraw-scenario.ts | 76 ++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/test/withdraw/withdraw-scenario.ts b/test/withdraw/withdraw-scenario.ts index 36b32d2c..18345979 100644 --- a/test/withdraw/withdraw-scenario.ts +++ b/test/withdraw/withdraw-scenario.ts @@ -385,11 +385,13 @@ contract('WithdrawTest', ([deployer, user1, user2, user3, user4]) => { .div(1e18) .div(1e18) .integerValue(BigNumber.ROUND_DOWN) - const unitPriceCap = cap - .minus(lastRewardCap) - .times(1e18) - .div(totalSupply) - .integerValue(BigNumber.ROUND_DOWN) + const unitPriceCap = cap.isGreaterThanOrEqualTo(lastRewardCap) + ? cap + .minus(lastRewardCap) + .times(1e18) + .div(totalSupply) + .integerValue(BigNumber.ROUND_DOWN) + : cap.times(1e18).div(totalSupply).integerValue(BigNumber.ROUND_DOWN) const capped = unitPriceCap .times(balanceOfUser) .div(1e18) @@ -1357,5 +1359,69 @@ contract('WithdrawTest', ([deployer, user1, user2, user3, user4]) => { }) }) }) + + describe('scenario: multiple properties: transfer tokens before got its first staking', () => { + let dev: DevProtocolInstance + let property1: PropertyInstance + let property2: PropertyInstance + let calc: Calculator + + const alice = deployer + const bob = user1 + const carol = user2 + const dave = user4 + + before(async () => { + ;[dev, , property1] = await init(deployer, user3) + calc = createCalculator(dev) + const aliceBalance = await dev.dev.balanceOf(alice).then(toBigNumber) + await dev.dev.mint(bob, aliceBalance) + await dev.dev.mint(carol, aliceBalance) + await dev.dev.mint(dave, aliceBalance) + property2 = await artifacts + .require('Property') + .at( + getPropertyAddress( + await dev.propertyFactory.create('test2', 'TEST2', bob) + ) + ) + await dev.metricsFactory.__setMetricsCountPerProperty( + property2.address, + 1 + ) + await dev.dev.approve(dev.lockup.address, '50000000000000000000000', { + from: dave, + }) + await dev.lockup.depositToProperty( + property1.address, + '10000000000000000000000', + { + from: dave, + } + ) + await forwardBlockTimestamp(3) + }) + + it('transfer tokens before got its first staking', async () => { + await property2.transfer(alice, '3000000000000000000000000', { + from: bob, + }) + await dev.lockup.depositToProperty( + property2.address, + '10000000000000000000000', + { + from: dave, + } + ) + await forwardBlockTimestamp(3) + const calcResult = await calc(property2, alice) + await validateCalculateRewardAmountData( + dev, + property2, + alice, + calcResult + ) + }) + }) }) }) From 85555ab1ed1bf0ffe7d074009fa7683edf37e373 Mon Sep 17 00:00:00 2001 From: aggre Date: Wed, 19 Oct 2022 15:32:24 +0900 Subject: [PATCH 4/5] typo --- contracts/src/withdraw/Withdraw.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/src/withdraw/Withdraw.sol b/contracts/src/withdraw/Withdraw.sol index f86fb039..71556ae2 100644 --- a/contracts/src/withdraw/Withdraw.sol +++ b/contracts/src/withdraw/Withdraw.sol @@ -204,7 +204,7 @@ contract Withdraw is InitializableUsingRegistry, IWithdraw { uint256 totalSupply = property.totalSupply(); uint256 unitPriceCap = _cap >= _lastRewardCap ? (_cap - _lastRewardCap) / totalSupply - : _cap / totalSupply; // If this user has held this tokens since before this tokens got its first staking, _lastRewardCap is expected to bigger than _cap. In this case, it can treat _cap as the latest range of the value. + : _cap / totalSupply; // If this user has held this tokens since before this tokens got its first staking, _lastRewardCap is expected to larger than _cap. In this case, it can treat _cap as the latest range of the value. return (unitPriceCap * balance).divBasis(); } From a0b4224265f96d7a9d3a849cbd0637771208d23d Mon Sep 17 00:00:00 2001 From: aggre Date: Wed, 19 Oct 2022 15:41:20 +0900 Subject: [PATCH 5/5] add migration script for Withdraw --- migrations/update/1_update-withdraw.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 migrations/update/1_update-withdraw.ts diff --git a/migrations/update/1_update-withdraw.ts b/migrations/update/1_update-withdraw.ts new file mode 100644 index 00000000..ebce9aa0 --- /dev/null +++ b/migrations/update/1_update-withdraw.ts @@ -0,0 +1,24 @@ +const handler = async function (deployer, network) { + if (network === 'test') { + return + } + + const adminAddress = process.env.ADMIN! + const proxyAddress = process.env.WITHDRAW_PROXY! + console.log(`admin address:${adminAddress}`) + console.log(`withdraw proxy address:${proxyAddress}`) + + const logic = artifacts.require('Withdraw') + await deployer.deploy(logic) + const logicInstance = await logic.deployed() + console.log(`logic address:${logicInstance.address}`) + + const adminInstance = await artifacts.require('DevAdmin').at(adminAddress) + await adminInstance.upgrade(proxyAddress, logicInstance.address) + + const implAddress = await adminInstance.getProxyImplementation(proxyAddress) + + console.log(`impl address:${implAddress}`) +} as Truffle.Migration + +export = handler