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

Completion function and event #19

Closed
LittleLollipop opened this issue Sep 21, 2022 · 1 comment
Closed

Completion function and event #19

LittleLollipop opened this issue Sep 21, 2022 · 1 comment

Comments

@LittleLollipop
Copy link
Contributor

The following figure is the process of successful cross-chain

obridge_circuit_succeed

This part is the contract of Ethereum, for reference

// SPDX-License-Identifier: GPL-3.0-only

pragma solidity >=0.8.0 <0.9.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract CBridge {
    using SafeERC20 for IERC20;

    enum TransferStatus {
        Null,
        Pending,
        Confirmed,
        Refunded
    }
    struct Transfer {
        address sender;
        address receiver;
        address token;
        uint256 amount;
        bytes32 hashlock; // hash of the preimage
        uint64 timelock; // UNIX timestamp seconds - locked UNTIL this time
        TransferStatus status;
    }

    mapping(bytes32 => Transfer) public transfers;

    event LogNewTransferOut(
        bytes32 transferId,
        address sender,
        address receiver,
        address token,
        uint256 amount,
        bytes32 hashlock, // hash of the preimage
        uint64 timelock, // UNIX timestamp seconds - locked UNTIL this time
        uint64 dstChainId,
        address dstAddress
    );
    event LogNewTransferIn(
        bytes32 transferId,
        address sender,
        address receiver,
        address token,
        uint256 amount,
        bytes32 hashlock, // hash of the preimage
        uint64 timelock, // UNIX timestamp seconds - locked UNTIL this time
        uint64 srcChainId,
        bytes32 srcTransferId // outbound transferId at src chain
    );
    event LogTransferConfirmed(bytes32 transferId, bytes32 preimage);
    event LogTransferRefunded(bytes32 transferId);

    /**
     * @dev transfer sets up a new outbound transfer with hash time lock.
     */
    function transferOut(
        address _bridge,
        address _token,
        uint256 _amount,
        bytes32 _hashlock,
        uint64 _timelock,
        uint64 _dstChainId,
        address _dstAddress
    ) external {
        bytes32 transferId = _transfer(_bridge, _token, _amount, _hashlock, _timelock);
        emit LogNewTransferOut(
            transferId,
            msg.sender,
            _bridge,
            _token,
            _amount,
            _hashlock,
            _timelock,
            _dstChainId,
            _dstAddress
        );
    }

    /**
     * @dev transfer sets up a new inbound transfer with hash time lock.
     */
    function transferIn(
        address _dstAddress,
        address _token,
        uint256 _amount,
        bytes32 _hashlock,
        uint64 _timelock,
        uint64 _srcChainId,
        bytes32 _srcTransferId
    ) external {
        bytes32 transferId = _transfer(_dstAddress, _token, _amount, _hashlock, _timelock);
        emit LogNewTransferIn(
            transferId,
            msg.sender,
            _dstAddress,
            _token,
            _amount,
            _hashlock,
            _timelock,
            _srcChainId,
            _srcTransferId
        );
    }

    /**
     * @dev confirm a transfer.
     *
     * @param _transferId Id of pending transfer.
     * @param _preimage key for the hashlock
     */
    function confirm(bytes32 _transferId, bytes32 _preimage) external {
        Transfer memory t = transfers[_transferId];

        require(t.status == TransferStatus.Pending, "not pending transfer");
        require(t.hashlock == keccak256(abi.encodePacked(_preimage)), "incorrect preimage");

        transfers[_transferId].status = TransferStatus.Confirmed;

        IERC20(t.token).safeTransfer(t.receiver, t.amount);
        emit LogTransferConfirmed(_transferId, _preimage);
    }

    /**
     * @dev refund a transfer after timeout.
     *
     * @param _transferId Id of pending transfer.
     */
    function refund(bytes32 _transferId) external {
        Transfer memory t = transfers[_transferId];

        require(t.status == TransferStatus.Pending, "not pending transfer");
        require(t.timelock <= block.timestamp, "timelock not yet passed");

        transfers[_transferId].status = TransferStatus.Refunded;

        IERC20(t.token).safeTransfer(t.sender, t.amount);
        emit LogTransferRefunded(_transferId);
    }

    /**
     * @dev transfer sets up a new transfer with hash time lock.
     */
    function _transfer(
        address _receiver,
        address _token,
        uint256 _amount,
        bytes32 _hashlock,
        uint64 _timelock
    ) private returns (bytes32 transferId) {
        require(_amount > 0, "invalid amount");
        require(_timelock > block.timestamp, "invalid timelock");

        transferId = keccak256(abi.encodePacked(msg.sender, _receiver, _hashlock, block.chainid));
        require(transfers[transferId].status == TransferStatus.Null, "transfer exists");

        IERC20(_token).safeTransferFrom(msg.sender, address(this), _amount);

        transfers[transferId] = Transfer(
            msg.sender,
            _receiver,
            _token,
            _amount,
            _hashlock,
            _timelock,
            TransferStatus.Pending
        );
        return transferId;
    }
}

Question

We did not find the following in the contract

  • LogNewTransferOut
  • LogNewTransferIn
  • transferOut
  • transferIn
@yanganto
Copy link
Collaborator

Hi,
This is different for different chains, and there is no event now, please check on the following issue.
solana-labs/solana#14076

The implementation to swap is different, so it is not one method to one method porting contract.
The idea is still the same with hash lock and time lock. Please check on the demo.

The status of a transfer should be read in the following way, and not like Ethereum with event listening.
https://github.com/obridge-io/atomic-swap-contracts/blob/main/solana/examples/demo.rs#L94-L101

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

No branches or pull requests

2 participants