Skip to content

Commit

Permalink
Add polygon destination (#296)
Browse files Browse the repository at this point in the history
* inflation supports polygon destination

* Update contracts/root/PolygonDestination.sol

* fix test

---------

Co-authored-by: mzxyz <[email protected]>
  • Loading branch information
ianhe8x and mzxyz authored Dec 12, 2023
1 parent eac4583 commit 8de6dfc
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 73 deletions.
25 changes: 25 additions & 0 deletions contracts/mocks/MockInflationDestination.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2020-2023 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.15;

import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import {IInflationDestination} from "../root/IInflationDestination.sol";

contract MockInflationDestination is IInflationDestination, ERC165 {
event HookCalled();
constructor() {
}

/**
* @notice Check ERC165 interface
* @param interfaceId interface ID
* @return Result of support or not
*/
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165) returns (bool) {
return interfaceId == type(IInflationDestination).interfaceId || super.supportsInterface(interfaceId);
}

function afterReceiveInflatedTokens(uint256 tokenAmount) external {
emit HookCalled();
}
}
10 changes: 10 additions & 0 deletions contracts/mocks/MockInflationDestination2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2020-2023 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.15;

import {IInflationDestination} from "../root/IInflationDestination.sol";

contract MockInflationDestination2 {
constructor() {
}
}
3 changes: 3 additions & 0 deletions contracts/root/IRootChainManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
pragma solidity 0.8.15;

