简体   繁体   English

如何使用汇编脚本转换到 yocto 附近?

[英]How to convert near to yocto using assemblyscript?

If I have an amount less than 1 NEAR let's say .5 near, how do I convert it and store it using assemblyscript in a near protocol smart contract?如果我的金额小于 1 NEAR,比如说 0.5 近,我如何转换它并将其使用汇编脚本存储在近协议智能合约中?

I tried to convert it to f64 first and make the arithmetic operation then convert it back to u128 like:我尝试先将其转换为 f64 并进行算术运算,然后将其转换回 u128,如:

u128.fromF64((ONE_NEAR.toF64() * .5))

but fromF64 gives the following error但 fromF64 给出以下错误

ExecutionError: 'WebAssembly trap: An arithmetic exception, e.g. divided by zero.'

I think you are going about this the wrong way.我认为你正在以错误的方式解决这个问题。 You need to operate on yoctoNEAR instead of 0.5 NEAR.您需要在 yoctoNEAR 而不是 0.5 NEAR 上进行操作。

Below examples are taken directly from the docs以下示例直接取自文档

https://docs.near.org/docs/api/naj-quick-reference#utils https://docs.near.org/docs/api/naj-quick-reference#utils

NEAR => yoctoNEAR NEAR => yoctoNEAR

// converts NEAR amount into yoctoNEAR (10^-24)

const { utils } = nearAPI;
const amountInYocto = utils.format.parseNearAmount("1");

YoctoNEAR => NEAR YoctoNEAR => 近

// converts yoctoNEAR (10^-24) amount into NEAR

const { utils } = nearAPI;
const amountInNEAR = utils.format.formatNearAmount("1000000000000000000000000");

basically, you have to multiply the near amount (eg 1.25) by a large number first (eg 1000000) in order to preserve the fraction before you convert the number to Yocto using toYoctob128(u128.from(amount)) .基本上,在使用toYoctob128(u128.from(amount))将数字转换为 Yocto 之前,您必须先将近似量(例如 1.25)乘以一个大数(例如 1000000)以保留分数。 and after the successful conversion, you can use u128.div() (eg u128.div(amountInU128, u128.from(1000000)) ) to get the correct value.转换成功后,您可以使用u128.div() (例如u128.div(amountInU128, u128.from(1000000)) )来获取正确的值。

PS The larger the number you multiply by first the more accurate the conversion will be but be careful not to exceed the number and u128 limit. PS你首先乘以的数字越大,转换就越准确,但注意不要超过numberu128的限制。

Here is a complete example:这是一个完整的例子:

Contract Class:

  nearToYocto(x: string): u128 {
    let amountBase = (parseFloat(x) * BASE_TO_CONVERT);
    return u128.div(toYoctob128(u128.from(amountBase)), u128.from(BASE_TO_CONVERT));
  }

utils:

export const BASE_TO_CONVERT = 1000000.0;

export function toYoctob128(amount: u128): u128 {
  return u128.mul(ONE_NEAR, amount)
}

暂无
暂无

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

相关问题 如何在 AssemblyScript 中为 NEAR 合同创建 UID? - How to create a UID in AssemblyScript for a NEAR contract? 如何在AssemblyScript / Near中打印数组的长度? - How to print the length of an array in AssemblyScript / Near? 如何使用 AssemblyScript 在 NEAR 合约上使用 PersistentUnorderedMap? PersistentUnorderedMap 初始化后没有出现在合约存储中 - How to use PersistentUnorderedMap on NEAR contract using AssemblyScript? PersistentUnorderedMap does not appear in Contract Storage after init 如何测试 function 是否在 NEAR 智能合约(AssemblyScript)中断言? - How to test if a function asserts in a NEAR smart contract (AssemblyScript)? 如何让我的 NFT 在 NEAR 钱包(AssemblyScript)中可见? - How can I make my NFT visible in the NEAR wallet (AssemblyScript)? 与接近原生的 rust/assemblyscript 相比,使用接近 evm 的 Solidity 合约有什么权衡? - What are the trade-off of using near-evm solidity contract compared to near native rust/assemblyscript? 从 Near 区块链调用 Aurora 合约时如何在 AssemblyScript 中编码参数? - How to encode arguments in AssemblyScript when calling Aurora contract from Near blockchain? 使用 u128.add() function 在汇编脚本智能合约中添加 NEAR 令牌的问题 - Issues adding NEAR tokens in assemblyscript smart contract using u128.add() function 为什么我看到错误:在构建 NEAR 智能合约(AssemblyScript)时找不到名称“解码”和“编码”? - Why am I seeing error: Cannot find name 'decode' and 'encode' when building a NEAR smart contract (AssemblyScript)? 如何在 Near 协议合约中将“AccountId”转换为“ValidAccountId”? - How to convert 'AccountId' into 'ValidAccountId' in Near protocol contracts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM