5

I've implemented use primitive_types::U256 and now I'm trying to return a U256 int as a result from one of my functions, but I'm getting the following error: the trait TypeInfo is not implemented for primitive_types::U256, I understand that I need to implement TypeInfo in my smart contract but I just don't understand how.

Here is the mentioned function:

    #[ink(message)]
    pub fn get_est_price_psp22_to_a01(
        &self,
        psp22_amount_in:Balance
    )   -> Result<U256, TradingPairErrors> {

        let caller = self.env().caller();

        //fetching caller current PSP22 balance
        let caller_current_balance:Balance = PSP22Ref::balance_of(
            &self.panx_contract,
            caller
        );

        let actual_fee:Balance;

        //calculating the actual LP fee
        match self.fee.checked_div(10u128.pow(18)) {
            Some(result) => {
                actual_fee = result;
            }
            None => {
                return Err(TradingPairErrors::Overflow);
            }
        };

        let mut amount_in_with_lp_fees:Balance;

        //reducting the LP fee from the PSP22 amount in
        match psp22_amount_in.checked_mul(100u128 - actual_fee) {
            Some(result) => {
                amount_in_with_lp_fees = result;
            }
            None => {
                return Err(TradingPairErrors::Overflow);
            }
        };

        let tokens_to_validate:Balance = 3500u128 * 10u128.pow(18);

        //validating if caller has more than 3500 PANX to verify if the caller is eligible for the incentive program 
        if caller_current_balance >= tokens_to_validate{

            if self.fee  <= 1400000000000u128 {

                //reducting HALF of the LP fee from PSP22 amount in, if the caller has more than 3500 PANX and the LP fee is less than 1.4%
                match psp22_amount_in.checked_mul(100u128 - (actual_fee / 2u128)) {
                    Some(result) => {
                        amount_in_with_lp_fees = result;
                    }
                    None => {
                        return Err(TradingPairErrors::Overflow);
                    }
                };   
            }

            if self.fee  > 1400000000000u128 {

                //reducting (LP fee - 1) of the LP fee from PSP22 amount in, if the caller has more than 3500 PANX and the LP fee is more than 1.4%
                match psp22_amount_in.checked_mul(100u128 - (actual_fee - 1u128)) {
                    Some(result) => {
                        amount_in_with_lp_fees = result;
                    }
                    None => {
                        return Err(TradingPairErrors::Overflow);
                    }
                };  
            }

        }

         
        let a0_amount_out = self.casted_div(amount_in_with_lp_fees * self.get_a0_balance(),(self.get_psp22_balance() * 100u128) + amount_in_with_lp_fees);
        
        //calculating the final A0 amount to transfer to the caller.
        //match (amount_in_with_lp_fees * self.get_a0_balance()).checked_div((self.get_psp22_balance() * 100u128) + amount_in_with_lp_fees) {
        //    Some(result) => {
        //        a0_amount_out = result;
        //    }
        //    None => {
        //        return Err(TradingPairErrors::Overflow);
        //    }
        //};

        Ok(a0_amount_out)  

    }

I'm using casted_div function which takes two u128 objects and turns them into U256:

    fn casted_div(
        &self,
        value1: u128,
        value2: u128
    ) -> U256 {

        U256::from(value1) / U256::from(value2)

    }

Thank you and have a pleasant day.

2 Answers 2

5

U256 does implement TypeInfo, but it is behind a feature: https://github.com/paritytech/parity-common/blob/master/primitive-types/src/lib.rs#L40.

Enable the `features = ["scale-info"]" for your primtive-types dependency and it should work.

1
4

It is not possible to implement a Trait on a type outside of his crate (Rust oprhan rule). So the only option is to create a new type that will wrap arround U256 and derive TypeInfo:

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct WrappedU256(U256);

impl From<WrappedU256> for U256 {
    fn from(value: WrappedU256) -> Self {
        value.0
    }
}

impl From<U256> for WrappedU256 {
    fn from(value: U256) -> Self {
        WrappedU256(value)
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.