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 OPL SDK #117

Merged
merged 10 commits into from
Feb 28, 2023
Merged

Add OPL SDK #117

merged 10 commits into from
Feb 28, 2023

Conversation

nhynes
Copy link
Contributor

@nhynes nhynes commented Feb 22, 2023

This SDK makes it very easy to use OPL. All you need to do is split your dapp and drop in the addresses.

Features:

  • minimal boilerplate code (partial message decoding, consolidated access control)
  • automatic mainnet/testnet/localnet switching
    • contracts all deployed on the same network don't need to use the bridge
  • automatic _msgSender for easier billing of gas
  • specify the home chain using name instead of id
  • mostly gas efficient (except for one place, which has been commented). depends a lot on inlining and constant evaluation for code size reduction
  • GSN integration

Here's a hello world example:

pragma solidity ^0.8.0;

import "@oasisprotocol/sapphire-contracts/contracts/opl/Host.sol";

contract ScoreNFT is Host {
    constructor(address _scorer) Host(_scorer) {
        registerEndpoint("score", _oplReceiveScore);
    }
    
    function mintScore() external {
        postMessage("genScore");
    }

    function _oplReceiveScore(bytes calldata scoreBytes) internal returns (Result) {
        _mint(_msgActor(), uint256(bytes32(scoreBytes)));
        return Result.Success;
    }
}
pragma solidity ^0.8.0;

import "@oasisprotocol/sapphire-contracts/contracts/opl/Enclave.sol";

contract Scorer is Enclave {
    constructor(address _nft) Enclave(_nft, autoswitch("ethereum")) {
        registerEndpoint("genScore", _oplGenScore);
    }

    function _oplGenScore(bytes calldata) internal returns (Result) {
        postMessage("score", bytes(bytes32(secretAlgorithm(_msgActor()))));
        return Result.Success;
    }
}

I'd also like to generate a lot of overloads for the receiver methods so you get automatic decoding, but that'll might make the compiler slow.

@nhynes nhynes force-pushed the nhynes/opl-sdk branch 2 times, most recently from 1120d57 to 91a8d99 Compare February 22, 2023 03:02
Copy link
Member

@kostko kostko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

The example above has the following in _oplReceiveScore:

_mint(msg.sender, score);

Shouldn't it instead use whom not msg.sender as the sender would probably be the relayer?

contracts/contracts/opl/Endpoint.sol Outdated Show resolved Hide resolved
contracts/contracts/opl/Endpoint.sol Outdated Show resolved Hide resolved
contracts/contracts/opl/Endpoint.sol Show resolved Hide resolved
contracts/contracts/opl/Enclave.sol Outdated Show resolved Hide resolved
Copy link
Member

@kostko kostko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, would be nice to have some E2E tests for this somewhere.

@nhynes nhynes force-pushed the nhynes/opl-sdk branch 2 times, most recently from 7a2a9cb to 7a7f4b4 Compare February 24, 2023 01:39
@nhynes nhynes merged commit a3d912c into main Feb 28, 2023
@nhynes nhynes deleted the nhynes/opl-sdk branch February 28, 2023 00:11
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.

None yet

2 participants