Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PolyERC20Factory contract #19

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Nemeswat
Copy link

@Nemeswat Nemeswat commented Mar 27, 2024

No description provided.

constructor(string memory name, string memory symbol) ERC20(name, symbol) UniversalChanIbcApp() {}

  function mint(address account, uint256 amount) public onlyOwner {
  function mint(address account, uint256 amount) public virtual onlyOwner {
    _mint(account, amount);
  }

  function burn(address account, uint256 amount) public onlyOwner {
  function burn(address account, uint256 amount) public virtual onlyOwner {
    _burn(account, amount);
  }
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import './UniversalChanIbcApp.sol';
import {PolyERC20} from './PolyERC20.sol';
import {receiverNotOriginPacketSender} from '@open-ibc/vibc-core-smart-contracts/contracts/libs/Ibc.sol';

contract PolyERC20FixedSupply is PolyERC20 {
  constructor(string memory _name, string memory _symbol, uint256 tokenSupply) PolyERC20(_name, _symbol) {
    _mint(msg.sender, tokenSupply);
  }

  function mint(address account, uint256 amount) public virtual override onlyOwner {
    revert('minting disabled');
  }

  function burn(address account, uint256 amount) public virtual override onlyOwner {
    revert('burning disabled');
  }
}

contract PolyERC20Factory is UniversalChanIbcApp {
  event TokenMint(address indexed token, address indexed receiver, uint256 amount);
  event BridgeSuccess(bytes32 indexed channelId);
  event BridgeFailure(bytes32 indexed channelId);

  constructor() UniversalChanIbcApp() {}

  function deployXPolyERC20(
    string[] memory channels,
    string memory _name,
    string memory _symbol,
    bytes32 _salt,
    uint256 _tokenSupply
  ) external returns (PolyERC20FixedSupply token) {
    return this.deployXPolyERC20(channels, _name, _symbol, _salt, _tokenSupply, 60);
  }

  function deployXPolyERC20(
    string[] memory channels,
    string memory _name,
    string memory _symbol,
    bytes32 _salt,
    uint256 _tokenSupply,
    uint64 timeoutSeconds
  ) external returns (PolyERC20FixedSupply token) {
    // deploy on the current chain
    token = this.deployPolyERC20(_name, _symbol, _salt, _tokenSupply);

    // deploy to other chains
    for (uint256 i = 0; i < channels.length; i++) {
      bytes memory payload = abi.encode(msg.sender, _name, _symbol, _salt, _tokenSupply);

      uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * 1_000_000_000);

      IbcUniversalPacketSender(mw).sendUniversalPacket(
        IbcUtils.toBytes32(channels[i]), IbcUtils.toBytes32(address(this)), payload, timeoutTimestamp
      );
    }
    return token;
  }

  function deployPolyERC20(
    string memory _name,
    string memory _symbol,
    bytes32 _salt,
    uint256 _tokenSupply
  ) external returns (PolyERC20FixedSupply token) {
    token = new PolyERC20FixedSupply{salt: _salt}(_name, _symbol, _tokenSupply);
    return token;
  }

  function onRecvUniversalPacket(
    bytes32 channelId,
    UniversalPacket calldata packet
  ) external override onlyIbcMw returns (AckPacket memory ackPacket) {
    if (packet.srcPortAddr != packet.destPortAddr) {
      revert receiverNotOriginPacketSender();
    }

    (address sender, string memory _name, string memory _symbol, bytes32 _salt, uint256 _tokenSupply) =
      abi.decode(packet.appData, (address, string, string, bytes32, uint256));

    PolyERC20FixedSupply token = this.deployPolyERC20(_name, _symbol, _salt, _tokenSupply);
    emit TokenMint(address(token), sender, _tokenSupply);

    return AckPacket(true, abi.encode(address(this)));
  }

  function onUniversalAcknowledgement(
    bytes32 channelId,
    UniversalPacket memory packet,
    AckPacket calldata ack
  ) external override onlyIbcMw {
    if (packet.srcPortAddr != packet.destPortAddr) {
      revert receiverNotOriginPacketSender();
    }

    (address sender, string memory _name, string memory _symbol, bytes32 _salt, uint256 _tokenSupply) =
      abi.decode(packet.appData, (address, string, string, bytes32, uint256));

    if (ack.success) {
      emit BridgeSuccess(channelId);
      (address receiver) = abi.decode(ack.data, (address));
    } else {
      emit BridgeFailure(channelId);
    }
  }

  function onTimeoutUniversalPacket(bytes32 channelId, UniversalPacket calldata packet) external override onlyIbcMw {
    if (packet.srcPortAddr != packet.destPortAddr) {
      revert receiverNotOriginPacketSender();
    }

    (address sender, string memory _name, string memory _symbol, string memory _salt, uint256 _tokenSupply) =
      abi.decode(packet.appData, (address, string, string, string, uint256));
    emit BridgeFailure(channelId);
  }
}
@@ -5,6 +5,7 @@ import 'forge-std/Script.sol';
import {IbcMwUser} from '@open-ibc/vibc-core-smart-contracts/contracts/interfaces/IbcMiddleware.sol';
import '../contracts/PolyERC20.sol';
import './XERC20Deploy.sol';
import '../contracts/PolyERC20Factory.sol';

contract PolyERC20Deploy is Script {
  using stdJson for string;
@@ -15,7 +16,7 @@ contract PolyERC20Deploy is Script {
    return keccak256(bytes(vm.envString('SALT')));
  }

  function run() external {
  function deployPolyERC20() public {
    string memory _json = vm.readFile('./solidity/scripts/xerc20-deployment-config.json');
    DeploymentConfig memory _data = abi.decode(_json.parseRaw('.'), (DeploymentConfig));

@@ -47,4 +48,51 @@ contract PolyERC20Deploy is Script {

    vm.stopBroadcast();
  }

  function deployPolyERC20WithFactory() public {
    string memory _json = vm.readFile('./solidity/scripts/xerc20-deployment-config.json');
    DeploymentConfig memory _data = abi.decode(_json.parseRaw('.'), (DeploymentConfig));

    uint256 baseFork = vm.createFork(vm.rpcUrl(vm.envString('BASE_SEPOLIA_RPC')));
    uint256 opFork = vm.createFork(vm.rpcUrl(vm.envString('OPTIMISM_SEPOLIA_RPC')));
    uint256 chainId = vm.envUint('CHAIN_ID');

    if (chainId == 84_532) {
      string[] memory channels = new string[](1);
      channels[0] = 'channel-11';

      vm.selectFork(baseFork);
      vm.startBroadcast(deployer);
      IbcMwUser baseMW = IbcMwUser(payable(vm.envAddress('BASE_UC_MW_SIM')));
      PolyERC20Factory factory = new PolyERC20Factory{salt: salt()}();
      factory.setDefaultMw(address(baseMW));
      PolyERC20FixedSupply basePolyToken =
        factory.deployXPolyERC20(channels, _data.name, _data.symbol, salt(), 1_000_000_000);

      // solhint-disable-next-line no-console
      console.log('factory deployed on Base chain at:', address(factory));
      // solhint-disable-next-line no-console
      console.log('PolyERC20 token deployed on Base chain at:', address(basePolyToken));
    }

    if (chainId == 11_155_420) {
      string[] memory channels = new string[](1);
      channels[0] = 'channel-10';

      vm.selectFork(opFork);
      vm.startBroadcast(deployer);
      IbcMwUser opMW = IbcMwUser(payable(vm.envAddress('OP_UC_MW_SIM')));
      PolyERC20Factory factory = new PolyERC20Factory{salt: salt()}();
      factory.setDefaultMw(address(opMW));

      // solhint-disable-next-line no-console
      console.log('factory deployed on Optimism chain at:', address(factory));
    }

    vm.stopBroadcast();
  }

  function run() external {
    deployPolyERC20();
  }
}
@Nemeswat Nemeswat changed the title PolyxERC20 add PolyERC20Factory contract Mar 27, 2024
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0; 

import './UniversalChanIbcApp.sol';
import {PolyERC20} from './PolyERC20.sol';
import {receiverNotOriginPacketSender} from '@open-ibc/vibc-core-smart-contracts/contracts/libs/Ibc.sol';

contract PolyERC20FixedSupply is PolyERC20 {
  constructor(string memory _name, string memory _symbol, uint256 tokenSupply) PolyERC20(_name, _symbol) {
    _mint(msg.sender, tokenSupply);
  }

  function mint(address account, uint256 amount) public virtual override onlyOwner {
    revert('minting disabled');
  }

  function burn(address account, uint256 amount) public virtual override onlyOwner {
    revert('burning disabled');
  }
}

contract PolyERC20Factory is UniversalChanIbcApp {
  event TokenMint(address indexed token, address indexed receiver, uint256 amount);
  event BridgeSuccess(bytes32 indexed channelId);
  event BridgeFailure(bytes32 indexed channelId);

  constructor() UniversalChanIbcApp() {}

  function deployXPolyERC20(
    string[] memory channels,
    string memory _name,
    string memory _symbol,
    bytes32 _salt,
    uint256 _tokenSupply
  ) external returns (PolyERC20FixedSupply token) {
    return this.deployXPolyERC20(channels, _name, _symbol, _salt, _tokenSupply, 60);
  }

  function deployXPolyERC20(
    string[] memory channels,
    string memory _name,
    string memory _symbol,
    bytes32 _salt,
    uint256 _tokenSupply,
    uint64 timeoutSeconds
  ) external returns (PolyERC20FixedSupply token) {
    // deploy on the current chain
    token = this.deployPolyERC20(_name, _symbol, _salt, _tokenSupply);

    // deploy to other chains
    for (uint256 i = 0; i < channels.length; i++) {
      bytes memory payload = abi.encode(msg.sender, _name, _symbol, _salt, _tokenSupply);

      uint64 timeoutTimestamp = uint64((block.timestamp + timeoutSeconds) * 1_000_000_000);

      IbcUniversalPacketSender(mw).sendUniversalPacket(
        IbcUtils.toBytes32(channels[i]), IbcUtils.toBytes32(address(this)), payload, timeoutTimestamp
      );
    }
    return token;
  }

  function deployPolyERC20(
    string memory _name,
    string memory _symbol,
    bytes32 _salt,
    uint256 _tokenSupply
  ) external returns (PolyERC20FixedSupply token) {
    token = new PolyERC20FixedSupply{salt: _salt}(_name, _symbol, _tokenSupply);
    return token;
  }

  function onRecvUniversalPacket(
    bytes32 channelId,
    UniversalPacket calldata packet
  ) external override onlyIbcMw returns (AckPacket memory ackPacket) {
    if (packet.srcPortAddr != packet.destPortAddr) {
      revert receiverNotOriginPacketSender();
    }

    (address sender, string memory _name, string memory _symbol, bytes32 _salt, uint256 _tokenSupply) =
      abi.decode(packet.appData, (address, string, string, bytes32, uint256));

    PolyERC20FixedSupply token = this.deployPolyERC20(_name, _symbol, _salt, _tokenSupply);
    emit TokenMint(address(token), sender, _tokenSupply);

    return AckPacket(true, abi.encode(address(this)));
  }

  function onUniversalAcknowledgement(
    bytes32 channelId,
    UniversalPacket memory packet,
    AckPacket calldata ack
  ) external override onlyIbcMw {
    if (packet.srcPortAddr != packet.destPortAddr) {
      revert receiverNotOriginPacketSender();
    }

    (address sender, string memory _name, string memory _symbol, bytes32 _salt, uint256 _tokenSupply) =
      abi.decode(packet.appData, (address, string, string, bytes32, uint256));

    if (ack.success) {
      emit BridgeSuccess(channelId);
      (address receiver) = abi.decode(ack.data, (address));
    } else {
      emit BridgeFailure(channelId);
    }
  }

  function onTimeoutUniversalPacket(bytes32 channelId, UniversalPacket calldata packet) external override onlyIbcMw {
    if (packet.srcPortAddr != packet.destPortAddr) {
      revert receiverNotOriginPacketSender();
    }

    (address sender, string memory _name, string memory _symbol, string memory _salt, uint256 _tokenSupply) =
      abi.decode(packet.appData, (address, string, string, string, uint256));
    emit BridgeFailure(channelId);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant