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

Static ABI for Exchange #16

Closed
abandeali1 opened this issue Dec 19, 2017 · 0 comments
Closed

Static ABI for Exchange #16

abandeali1 opened this issue Dec 19, 2017 · 0 comments

Comments

@abandeali1
Copy link
Member

Summary

Given that 0x is intended to be upgraded via decentralized governance, we need to make it as simple as possible for higher level protocols built using the 0x smart contracts to migrate to new versions. Currently, any change in the Exchange contract's ABI would require major upgrades in any smart contracts that use it. With a completely static ABI, any smart contracts that use Exchange can upgrade by simply updating a pointer to the new Exchange address, rather deploying a brand new contract.

Motivation

A static ABI allows allows for much more backwards compatability, making it much easier to upgrade.

Implementation

Rather than making all of the Exchange methods take typed arguments, we can always pass in a dynamic byte array. This way, changes to the arguments will not actually change the contract's ABI.

An example of what fillOrder might look like:

contract Exchange {
    struct Order {
        address maker;
        address taker;
        address makerToken;
        address takerToken;
        address feeRecipient;
        uint makerTokenAmount;
        uint takerTokenAmount;
        uint makerFee;
        uint takerFee;
        uint expirationTimestampInSec;
    }

    // @param data Arguments converted to hex, padded to 32 bytes, and concatenated.
    function fillOrder(bytes memory data)
        public
        returns (uint)
    {
        Order memory order = getOrderFromData(data);
        // remaining fill logic...
    }

    function getOrderFromData(bytes memory data)
        public
        pure
        returns (Order memory order)
    {
        address maker;
        address taker;
        address makerToken;
        address takerToken;
        address feeRecipient;
        uint makerTokenAmount;
        uint takerTokenAmount;
        uint makerFee;
        uint takerFee;
        uint expirationTimestampInSec;
        assembly {
            maker := mload(data)
            taker := mload(add(data, 32))
            makerToken := mload(add(data, 64))
            takerToken := mload(add(data, 96))
            feeRecipient := mload(add(data, 128))
            makerTokenAmount := mload(add(data, 160))
            takerTokenAmount := mload(add(data, 192))
            makerFee := mload(add(data, 224))
            takerFee := mload(add(data, 256))
            expirationTimestampInSec := mload(add(data, 288))
        }
        order = Order({
            maker: maker,
            taker: taker,
            makerToken: makerToken,
            takerToken: takerToken,
            feeRecipient: feeRecipient,
            makerTokenAmount: makerTokenAmount,
            takerTokenAmount: takerTokenAmount,
            makerFee: makerFee,
            takerFee: takerFee,
            expirationTimestampInSec: expirationTimestampInSec
        });
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant