From e9281116fdc18be8acc189a35b09a094c8000edd Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Tue, 15 Jun 2021 21:21:19 +0530 Subject: [PATCH] Minor updates --- .../connectors/erc20_staking/helpers.sol | 6 +- .../connectors/erc20_staking/interface.sol | 2 +- .../mainnet/connectors/erc20_staking/main.sol | 2 +- contracts/mainnet/mapping/StakeERC20.sol | 119 ++++++++++++++++++ 4 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 contracts/mainnet/mapping/StakeERC20.sol diff --git a/contracts/mainnet/connectors/erc20_staking/helpers.sol b/contracts/mainnet/connectors/erc20_staking/helpers.sol index a00c5ccb..f559e69e 100644 --- a/contracts/mainnet/connectors/erc20_staking/helpers.sol +++ b/contracts/mainnet/connectors/erc20_staking/helpers.sol @@ -5,7 +5,7 @@ pragma experimental ABIEncoderV2; import { DSMath } from "../../common/math.sol"; import { Basic } from "../../common/basic.sol"; import { TokenInterface } from "../../common/interfaces.sol"; -import { IStakingRewards, SynthetixMapping } from "./interface.sol"; +import { IStakingRewards, StakingERC20Mapping } from "./interface.sol"; abstract contract Helpers is DSMath, Basic { @@ -34,7 +34,7 @@ abstract contract Helpers is DSMath, Basic { ) { stakingType = stringToBytes32(stakingName); - SynthetixMapping.StakingData memory stakingData = SynthetixMapping(getMappingAddr()).stakingMapping(stakingType); + StakingERC20Mapping.StakingData memory stakingData = StakingERC20Mapping(getMappingAddr()).stakingMapping(stakingType); require(stakingData.stakingPool != address(0) && stakingData.stakingToken != address(0), "Wrong Staking Name"); stakingContract = IStakingRewards(stakingData.stakingPool); stakingToken = TokenInterface(stakingData.stakingToken); @@ -42,7 +42,7 @@ abstract contract Helpers is DSMath, Basic { } function getMappingAddr() internal virtual view returns (address) { - return 0x4a56E4209F0757CE630a2ebCF45DCe5BAfcb9782; // InstaMapping Address + return 0xbE658233bA9990d86155b3902fd05a7AfC7eBdB5; // InstaMapping Address } } \ No newline at end of file diff --git a/contracts/mainnet/connectors/erc20_staking/interface.sol b/contracts/mainnet/connectors/erc20_staking/interface.sol index dad42611..d9a209fd 100644 --- a/contracts/mainnet/connectors/erc20_staking/interface.sol +++ b/contracts/mainnet/connectors/erc20_staking/interface.sol @@ -8,7 +8,7 @@ interface IStakingRewards { function balanceOf(address) external view returns(uint); } -interface SynthetixMapping { +interface StakingERC20Mapping { struct StakingData { address stakingPool; diff --git a/contracts/mainnet/connectors/erc20_staking/main.sol b/contracts/mainnet/connectors/erc20_staking/main.sol index d72201bc..176b21fa 100644 --- a/contracts/mainnet/connectors/erc20_staking/main.sol +++ b/contracts/mainnet/connectors/erc20_staking/main.sol @@ -10,7 +10,7 @@ import { TokenInterface } from "../../common/interfaces.sol"; import { Stores } from "../../common/stores.sol"; import { Helpers } from "./helpers.sol"; import { Events } from "./events.sol"; -import { IStakingRewards, SynthetixMapping } from "./interface.sol"; +import { IStakingRewards, StakingERC20Mapping } from "./interface.sol"; contract Main is Helpers, Events { diff --git a/contracts/mainnet/mapping/StakeERC20.sol b/contracts/mainnet/mapping/StakeERC20.sol new file mode 100644 index 00000000..4594d9b8 --- /dev/null +++ b/contracts/mainnet/mapping/StakeERC20.sol @@ -0,0 +1,119 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +interface ConnectorsInterface { + function chief(address) external view returns (bool); +} + +interface IndexInterface { + function master() external view returns (address); +} + +contract BytesHelper { + /** + * @dev Convert String to bytes32. + */ + function stringToBytes32(string memory str) internal pure returns (bytes32 result) { + require(bytes(str).length != 0, "String-Empty"); + // solium-disable-next-line security/no-inline-assembly + assembly { + result := mload(add(str, 32)) + } + } + + /** + * @dev Convert bytes32 to String. + */ + function bytes32ToString(bytes32 _bytes32) internal pure returns (string memory) { + bytes32 _temp; + uint count; + for (uint256 i; i < 32; i++) { + _temp = _bytes32[i]; + if( _temp != bytes32(0)) { + count += 1; + } + } + bytes memory bytesArray = new bytes(count); + for (uint256 i; i < count; i++) { + bytesArray[i] = (_bytes32[i]); + } + return (string(bytesArray)); + } +} + +contract Helpers is BytesHelper { + address public constant connectorsV2 = 0x97b0B3A8bDeFE8cB9563a3c610019Ad10DB8aD11; + address public constant instaIndex = 0x2971AdFa57b20E5a416aE5a708A8655A9c74f723; + + mapping (bytes32 => StakingData) public stakingMapping; + + struct StakingData { + address stakingPool; + address stakingToken; + address rewardToken; + } + + event LogAddStakingMapping( + string stakingName, + bytes32 stakingType, + address stakingAddress, + address stakingToken, + address rewardToken + ); + event LogRemoveStakingMapping( + string stakingName, + bytes32 stakingType, + address stakingAddress, + address stakingToken, + address rewardToken + ); + + modifier isChief virtual { + require( + ConnectorsInterface(connectorsV2).chief(msg.sender) || + IndexInterface(instaIndex).master() == msg.sender, "not-Chief"); + _; + } + + function addStakingMapping( + string memory stakingName, + address stakingAddress, + address stakingToken, + address rewardToken + ) public isChief { + require(stakingAddress != address(0), "stakingAddress-not-vaild"); + require(stakingToken != address(0), "stakingToken-not-vaild"); + require(rewardToken != address(0), "rewardToken-not-vaild"); + require(bytes(stakingName).length <= 32, "Length-exceeds"); + bytes32 stakeType = stringToBytes32(stakingName); + require(stakingMapping[stakeType].stakingPool == address(0), "StakingPool-already-added"); + require(stakingMapping[stakeType].stakingToken == address(0), "StakingToken-already-added"); + require(stakingMapping[stakeType].rewardToken == address(0), "rewardToken-already-added"); + + stakingMapping[stakeType] = StakingData( + stakingAddress, + stakingToken, + rewardToken + ); + emit LogAddStakingMapping(stakingName, stakeType, stakingAddress, stakingToken, rewardToken); + } + + function removeStakingMapping(string memory stakingName, address stakingAddress) public isChief { + require(stakingAddress != address(0), "stakingAddress-not-vaild"); + bytes32 stakeType = stringToBytes32(stakingName); + require(stakingMapping[stakeType].stakingPool == stakingAddress, "different-staking-pool"); + + emit LogRemoveStakingMapping( + stakingName, + stakeType, + stakingAddress, + stakingMapping[stakeType].stakingToken, + stakingMapping[stakeType].rewardToken + ); + delete stakingMapping[stakeType]; + } +} + +contract InstaStakingERC20Mapping is Helpers { + string constant public name = "Staking-ERC20-Mapping-v1"; +}