Skip to content

Commit 2ddc62b

Browse files
committed
CR Oracle : add swapDeposit(old, new) function
1 parent b261efe commit 2ddc62b

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

contracts/oracle/CollateralizationOracle.sol

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,49 @@ contract CollateralizationOracle is ICollateralizationOracle, CoreRef {
137137
emit DepositRemove(msg.sender, _deposit);
138138
}
139139

140+
/// @notice Swap a PCVDeposit with a new one, for instance when a new version
141+
/// of a deposit (holding the same token) is deployed.
142+
/// @param _oldDeposit : the PCVDeposit to remove from the list.
143+
/// @param _newDeposit : the PCVDeposit to add to the list.
144+
function swapDeposit(address _oldDeposit, address _newDeposit) external onlyGovernor {
145+
// get the token in which the old deposit reports its token
146+
address _token = depositToToken[_oldDeposit];
147+
address _newToken = IPCVDepositV2(_newDeposit).balanceReportedIn();
148+
149+
// revert if old deposit is not found
150+
require(_token != address(0), "CollateralizationOracle: deposit not found");
151+
152+
// revert if new deposit is found
153+
require(depositToToken[_newDeposit] == address(0), "CollateralizationOracle: deposit duplicate");
154+
155+
// revert if token is different
156+
require(_token == _newToken, "CollateralizationOracle: deposit has different token");
157+
158+
// swap the PCVDeposit in the list
159+
bool found = false;
160+
for (uint256 i = 0; !found; i++) {
161+
if (pcvDeposits[i] == _oldDeposit) {
162+
found = true;
163+
pcvDeposits[i] = _newDeposit;
164+
}
165+
}
166+
167+
// update maps & arrays for faster access
168+
depositToToken[_oldDeposit] = address(0);
169+
depositToToken[_newDeposit] = _token;
170+
found = false;
171+
for (uint256 i = 0; !found; i++) {
172+
if (tokenToDeposits[_token][i] == _oldDeposit) {
173+
found = true;
174+
tokenToDeposits[_token][i] = _newDeposit;
175+
}
176+
}
177+
178+
// emit event
179+
emit DepositRemove(msg.sender, _oldDeposit);
180+
emit DepositAdd(msg.sender, _newDeposit, _token);
181+
}
182+
140183
/// @notice Set the price feed oracle (in USD) for a given asset.
141184
/// @param _token : the asset to add price oracle for
142185
/// @param _newOracle : price feed oracle for the given asset

test/oracle/CollateralizationOracle.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,89 @@ describe('CollateralizationOracle', function () {
167167
});
168168
});
169169

170+
describe('swapDeposit()', function() {
171+
beforeEach(async function() {
172+
this.deposit1bis = await MockPCVDepositV2.new(
173+
this.core.address,
174+
this.token1.address,
175+
`2000${e18}`,// balance
176+
`1000${e18}`// protocol FEI
177+
);
178+
await this.oracle.setOracle(this.token1.address, this.oracle1.address, { from: governorAddress });
179+
await this.oracle.addDeposit(this.deposit1.address, { from: governorAddress });
180+
});
181+
it('should emit DepositRemove', async function() {
182+
expectEvent(
183+
await this.oracle.swapDeposit(this.deposit1.address, this.deposit1bis.address, { from: governorAddress }),
184+
'DepositRemove',
185+
{
186+
from: governorAddress,
187+
deposit: this.deposit1.address
188+
}
189+
);
190+
});
191+
it('should emit DepositAdd', async function() {
192+
expectEvent(
193+
await this.oracle.swapDeposit(this.deposit1.address, this.deposit1bis.address, { from: governorAddress }),
194+
'DepositAdd',
195+
{
196+
from: governorAddress,
197+
deposit: this.deposit1bis.address,
198+
token: this.token1.address
199+
}
200+
);
201+
});
202+
it('should update maps & array properties', async function() {
203+
// initial situation : 1 deposit
204+
expect(await this.oracle.pcvDeposits('0')).to.be.equal(this.deposit1.address);
205+
expect(await this.oracle.tokenToDeposits(this.token1.address, '0')).to.be.equal(this.deposit1.address);
206+
expect(await this.oracle.depositToToken(this.deposit1.address)).to.be.equal(this.token1.address);
207+
expect(await this.oracle.tokensInPcv('0')).to.be.equal(this.token1.address);
208+
expect(await this.oracle.isTokenInPcv(this.token1.address)).to.be.equal(true);
209+
// swap deposit
210+
await this.oracle.swapDeposit(this.deposit1.address, this.deposit1bis.address, { from: governorAddress });
211+
// after swap
212+
expect(await this.oracle.pcvDeposits('0')).to.be.equal(this.deposit1bis.address);
213+
await expectRevert.unspecified(this.oracle.pcvDeposits('1'));
214+
expect(await this.oracle.tokenToDeposits(this.token1.address, '0')).to.be.equal(this.deposit1bis.address);
215+
await expectRevert.unspecified(this.oracle.tokenToDeposits(this.token1.address, '1'));
216+
expect(await this.oracle.depositToToken(this.deposit1bis.address)).to.be.equal(this.token1.address);
217+
expect(await this.oracle.depositToToken(this.deposit1.address)).to.be.equal(ZERO_ADDRESS);
218+
expect(await this.oracle.tokensInPcv('0')).to.be.equal(this.token1.address);
219+
expect(await this.oracle.isTokenInPcv(this.token1.address)).to.be.equal(true);
220+
});
221+
it('should revert if not governor', async function() {
222+
await expectRevert(
223+
this.oracle.swapDeposit(
224+
this.deposit1.address,
225+
this.deposit1bis.address,
226+
{ from: userAddress }
227+
),
228+
'CoreRef: Caller is not a governor'
229+
);
230+
});
231+
it('should revert if deposit is not found', async function() {
232+
await expectRevert(
233+
this.oracle.swapDeposit(
234+
this.deposit2.address,
235+
this.deposit1bis.address,
236+
{ from: governorAddress }
237+
),
238+
'CollateralizationOracle: deposit not found'
239+
);
240+
});
241+
it('should revert if new deposit is already found', async function() {
242+
await expectRevert(
243+
this.oracle.swapDeposit(
244+
this.deposit1.address,
245+
this.deposit1.address,
246+
{ from: governorAddress }
247+
),
248+
'CollateralizationOracle: deposit duplicate'
249+
);
250+
});
251+
});
252+
170253
describe('setOracle()', function() {
171254
it('should emit OracleUpdate', async function() {
172255
expectEvent(

0 commit comments

Comments
 (0)