interface IRootChainManager {
function typeToPredicate(bytes32 _type) external view returns (address);
function tokenToType(address _addr) external view returns (bytes32);

event TokenMapped(
address indexed rootToken,
address indexed childToken,
Expand Down
13 changes: 10 additions & 3 deletions contracts/root/InflationController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ contract InflationController is Initializable, OwnableUpgradeable, Constants {
/// @notice Seconds for the Julian year
uint256 private constant YEAR_SECONDS = (3600 * 24 * 36525) / 100;

/// @notice Emitted when inflation started
event InflationStart();

/**
* @dev ### FUNCTIONS
* @notice Initialize the contract to setup parameters: inflationRate, inflationDestination, lastInflationTimestamp
Expand All @@ -58,7 +61,7 @@ contract InflationController is Initializable, OwnableUpgradeable, Constants {
settings = _settings;
inflationRate = _inflationRate;
inflationDestination = _inflationDestination;
lastInflationTimestamp = block.timestamp;
// lastInflationTimestamp = block.timestamp;
}

/**
Expand Down Expand Up @@ -90,11 +93,15 @@ contract InflationController is Initializable, OwnableUpgradeable, Constants {
* @notice Can only called by eraManager when startNewEra, it will calculate and mint the inflation SQT token for last Era according to the inflation rate.
*/
function mintInflatedTokens() external {
// require(msg.sender == settings.getContractAddress(SQContracts.EraManager), 'G012');
// require(msg.sender == settings.getContractAddress(SQContracts.EventSyncRootTunnel), 'G012');
if (lastInflationTimestamp == 0) {
lastInflationTimestamp = block.timestamp;
emit InflationStart();
return;
}
uint256 passedTime = block.timestamp - lastInflationTimestamp;
require(passedTime > 0, 'IC002');

// passedTimeRate is enlarged by 1e9 (PER_BILL)
uint256 passedTimeRate = MathUtil.mulDiv(passedTime * inflationRate, PER_BILL / PER_MILL, YEAR_SECONDS);
lastInflationTimestamp = block.timestamp;

Expand Down
9 changes: 9 additions & 0 deletions contracts/root/PolygonDestination.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.15;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import {IInflationDestination} from "./IInflationDestination.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
Expand Down Expand Up @@ -44,8 +45,16 @@ contract PolygonDestination is IInflationDestination, Ownable, ERC165 {
address rcManager = ISettings(settings).getContractAddress(SQContracts.RootChainManager);
require(rcManager != address(0), "PD001");
address sqtoken = ISettings(settings).getContractAddress(SQContracts.SQToken);
bytes32 tokenType = IRootChainManager(rcManager).tokenToType(sqtoken);
// ERC20Predicate contract address, use for locking tokens
address predicate = IRootChainManager(rcManager).typeToPredicate(tokenType);
ERC20(sqtoken).increaseAllowance(predicate, tokenAmount);
bytes memory depositData = abi.encode(tokenAmount);
IRootChainManager(rcManager).depositFor(xcRecipient, sqtoken, depositData);
}

function withdraw(address _token) external onlyOwner {
uint256 amount = ERC20(_token).balanceOf(address(this));
ERC20(_token).transfer(owner(), amount);
}
}
6 changes: 6 additions & 0 deletions publish/ABI/InflationController.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[
{
"anonymous": false,
"inputs": [],
"name": "InflationStart",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down
24 changes: 12 additions & 12 deletions publish/testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@
"bytecodeHash": "1de77fa539d4b4f383f0ff1796313249f62dd7e2e816f9b8323c21b9728b4a5e",
"lastUpdate": "Wed, 06 Dec 2023 08:50:52 GMT"
},
"InflationController": {
"innerAddress": "0x24b92bd014426d040a6f961A92F40D3dcF14b087",
"address": "0x423Ca5860df7775b37493abaF877F2B3123d844d",
"bytecodeHash": "5d3f5ec0939560ee147d7a15a0296d0c7d384e950a52679d9480345f92761c31",
"lastUpdate": "Mon, 11 Dec 2023 01:49:26 GMT"
},
"EventSyncRootTunnel": {
"innerAddress": "",
"address": "0x561d1322d2C82D516D1966e4a476d26195D5556D",
"bytecodeHash": "9326339092b0195c03b081aa5b7f062143b609abd1b07ebeaf4cbb8cc39f8222",
"lastUpdate": "Wed, 06 Dec 2023 09:07:05 GMT"
},
"PolygonDestination": {
"innerAddress": "",
"address": "0x4F45d18BB9B99413fC299BE9410DF91B5ca57538",
"bytecodeHash": "aef8893e35eda2ed0ff967f0840127e52efda5e20e45468125138ae83839c16f",
"lastUpdate": "Mon, 11 Dec 2023 03:27:51 GMT"
},
"Vesting": {
"innerAddress": "",
"address": "0x2313f40a4F8a551f42F2bD945591ee0035052C95",
"bytecodeHash": "1208cc441a58d80ec117c5204dae43ac65a0dc7b3694177820f380a62647a037",
"lastUpdate": "Mon, 11 Dec 2023 03:58:39 GMT"
},
"InflationController": {
"innerAddress": "0x4c57881890b76bA70becB2E2Ba195fEB952Baa96",
"address": "0xD3C8939EC6eD2B7a474D98A810285fEeC3c01298",
"bytecodeHash": "dfd9277a412a48549880ecfcb856649e81471b23e0aa3b05c26322b3832eb701",
"lastUpdate": "Tue, 12 Dec 2023 00:59:38 GMT"
},
"PolygonDestination": {
"innerAddress": "",
"address": "0x35d4BF80a13D6b1f5dF783CE37E1dcB37F14F654",
"bytecodeHash": "d5180aa05d3c3fdbe206f0e5abafddfab6606ccbcf91812bffe919fb6fed3567",
"lastUpdate": "Tue, 12 Dec 2023 02:27:52 GMT"
}
},
"child": {
Expand Down
4 changes: 2 additions & 2 deletions scripts/config/contracts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { utils } from "ethers";

export default {
mainnet: {
InflationController: [1000, '0x34c35136ECe9CBD6DfDf2F896C6e29be01587c0C'], // inflationRate, inflationDestination
InflationController: [10000, '0x34c35136ECe9CBD6DfDf2F896C6e29be01587c0C'], // inflationRate, inflationDestination
SQToken: [utils.parseEther("10000000000")], // initial supply 10 billion
Staking: [1209600, 1e3], // lockPeriod, unbondFeeRate
Airdropper: ['0x34c35136ECe9CBD6DfDf2F896C6e29be01587c0C'], // settle destination
Expand All @@ -26,7 +26,7 @@ export default {
DisputeManager: [utils.parseEther("10000")], // minimumDeposit
},
testnet: {
InflationController: [1000, '0x4ae8fcdddc859e2984ce0b8f4ef490d61a7a9b7f'], // inflationRate, inflationDestination
InflationController: [10000, '0x4ae8fcdddc859e2984ce0b8f4ef490d61a7a9b7f'], // inflationRate, inflationDestination
SQToken: [utils.parseEther("10000000000")], // initial supply 10 billion
Staking: [1000, 1e3], // lockPeriod, unbondFeeRate
Airdropper: ['0x4ae8fcdddc859e2984ce0b8f4ef490d61a7a9b7f'], // settle destination
Expand Down
20 changes: 17 additions & 3 deletions scripts/deployContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
TransparentUpgradeableProxy__factory,
TokenExchange,
PolygonDestination,
RootChainManager__factory,
} from '../src';
import {
CONTRACT_FACTORY,
Expand Down Expand Up @@ -226,25 +227,38 @@ export async function deployRootContracts(
});
getLogger('Deployer').info('🤞 InflationController');

let tx = await sqtToken.setMinter(inflationController.address);
await tx.wait(confirms);

//deploy vesting contract
const vesting = await deployContract<Vesting>('Vesting', 'root', { deployConfig: [deployment.root.SQToken.address] });
getLogger('Deployer').info('🤞 Vesting');

//deploy PolygonDestination contract
const polygonDestination = await deployContract<PolygonDestination>('PolygonDestination' as any, 'root',
{ deployConfig: [settingsAddress, constants.AddressZero] });
{ deployConfig: [settingsAddress, _wallet.address] });

let rootChainManager;
if (network === 'local') {
// deploy MockRootChainManager
rootChainManager = await new RootChainManager__factory(wallet).deploy();
}
// tx = await inflationController.setInflationDestination(polygonDestination.address);
// await tx.wait(confirms);

getLogger('Deployer').info('🤞 PolygonDestination');

getLogger('SettingContract').info('🤞 Set addresses');
let tx = await settings.setBatchAddress([
tx = await settings.setBatchAddress([
SQContracts.SQToken,
SQContracts.InflationController,
SQContracts.Vesting,
SQContracts.RootChainManager,
],[
sqtToken.address,
inflationController.address,
vesting.address
vesting.address,
deployment.root['RootChainManager']?.address ?? rootChainManager.address,
]);
await tx.wait(confirms);

Expand Down
8 changes: 4 additions & 4 deletions test/Airdropper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ethers, waffle } from 'hardhat';
import { Airdropper, SQToken, Settings } from '../src';
import { ZERO_ADDRESS } from './constants';
import { etherParse, futureTimestamp, lastestTime, timeTravel } from './helper';
import { deployContracts } from './setup';
import { deployRootContracts } from './setup';

// `Airdropper` only available on Kepler Network
describe.skip('Airdropper Contract', () => {
Expand All @@ -19,11 +19,11 @@ describe.skip('Airdropper Contract', () => {

beforeEach(async () => {
[wallet_0, wallet_1, wallet_2, wallet_3] = await ethers.getSigners();
const deployment = await deployContracts(wallet_0, wallet_1);
const deployment = await deployRootContracts(wallet_0, wallet_1);
airdropper = deployment.airdropper;
settings = deployment.settings;
token = deployment.token;
sqtAddress = await settings.getSQToken();
token = deployment.rootToken;
sqtAddress = token.address;
});

describe('init states check', () => {
Expand Down
Loading

0 comments on commit 8de6dfc

Please sign in to comment.