Skip to content

Commit ac4e4a3

Browse files
committed
VaultRecoverable: emit event on successful recovery (#480)
1 parent ec588b5 commit ac4e4a3

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

contracts/common/IVaultRecoverable.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ pragma solidity ^0.4.24;
66

77

88
interface IVaultRecoverable {
9+
event RecoverToVault(address indexed vault, address indexed token, uint256 amount);
10+
911
function transferToVault(address token) external;
1012

1113
function allowRecoverability(address token) external view returns (bool);

contracts/common/VaultRecoverable.sol

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ contract VaultRecoverable is IVaultRecoverable, EtherTokenConstant, IsContract {
2828
address vault = getRecoveryVault();
2929
require(isContract(vault), ERROR_VAULT_NOT_CONTRACT);
3030

31+
uint256 balance;
3132
if (_token == ETH) {
32-
vault.transfer(address(this).balance);
33+
balance = address(this).balance;
34+
vault.transfer(balance);
3335
} else {
3436
ERC20 token = ERC20(_token);
35-
uint256 amount = token.staticBalanceOf(this);
36-
require(token.safeTransfer(vault, amount), ERROR_TOKEN_TRANSFER_FAILED);
37+
balance = token.staticBalanceOf(this);
38+
require(token.safeTransfer(vault, balance), ERROR_TOKEN_TRANSFER_FAILED);
3739
}
40+
41+
emit RecoverToVault(vault, _token, balance);
3842
}
3943

4044
/**

test/recovery_to_vault.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const assertEvent = require('./helpers/assertEvent')
12
const { assertRevert } = require('./helpers/assertThrow')
23
const { skipCoverage } = require('./helpers/coverage')
34
const { getBalance } = require('./helpers/web3')
@@ -36,18 +37,23 @@ contract('Recovery to vault', accounts => {
3637
const initialBalance = await getBalance(target.address)
3738
const initialVaultBalance = await getBalance(vault.address)
3839
const r = await target.sendTransaction({ value: 1, gas: SEND_ETH_GAS })
39-
assert.equal((await getBalance(target.address)).valueOf(), initialBalance.plus(amount))
40+
assert.equal((await getBalance(target.address)).valueOf(), initialBalance.plus(amount), 'Target initial balance should be correct')
4041

4142
const recoverAction = () => target.transferToVault(ETH)
4243

4344
if (shouldFail) {
4445
await assertRevert(recoverAction)
45-
assert.equal((await getBalance(target.address)).valueOf(), initialBalance.plus(amount))
46-
assert.equal((await getBalance(vault.address)).valueOf(), initialVaultBalance)
46+
assert.equal((await getBalance(target.address)).valueOf(), initialBalance.plus(amount), 'Target balance should be same as before')
47+
assert.equal((await getBalance(vault.address)).valueOf(), initialVaultBalance, 'Vault balance should should be same as before')
4748
} else {
48-
await recoverAction()
49-
assert.equal((await getBalance(target.address)).valueOf(), 0)
50-
assert.equal((await getBalance(vault.address)).valueOf(), initialVaultBalance.plus(initialBalance).plus(amount))
49+
const recoverReceipt = await recoverAction()
50+
assert.equal((await getBalance(target.address)).valueOf(), 0, 'Target balance should be 0')
51+
assert.equal((await getBalance(vault.address)).valueOf(), initialVaultBalance.plus(initialBalance).plus(amount), 'Vault balance should include recovered amount')
52+
53+
assert.equal(getEvent(recoverReceipt, 'RecoverToVault', 'vault'), vault.address, 'RecoverToVault event should have correct vault')
54+
assert.equal(getEvent(recoverReceipt, 'RecoverToVault', 'token'), ETH, 'RecoverToVault event should have correct token')
55+
assert.equal(getEvent(recoverReceipt, 'RecoverToVault', 'amount'), amount, 'RecoverToVault event should have correct amount')
56+
assertEvent(recoverReceipt, 'RecoverToVault', 1)
5157
}
5258
}
5359

@@ -57,18 +63,23 @@ contract('Recovery to vault', accounts => {
5763
const initialBalance = await token.balanceOf(target.address)
5864
const initialVaultBalance = await token.balanceOf(vault.address)
5965
await token.transfer(target.address, amount)
60-
assert.equal((await token.balanceOf(target.address)).valueOf(), initialBalance.plus(amount))
66+
assert.equal((await token.balanceOf(target.address)).valueOf(), initialBalance.plus(amount), 'Target initial balance should be correct')
6167

6268
const recoverAction = () => target.transferToVault(token.address)
6369

6470
if (shouldFail) {
6571
await assertRevert(recoverAction)
66-
assert.equal((await token.balanceOf(target.address)).valueOf(), initialBalance.plus(amount))
67-
assert.equal((await token.balanceOf(vault.address)).valueOf(), initialVaultBalance)
72+
assert.equal((await token.balanceOf(target.address)).valueOf(), initialBalance.plus(amount), 'Target balance should be same as before')
73+
assert.equal((await token.balanceOf(vault.address)).valueOf(), initialVaultBalance, 'Vault balance should should be same as before')
6874
} else {
69-
await recoverAction()
70-
assert.equal((await token.balanceOf(target.address)).valueOf(), 0)
71-
assert.equal((await token.balanceOf(vault.address)).valueOf(), initialVaultBalance.plus(initialBalance).plus(amount))
75+
const recoverReceipt = await recoverAction()
76+
assert.equal((await token.balanceOf(target.address)).valueOf(), 0, 'Target balance should be 0')
77+
assert.equal((await token.balanceOf(vault.address)).valueOf(), initialVaultBalance.plus(initialBalance).plus(amount), 'Vault balance should include recovered amount')
78+
79+
assert.equal(getEvent(recoverReceipt, 'RecoverToVault', 'vault'), vault.address, 'RecoverToVault event should have correct vault')
80+
assert.equal(getEvent(recoverReceipt, 'RecoverToVault', 'token'), token.address, 'RecoverToVault event should have correct token')
81+
assert.equal(getEvent(recoverReceipt, 'RecoverToVault', 'amount'), amount, 'RecoverToVault event should have correct amount')
82+
assertEvent(recoverReceipt, 'RecoverToVault', 1)
7283
}
7384
}
7485

@@ -78,16 +89,16 @@ contract('Recovery to vault', accounts => {
7889
const initialBalance = await token.balanceOf(target.address)
7990
const initialVaultBalance = await token.balanceOf(vault.address)
8091
await token.transfer(target.address, amount)
81-
assert.equal((await token.balanceOf(target.address)).valueOf(), initialBalance.plus(amount))
92+
assert.equal((await token.balanceOf(target.address)).valueOf(), initialBalance.plus(amount), 'Target initial balance should be correct')
8293

8394
// Stop token from being transferable
8495
await token.setAllowTransfer(false)
8596

8697
// Try to transfer
8798
await assertRevert(() => target.transferToVault(token.address))
8899

89-
assert.equal((await token.balanceOf(target.address)).valueOf(), initialBalance.plus(amount))
90-
assert.equal((await token.balanceOf(vault.address)).valueOf(), initialVaultBalance)
100+
assert.equal((await token.balanceOf(target.address)).valueOf(), initialBalance.plus(amount), 'Target balance should be same as before')
101+
assert.equal((await token.balanceOf(vault.address)).valueOf(), initialVaultBalance, 'Vault balance should should be same as before')
91102
}
92103

93104
const failWithoutVault = async (target, kernel) => {
@@ -96,7 +107,7 @@ contract('Recovery to vault', accounts => {
96107
const initialBalance = await getBalance(target.address)
97108
await kernel.setRecoveryVaultAppId(vaultId)
98109
const r = await target.sendTransaction({ value: 1, gas: SEND_ETH_GAS })
99-
assert.equal((await getBalance(target.address)).valueOf(), initialBalance.plus(amount))
110+
assert.equal((await getBalance(target.address)).valueOf(), initialBalance.plus(amount), 'Target initial balance should be correct')
100111
return assertRevert(async () => {
101112
await target.transferToVault(ETH)
102113
})
@@ -134,7 +145,7 @@ contract('Recovery to vault', accounts => {
134145

135146
// Test both the Kernel itself and the KernelProxy to make sure their behaviours are the same
136147
for (const kernelType of ['Kernel', 'KernelProxy']) {
137-
context(`> ${kernelType}`, () => {
148+
context.only(`> ${kernelType}`, () => {
138149
let kernelBase, kernel
139150

140151
before(async () => {

0 commit comments

Comments
 (0)