简体   繁体   English

从 Near 区块链调用 Aurora 合约时如何在 AssemblyScript 中编码参数?

[英]How to encode arguments in AssemblyScript when calling Aurora contract from Near blockchain?

I'm trying to call a contract located in Aurora from a contract located in Near.我正在尝试从位于 Near 的合同中调用位于 Aurora 的合同。 I'm using AssemblyScript and I'm struggling with passing arguments to the Aurora contract itself.我正在使用 AssemblyScript,并且正在努力将参数传递给 Aurora 合约本身。 I receive ERR_BORSH_DESERIALIZE panic from the Aurora contract.我收到来自 Aurora 合约的ERR_BORSH_DESERIALIZE恐慌。 Can anybody help me with figuring out how I would encode arguments?谁能帮我弄清楚我将如何编码参数? Here is sample code:这是示例代码:

import { BorshSerializer } from '@serial-as/borsh'

@serializable
class FunctionCallArgs {
  contract: Uint8Array;
  input: Uint8Array;
}

export function myFunction(): void {
  const args: FunctionCallArgs = {
    contract: util.stringToBytes(contractAddress),
    input: util.stringToBytes(abiEncodedFn),
  };
  const argsBorsh = BorshSerializer.encode(args);

  ContractPromise.create("aurora", "call", argsBorsh, 100);
}

I managed to find a solution.我设法找到了解决方案。 The flow of calling the contract was right, however I had two errors in implementation.调用合约的流程是正确的,但是我在执行中有两个错误。

  1. Wrong conversion of the contract address to 20 byte array.合约地址到 20 字节数组的错误转换。 My custom implementation of the function a bit verbose, so here is a single line JS script that does the same:我对该函数的自定义实现有点冗长,所以这是一个执行相同操作的单行 JS 脚本:
Buffer.from(contractAddress.substring(2), 'hex') // removing 0x prefix is mandatory
  1. "@serial-as/borsh" doesn't deserialize fixed length arrays. "@serial-as/borsh" 不会反序列化固定长度的数组。 So I had to convert contractAddress (which is Uint8Array after converting it to bytes in 1st point) to StaticArray(20), like this:所以我必须将contractAddress(在第一个点将其转换为字节后为Uint8Array)转换为StaticArray(20),如下所示:
const contract = hexToBytes(tokenAddress).reduce((memo, v, i) => {
  memo[i] = <u8>v;
  return memo;
}, new StaticArray<u8>(20);

And finally monkey-patched "encode_static_array" method in the library to not allocate space before adding bytes to buffer.最后在库中猴子修补的“encode_static_array”方法在将字节添加到缓冲区之前不分配空间。 So removed:所以删除:

encode_static_array<T>(value: StaticArray<T>): void {
  ...
  this.buffer.store<u32>(value.length); // remove this line
  ...
}

暂无
暂无

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

相关问题 为什么我看到错误:在构建 NEAR 智能合约(AssemblyScript)时找不到名称“解码”和“编码”? - Why am I seeing error: Cannot find name 'decode' and 'encode' when building a NEAR smart contract (AssemblyScript)? 如何在 AssemblyScript 中为 NEAR 合同创建 UID? - How to create a UID in AssemblyScript for a NEAR contract? 如何测试 function 是否在 NEAR 智能合约(AssemblyScript)中断言? - How to test if a function asserts in a NEAR smart contract (AssemblyScript)? 如何使用 AssemblyScript 在 NEAR 合约上使用 PersistentUnorderedMap? PersistentUnorderedMap 初始化后没有出现在合约存储中 - How to use PersistentUnorderedMap on NEAR contract using AssemblyScript? PersistentUnorderedMap does not appear in Contract Storage after init "如何从合约调用(AssemblyScript)中获取交易哈希?" - How can I get the transaction hash from a contract call (AssemblyScript)? 如何在AssemblyScript / Near中打印数组的长度? - How to print the length of an array in AssemblyScript / Near? 如何使用汇编脚本转换到 yocto 附近? - How to convert near to yocto using assemblyscript? 与接近原生的 rust/assemblyscript 相比,使用接近 evm 的 Solidity 合约有什么权衡? - What are the trade-off of using near-evm solidity contract compared to near native rust/assemblyscript? 使用 u128.add() function 在汇编脚本智能合约中添加 NEAR 令牌的问题 - Issues adding NEAR tokens in assemblyscript smart contract using u128.add() function Near-example/workshop--exploring-assemblyscript-contracts/assembly/A.sample-projects/04.token-contract/ - near-example/workshop--exploring-assemblyscript-contracts/assembly/A.sample-projects/04.token-contract/
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM