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

Merge the vanilla flavors with the typed ones #51

Closed
PaulRBerg opened this issue Sep 27, 2021 · 5 comments
Closed

Merge the vanilla flavors with the typed ones #51

PaulRBerg opened this issue Sep 27, 2021 · 5 comments

Comments

@PaulRBerg
Copy link
Owner

PaulRBerg commented Sep 27, 2021

Using Solidity v0.8.8 user defined value types!

Here's an example from the release announcement:

pragma solidity ^0.8.8;

type Decimal18 is uint256;

interface MinimalERC20 {
    function transfer(address to, Decimal18 value) external;
}

interface AnotherMinimalERC20 {
    function transfer(address to, uint256 value) external;
}

We should refactor the SD59x18 and the UD60x18 structs as user-defined value types.

@PaulRBerg PaulRBerg changed the title Merge the vanilla flavors with the typed ones [prb-math] Merge the vanilla flavors with the typed ones Sep 29, 2021
@PaulRBerg PaulRBerg changed the title [prb-math] Merge the vanilla flavors with the typed ones Merge the vanilla flavors with the typed ones Oct 20, 2021
@itsdevbear
Copy link

@PaulRBerg are you working on implementing at the moment?

@itsdevbear
Copy link

@PaulRBerg tried out a few of the functions, I can put together a more complete PR if you're interested. I am building this out regardless.

Cheers,

// SPDX-License-Identifier: MIT
pragma solidity =0.8.9;

import "@hifi-finance/prb-math/contracts/PRBMathUD60X18.sol";

/**
 * @dev Standard math utilities missing in the Solidity language.
 */
type u60x18_t is uint256;

library u60x18 {
    using PRBMathUD60x18 for uint256;

    /**
     * @dev Returns a + b
     */
    function add(u60x18_t a, u60x18_t b) internal pure returns (u60x18_t) {
        return u60x18_t.wrap(u60x18_t.unwrap(a) + u60x18_t.unwrap(b));
    }

    /**
     * @dev Returns a subtract b
     */
    function sub(u60x18_t a, u60x18_t b) internal pure returns (u60x18_t) {
        return u60x18_t.wrap(u60x18_t.unwrap(a) - u60x18_t.unwrap(b));
    }

    /**
     * @dev Returns a * b
     */
    function mul(u60x18_t a, u60x18_t b) internal pure returns (u60x18_t) {
        return u60x18_t.wrap(u60x18_t.unwrap(a).mul(u60x18_t.unwrap(b)));
    }

    /**
     * @dev Returns a / b
     */
    function div(u60x18_t a, u60x18_t b) internal pure returns (u60x18_t) {
        return u60x18_t.wrap(u60x18_t.unwrap(a).div(u60x18_t.unwrap(b)));
    }

    /**
     * @dev Returns a ^ b
     */
    function pow(u60x18_t a, u60x18_t b) internal pure returns (u60x18_t) {
        return u60x18_t.wrap(u60x18_t.unwrap(a).pow(u60x18_t.unwrap(b)));
    }

    /**
     * @dev Returns if a greater than b
     */
    function gt(u60x18_t a, u60x18_t b) internal pure returns (bool) {
        return (u60x18_t.unwrap(a) > u60x18_t.unwrap(b));
    }

    /**o
     * @dev Returns if a is less than b
     */
    function lt(u60x18_t a, u60x18_t b) internal pure returns (bool) {
        return (u60x18_t.unwrap(a) < u60x18_t.unwrap(b));
    }

    /**
     * @dev Returns if a is greater than or equal to b
     */
    function gte(u60x18_t a, u60x18_t b) internal pure returns (bool) {
        return (u60x18_t.unwrap(a) >= u60x18_t.unwrap(b));
    }

    /**
     * @dev Returns if a is less than or equal to b
     */
    function lte(u60x18_t a, u60x18_t b) internal pure returns (bool) {
        return (u60x18_t.unwrap(a) <= u60x18_t.unwrap(b));
    }

    /**
     * @dev Returns if numbers are equal.
     */
    function equals(u60x18_t a, u60x18_t b) internal pure returns (bool) {
        return (u60x18_t.unwrap(a) == u60x18_t.unwrap(b));
    }

    /**
     * @dev Returns the largest of two numbers.
     */
    function max(u60x18_t a, u60x18_t b) internal pure returns (u60x18_t) {
        return gte(a, b) ? a : b;
    }

    /**
     * @dev Returns the smallest of two numbers.
     */
    function min(u60x18_t a, u60x18_t b) internal pure returns (u60x18_t) {
        return lt(a, b) ? a : b;
    }
}

@PaulRBerg
Copy link
Owner Author

@PaulRBerg are you working on implementing at the moment?

No, I have not yet begun working on this.

I can put together a more complete PR if you're interested. I am building this out regardless.

This would be great. Thank you!

import "@hifi-finance/prb-math/contracts/PRBMathUD60X18.sol";

There is no @hifi-finance npm organization. You import PRBMath from the prb-math npm package.

@itsdevbear
Copy link

This would be great. Thank you!

Cool I will have something up in a few days.

There is no @hifi-finance npm organization. You import PRBMath from the prb-math npm package.

This is just my local brownie remapping

@PaulRBerg
Copy link
Owner Author

Done via #88.

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