Skip to content

Commit

Permalink
Add donate feature (#399)
Browse files Browse the repository at this point in the history
* add donate()

* add tests

* fix tests

* add function comment
  • Loading branch information
haythem96 committed May 5, 2021
1 parent 66ab3bb commit 874aeed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
14 changes: 14 additions & 0 deletions contracts/Controller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
event SystemFullyPaused(bool isPaused);
/// @notice emits an event when the call action restriction changes
event CallRestricted(bool isRestricted);
/// @notice emits an event when a donation transfer executed
event Donated(address indexed donator, address indexed asset, uint256 amount);

/**
* @notice modifier to check if the system is not partially paused, where only redeem and settleVault is allowed
Expand Down Expand Up @@ -260,6 +262,18 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
_refreshConfigInternal();
}

/**
* @notice send asset amount to margin pool
* @dev use donate() instead of direct transfer() to store the balance in assetBalance
* @param _asset asset address
* @param _amount amount to donate to pool
*/
function donate(address _asset, uint256 _amount) external {
pool.transferToPool(_asset, msg.sender, _amount);

emit Donated(msg.sender, _asset, _amount);
}

/**
* @notice allows the partialPauser to toggle the systemPartiallyPaused variable and partially pause or partially unpause the system
* @dev can only be called by the partialPauser
Expand Down
21 changes: 20 additions & 1 deletion test/unit-tests/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ enum ActionType {

contract(
'Controller',
([owner, accountOwner1, accountOwner2, accountOperator1, holder1, fullPauser, partialPauser, random]) => {
([owner, accountOwner1, accountOwner2, accountOperator1, holder1, fullPauser, partialPauser, random, donor]) => {
// ERC20 mock
let usdc: MockERC20Instance
let weth: MockERC20Instance
Expand Down Expand Up @@ -112,6 +112,7 @@ contract(
await usdc.mint(accountOwner1, createTokenAmount(10000, usdcDecimals))
await usdc.mint(accountOperator1, createTokenAmount(10000, usdcDecimals))
await usdc.mint(random, createTokenAmount(10000, usdcDecimals))
await usdc.mint(donor, createTokenAmount(10000, usdcDecimals))
})

describe('Controller initialization', () => {
Expand Down Expand Up @@ -4810,6 +4811,24 @@ contract(
})
})

describe('Donate to pool', () => {
it('it should donate to margin pool', async () => {
const amountToDonate = createTokenAmount(10, usdcDecimals)
const storedBalanceBefore = new BigNumber(await marginPool.getStoredBalance(usdc.address))

await usdc.approve(marginPool.address, amountToDonate, {from: donor})
await controllerProxy.donate(usdc.address, amountToDonate, {from: donor})

const storedBalanceAfter = new BigNumber(await marginPool.getStoredBalance(usdc.address))

assert.equal(
storedBalanceAfter.minus(storedBalanceBefore).toString(),
amountToDonate,
'Donated amount mismatch',
)
})
})

describe('Refresh configuration', () => {
it('should revert refreshing configuration from address other than owner', async () => {
await expectRevert(controllerProxy.refreshConfiguration({from: random}), 'Ownable: caller is not the owner')
Expand Down

0 comments on commit 874aeed

Please sign in to comment.