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

Decimal conversion issue on solana side. #96

Closed
dmsoltech opened this issue Jul 23, 2024 · 0 comments
Closed

Decimal conversion issue on solana side. #96

dmsoltech opened this issue Jul 23, 2024 · 0 comments

Comments

@dmsoltech
Copy link

dmsoltech commented Jul 23, 2024

Hello, layerzero team. I have been working on OFT implementation between EVM and Solana side.
I am sure EVM OFT should be fine, but I am wondering if there may be some issues regarding decimal conversion calculation on the Solana side.

impl OftConfig {
    // todo: optimize
    pub fn init(
        &mut self,
        endpoint_program: Option<Pubkey>,
        admin: Pubkey,
        shared_decimals: u8,
        decimals: u8,
        accounts: &[AccountInfo],
        oapp_signer: Pubkey,
    ) -> Result<()> {
        self.admin = admin;
        self.endpoint_program = if let Some(endpoint_program) = endpoint_program {
            endpoint_program
        } else {
            ENDPOINT_ID
        };

        require!(decimals >= shared_decimals, OftError::InvalidDecimals);
        self.ld2sd_rate = 10u64.pow((decimals - shared_decimals) as u32);

        // register oapp
        oapp::endpoint_cpi::register_oapp(
            self.endpoint_program,
            oapp_signer,
            accounts,
            &[OFT_SEED, &get_oft_config_seed(self).to_bytes(), &[self.bump]],
            RegisterOAppParams { delegate: self.admin },
        )
    }

    pub fn ld2sd(&self, amount_ld: u64) -> u64 {
        amount_ld / self.ld2sd_rate
    }

    pub fn sd2ld(&self, amount_sd: u64) -> u64 {
        amount_sd * self.ld2sd_rate
    }

    pub fn remove_dust(&self, amount_ld: u64) -> u64 {
        amount_ld - amount_ld % self.ld2sd_rate
    }
}

I guess, shared_decimals should be 18, and decimals should be 6.
That's because SPL tokens almost have 6 as its decimals, while EVM tokens almost have 18.
But according to the present implementation, it always return error because of below contraint.
require!(decimals >= shared_decimals, OftError::InvalidDecimals);

And I was not sure about amountLD and amoundSD calculation inside lz_receive and send instruction.
Hope you let me know, what I had implemented incorrectly.

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

1 participant