Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added daily limit for reserve spending #2303

Merged
merged 7 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/protocol/contracts/common/test/MockGoldToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pragma solidity ^0.5.3;
contract MockGoldToken {
uint8 public constant decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) balances;

function setTotalSupply(uint256 value) external {
totalSupply = value;
Expand All @@ -19,4 +20,13 @@ contract MockGoldToken {
function transferFrom(address, address, uint256) external pure returns (bool) {
return true;
}

function setBalanceOf(address a, uint256 value) external {
balances[a] = value;
}

function balanceOf(address a) external view returns (uint256) {
return balances[a];
}

}
29 changes: 29 additions & 0 deletions packages/protocol/contracts/stability/Reserve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ contract Reserve is IReserve, Ownable, Initializable, UsingRegistry, ReentrancyG
bytes32[] public assetAllocationSymbols;
uint256[] public assetAllocationWeights;

uint256 private lastSpendingDay;
mcortesi marked this conversation as resolved.
Show resolved Hide resolved
FixidityLib.Fraction public spendingRatio;
uint256 private spendingLimit;

event TobinTaxStalenessThresholdSet(uint256 value);
event TokenAdded(address token);
event TokenRemoved(address token, uint256 index);
Expand Down Expand Up @@ -65,6 +69,7 @@ contract Reserve is IReserve, Ownable, Initializable, UsingRegistry, ReentrancyG
_transferOwnership(msg.sender);
setRegistry(registryAddress);
setTobinTaxStalenessThreshold(_tobinTaxStalenessThreshold);
spendingRatio = FixidityLib.fixed1();
mrsmkl marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -77,6 +82,22 @@ contract Reserve is IReserve, Ownable, Initializable, UsingRegistry, ReentrancyG
emit TobinTaxStalenessThresholdSet(value);
}

/**
* @notice Set the ratio of reserve that is spendable per day.
* @param ratio Spending ratio as unwrapped Fraction.
*/
function setDailySpendingRatio(uint256 ratio) public onlyOwner {
spendingRatio = FixidityLib.wrap(ratio);
mrsmkl marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @notice Get daily spending ratio.
* @return Spending ratio as unwrapped Fraction.
*/
function getDailySpendingRatio() public view onlyOwner returns (uint256) {
return spendingRatio.unwrap();
}

/**
* @notice Sets target allocations for Celo Gold and a diversified basket of non-Celo assets.
* @param symbols The symbol of each asset in the Reserve portfolio.
Expand Down Expand Up @@ -204,6 +225,14 @@ contract Reserve is IReserve, Ownable, Initializable, UsingRegistry, ReentrancyG
*/
function transferGold(address to, uint256 value) external returns (bool) {
require(isSpender[msg.sender], "sender not allowed to transfer Reserve funds");
uint256 currentDay = now / 1 days;
if (currentDay > lastSpendingDay) {
uint256 balance = getGoldToken().balanceOf(address(this));
mrsmkl marked this conversation as resolved.
Show resolved Hide resolved
lastSpendingDay = currentDay;
spendingLimit = spendingRatio.multiply(FixidityLib.newFixed(balance)).fromFixed();
}
require(spendingLimit >= value, "Exceeding spending limit");
spendingLimit = spendingLimit.sub(value);
require(getGoldToken().transfer(to, value), "transfer of gold token failed");
return true;
}
Expand Down
39 changes: 32 additions & 7 deletions packages/protocol/test/stability/reserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
assertSameAddress,
timeTravel,
} from '@celo/protocol/lib/test-utils'
import { toFixed } from '@celo/utils/lib/fixidity'
import BigNumber from 'bignumber.js'
import BN = require('bn.js')
import {
Expand Down Expand Up @@ -67,6 +68,16 @@ contract('Reserve', (accounts: string[]) => {
})
})

describe('#setDailySpendingRatio()', async () => {
mrsmkl marked this conversation as resolved.
Show resolved Hide resolved
mrsmkl marked this conversation as resolved.
Show resolved Hide resolved
it('should allow owner to set the ratio', async () => {
await reserve.setDailySpendingRatio(123)
assert.equal(123, (await reserve.getDailySpendingRatio()).toNumber())
})
it('should not allow other users to set the ratio', async () => {
await assertRevert(reserve.setDailySpendingRatio(123, { from: nonOwner }))
})
})

describe('#setRegistry()', () => {
it('should allow owner to set registry', async () => {
await reserve.setRegistry(anAddress)
Expand Down Expand Up @@ -152,20 +163,34 @@ contract('Reserve', (accounts: string[]) => {
})

describe('#transferGold()', () => {
const aValue = 10
const aValue = 10000
beforeEach(async () => {
await web3.eth.sendTransaction({
from: accounts[0],
to: reserve.address,
value: aValue,
})
await mockGoldToken.setBalanceOf(reserve.address, aValue)
await reserve.addSpender(spender)
})

it('should allow a spender to call transferGold', async () => {
await reserve.transferGold(nonOwner, aValue, { from: spender })
})

it('should not allow a spender to transfer more than daily ratio', async () => {
await reserve.setDailySpendingRatio(toFixed(0.2))
await assertRevert(reserve.transferGold(nonOwner, aValue / 2, { from: spender }))
})

it('daily spending accumulates', async () => {
await reserve.setDailySpendingRatio(toFixed(0.15))
await reserve.transferGold(nonOwner, aValue * 0.1, { from: spender })
await assertRevert(reserve.transferGold(nonOwner, aValue * 0.1, { from: spender }))
})

it('daily spending limit should be reset after 24 hours', async () => {
await reserve.setDailySpendingRatio(toFixed(0.15))
await reserve.transferGold(nonOwner, aValue * 0.1, { from: spender })
await timeTravel(3600 * 24, web3)
await reserve.transferGold(nonOwner, aValue * 0.1, { from: spender })
})

it('should not allow a removed spender to call transferGold', async () => {
await reserve.removeSpender(spender)
await assertRevert(reserve.transferGold(nonOwner, aValue, { from: spender }))
Expand Down Expand Up @@ -414,7 +439,7 @@ contract('Reserve', (accounts: string[]) => {
})
})

describe.only('#setAssetAllocations', () => {
mrsmkl marked this conversation as resolved.
Show resolved Hide resolved
describe('#setAssetAllocations', () => {
const newAssetAllocationSymbols = [
web3.utils.padRight(web3.utils.utf8ToHex('cGLD'), 64),
web3.utils.padRight(web3.utils.utf8ToHex('BTC'), 64),
Expand Down