简体   繁体   English

在智能合约中将以太坊 uint256 转换为 Solana Rust u64

[英]Convert Ethereum uint256 to Solana Rust u64 in smart contract

How can I convert Ethereum uint256 variables into Solana Rust?如何将以太坊 uint256 变量转换为 Solana Rust?

Should I use u64 and 9 decimals in Solana Rust instead of 18 decimals in Ethereum?我应该在 Solana Rust 中使用 u64 和 9 位小数,而不是在以太坊中使用 18 位小数吗?

According to Solana's doc, a lamport has a value of 0.000000001 SOL.根据 Solana 的文档,灯的值为 0.000000001 SOL。 That is 9 decimals.那是9位小数。

So it seems 9 decimals is the standard in Solana, in contrast of 18 decimals in Ethereum.所以看起来 9 位小数是 Solana 的标准,与以太坊的 18 位小数形成对比。

u64 in Rust has the maximum value of 18,446,744,073,709,551,615... Rust中的u64最大值为18,446,744,073,709,551,615...

u128 in Rust has the maximum value of 2^128 - 1 Rust 中的 u128 最大值为 2^128 - 1

Somehow Rust Solana does not have u256!不知何故Rust Solana没有u256! Strange奇怪的

and if I use 9 decimals for all calculations, then I should make sure 18,446,744,073 is big enough for all my Solana Rust u64 variables' maximum values, right?如果我在所有计算中使用 9 位小数,那么我应该确保 18,446,744,073 对于我所有的 Solana Rust u64 变量的最大值都足够大,对吧? Is this the correct strategy?这是正确的策略吗? Thank you!谢谢!

I think we do not need additional library like uint256, because the Solana Program Library(their token library) is using u64 for amount to transfer https://github.com/solana-labs/solana-program-library/blob/master/token/program/src/processor.rs#L203我认为我们不需要像 uint256 这样的额外库,因为 Solana 程序库(他们的令牌库)正在使用 u64 来传输https://github.com/solana-labs/solana-program-library/blob/master/令牌/程序/src/processor.rs#L203

uint128 in Rust is actually large enough. Rust 中的 uint128 实际上足够大。

uint128 has a range of 0 to 2^128 - 1, that is 0 ~ 340,282,366,920,938,463,463,374,607,431,768,211,455 uint128 的范围是 0 到 2^128 - 1,即 0 到 340,282,366,920,938,463,463,374,607,431,768,211,455

with 9 decimals, uint128 will be large enough.使用 9 位小数,uint128 就足够大了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM