Skip to content

Commit

Permalink
fix: revert with wrong pool initialization
Browse files Browse the repository at this point in the history
- revert when using a rogue base token or a normal wallet as base token.
- assert in tests
  • Loading branch information
gabririgo committed Nov 6, 2023
1 parent 3e1efc8 commit 5cb1cb8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
26 changes: 13 additions & 13 deletions contracts/protocol/core/sys/MixinInitializer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ import "../../interfaces/IRigoblockPoolProxyFactory.sol";
abstract contract MixinInitializer is MixinImmutables, MixinStorage {
modifier onlyUninitialized() {
// pool proxy is always initialized in the constructor, therefore
// empty extcodesize means the pool has not been initialized
address self = address(this);
uint256 size;
assembly {
size := extcodesize(self)
}
require(size == 0, "POOL_ALREADY_INITIALIZED_ERROR");
// empty code means the pool has not been initialized
require(address(this).code.length == 0, "POOL_ALREADY_INITIALIZED_ERROR");
_;
}

/// @inheritdoc IRigoblockV3PoolInitializer
function initializePool() external override onlyUninitialized {
uint8 tokenDecimals = 18;
IRigoblockPoolProxyFactory.Parameters memory initParams = IRigoblockPoolProxyFactory(msg.sender).parameters();
uint8 tokenDecimals;

if (initParams.baseToken != address(0)) {
tokenDecimals = IERC20(initParams.baseToken).decimals();
}

// a pool with small decimals could easily underflow.
assert(tokenDecimals >= 6);
// revert in case the ERC20 read call fails silently
assert(initParams.baseToken.code.length > 0);
try IERC20(initParams.baseToken).decimals() returns (uint8 decimals) {
tokenDecimals = decimals;
// a pool with small decimals could easily underflow.
assert(tokenDecimals >= 6);
} catch (bytes memory returnData) {
revert(string(returnData));
}
} else { tokenDecimals = 18; }

poolWrapper().pool = Pool({
name: initParams.name,
Expand Down
20 changes: 20 additions & 0 deletions test/factory/RigoblockPool.ProxyFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ describe("ProxyFactory", async () => {
factory.createPool('testpool', 'TEST', rogueToken.address)
).to.be.revertedWith("POOL_INITIALIZATION_FAILED_ERROR")
})

it('should revert when base token does not implement decimals', async () => {
const { factory } = await setupTests()
const [ user1 ] = waffle.provider.getWallets()
const source = 'contract RogueToken { function rogue() external pure returns (uint8) { return 0; } }'
const rogueToken = await deployContract(user1, source)
await expect(
factory.createPool('testpool', 'TEST', rogueToken.address)
).to.be.revertedWith("POOL_INITIALIZATION_FAILED_ERROR")
})

it('should revert when base token is not a contract', async () => {
const { factory } = await setupTests()
const [ user1 ] = waffle.provider.getWallets()
const source = 'contract RogueToken { function decimals() external pure returns (uint8) { return 5; } }'
const rogueToken = await deployContract(user1, source)
await expect(
factory.createPool('testpool', 'TEST', user1.address)
).to.be.revertedWith("POOL_INITIALIZATION_FAILED_ERROR")
})
})

describe("setImplementation", async () => {
Expand Down

0 comments on commit 5cb1cb8

Please sign in to comment.