Skip to content

Commit

Permalink
refactor(evm): refactors PNetwork Adapter and adds the Reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
allemanfredi committed Jan 25, 2024
1 parent 05102e8 commit 26e40a6
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 112 deletions.
57 changes: 39 additions & 18 deletions packages/evm/contracts/adapters/PNetwork/PNetworkAdapter.sol
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.17;

import { HeaderOracleAdapter } from "../HeaderOracleAdapter.sol";
import { PNetworkBase } from "./PNetworkBase.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { IERC777Recipient } from "@openzeppelin/contracts/interfaces/IERC777Recipient.sol";
import { IERC1820RegistryUpgradeable } from "@openzeppelin/contracts-upgradeable/interfaces/IERC1820RegistryUpgradeable.sol";
import { BlockHashOracleAdapter } from "../BlockHashOracleAdapter.sol";

contract PNetworkAdapter is BlockHashOracleAdapter, Ownable {
string public constant PROVIDER = "pnetwork";

address public immutable VAULT;
address public immutable TOKEN;
IERC1820RegistryUpgradeable private constant ERC1820 =
IERC1820RegistryUpgradeable(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
bytes32 private constant TOKENS_RECIPIENT_INTERFACE_HASH = keccak256("ERC777TokensRecipient");

mapping(bytes4 => address) public enabledReporters;
mapping(bytes4 => uint256) public chainIds;

contract PNetworkAdapter is HeaderOracleAdapter, PNetworkBase {
error InvalidSender(address sender, address expected);
error InvalidNetworkId(bytes4 networkId, bytes4 expected);
error InvalidToken(address token, address expected);
error UnauthorizedPNetworkReceive();

constructor(
uint256 reporterChain,
address reporterAddress,
address pNetworkVault,
address pNetworkToken,
bytes4 pNetworkReporterNetworkId
)
HeaderOracleAdapter(reporterChain, reporterAddress)
PNetworkBase(pNetworkVault, pNetworkToken, pNetworkReporterNetworkId)
{} // solhint-disable no-empty-blocks
event ReporterSet(uint256 indexed chainId, bytes4 indexed networkId, address indexed reporter);

modifier onlySupportedToken(address _tokenAddress) {
if (_tokenAddress != TOKEN) revert InvalidToken(_tokenAddress, TOKEN);
_;
}

constructor(address pNetworkVault, address pNetworkToken) {
VAULT = pNetworkVault;
TOKEN = pNetworkToken;
ERC1820.setInterfaceImplementer(address(this), TOKENS_RECIPIENT_INTERFACE_HASH, address(this));
}

// Implement the ERC777TokensRecipient interface
function tokensReceived(
Expand All @@ -28,14 +43,20 @@ contract PNetworkAdapter is HeaderOracleAdapter, PNetworkBase {
uint256,
bytes calldata data,
bytes calldata
) external override onlySupportedToken(msg.sender) {
) external onlySupportedToken(msg.sender) {
if (from != VAULT) revert InvalidSender(from, VAULT);
(, bytes memory userData, bytes4 networkId, address sender) = abi.decode(
data,
(bytes1, bytes, bytes4, address)
);
if (networkId != PNETWORK_REF_NETWORK_ID) revert InvalidNetworkId(networkId, PNETWORK_REF_NETWORK_ID);
if (sender != REPORTER_ADDRESS) revert UnauthorizedPNetworkReceive();
_receivePayload(userData);
if (enabledReporters[networkId] != sender) revert UnauthorizedPNetworkReceive();
(uint256[] memory ids, bytes32[] memory hashes) = abi.decode(userData, (uint256[], bytes32[]));
_storeHashes(chainIds[networkId], ids, hashes);
}

function setReporterByChain(uint256 chainId, bytes4 networkId, address reporter) external onlyOwner {
enabledReporters[networkId] = reporter;
chainIds[networkId] = chainId;
emit ReporterSet(chainId, networkId, reporter);
}
}
41 changes: 0 additions & 41 deletions packages/evm/contracts/adapters/PNetwork/PNetworkBase.sol

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions packages/evm/contracts/adapters/PNetwork/PNetworkMessageRelay.sol

This file was deleted.

52 changes: 40 additions & 12 deletions packages/evm/contracts/adapters/PNetwork/PNetworkReporter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,55 @@
pragma solidity ^0.8.17;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { Reporter } from "../Reporter.sol";
import { IErc20Vault } from "./interfaces/IErc20Vault.sol";
import { IPToken } from "./interfaces/IPToken.sol";
import { PNetworkBase } from "./PNetworkBase.sol";

abstract contract PNetworkReporter is PNetworkBase {
contract PNetworkReporter is Reporter, Ownable {
string public constant PROVIDER = "pnetwork";
uint256 private constant SWAP_AMOUNT = 1;

address public immutable VAULT;
address public immutable TOKEN;

mapping(uint256 => bytes4) public networkIds;

error NetworkIdNotAvailable();

event NetworkIdSet(uint256 indexed chainId, bytes4 indexed networkId);

constructor(
address headerStorage,
address yaho,
address pNetworkVault,
address pNetworkToken,
bytes4 pNetworkAdapterNetworkId
) PNetworkBase(pNetworkVault, pNetworkToken, pNetworkAdapterNetworkId) {} // solhint-disable no-empty-blocks
address pNetworkToken
) Reporter(headerStorage, yaho) {
VAULT = pNetworkVault;
TOKEN = pNetworkToken;
}

function _char(bytes1 b) internal pure returns (bytes1 c) {
if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
else return bytes1(uint8(b) + 0x57);
function setNetworkIdByChainId(uint256 chainId, bytes4 networkId) external onlyOwner {
networkIds[chainId] = networkId;
emit NetworkIdSet(chainId, networkId);
}

function _pNetworkSend(bytes memory payload, address adapter) internal {
function _dispatch(
uint256 toChainId,
address adapter,
uint256[] memory ids,
bytes32[] memory hashes
) internal override returns (bytes32) {
bytes4 networkId = networkIds[toChainId];
if (networkId == 0) revert NetworkIdNotAvailable();
bytes memory payload = abi.encode(ids, hashes);
if (VAULT != address(0)) {
IERC20(TOKEN).approve(VAULT, SWAP_AMOUNT);
IErc20Vault(VAULT).pegIn(SWAP_AMOUNT, TOKEN, _toAsciiString(adapter), payload, PNETWORK_REF_NETWORK_ID);
IErc20Vault(VAULT).pegIn(SWAP_AMOUNT, TOKEN, _toAsciiString(adapter), payload, networkId);
} else {
IPToken(TOKEN).redeem(SWAP_AMOUNT, payload, _toAsciiString(adapter), PNETWORK_REF_NETWORK_ID);
IPToken(TOKEN).redeem(SWAP_AMOUNT, payload, _toAsciiString(adapter), networkId);
}
return bytes32(0);
}

function _toAsciiString(address x) internal pure returns (string memory) {
Expand All @@ -41,4 +64,9 @@ abstract contract PNetworkReporter is PNetworkBase {
}
return string(s);
}

function _char(bytes1 b) internal pure returns (bytes1 c) {
if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
else return bytes1(uint8(b) + 0x57);
}
}

0 comments on commit 26e40a6

Please sign in to comment.