【问题标题】:Retreive a struct/list of structs as views from a smart contract从智能合约中检索结构/结构列表
【发布时间】:2021-12-07 17:22:14
【问题描述】:

我试图在智能合约的视图方法中获取单个结构的数据和该结构列表的数据。 结构类似于:

#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi, Clone)]
pub struct Stream<M: ManagedTypeApi> {
    pub id: u64,
    pub payment: BigUint<M>,
    pub enddate: u64,
    pub receiver: ManagedAddress<M>,
}

单个视图应该是这样的:

#[view(getStream)]
fn get_stream(&self, id: u64) -> Stream<Self::Api> {
    let payment = self.payment( id.clone() ).get().clone();
    let enddate = self.enddate( id.clone() ).get().clone();
    let receiver = self.receiver( id.clone() ).get().clone();
    Stream {
        id,
        payment,
        enddate,
        receiver,
    }
}

在 mandos 测试中,我期望类似:

"expect": {
    "out": [
        "u64:1",
        "100,000,000,000",
        "u64:200,000",
        "address:my_address"
    ]
],

但在测试中我总是得到一个未编码的字节结果,例如:

Want: ["u64:1", "100,000,000,000", "u64:200,000", "address:my_address"]. Have: [0x000000000000000100000005174876e8000000000000030d406d795f616464726573735f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f5f]

我还尝试了不同的返回类型,例如 ManagedMultiResultVecManagedMultiResultVecMultiResultManagedVec。但似乎都为我产生了这个输出。

我也不知道如何使用 erdjs lib 在 TypeScript 中的 dApp 中检索和解码这样的结果。

谁能告诉我我错过了什么?

【问题讨论】:

    标签: elrond


    【解决方案1】:

    在 mandos 中,你应该期待这样的结果:

    ["u64:1|biguint:100,000,000,000|u64:200,000|address:my_address"]
    

    或者

    {
    "0id": "u64:1",
    "1payment": "biguint:100,000,000,000",
    "2enddate": "u64:200,000",
    "3receiver": "address:my_address"
    }
    

    我认为应该是对的。

    而在 Dapp 中,你需要合约的 ABI 文件,并且需要执行以下操作:

    const result = ...; // do transaction here
    
    const abi = await SmartContractAbi.fromAbiPath('...abi.json');
    
    result.setEndpointDefinition(abi.getEndpoint('get_stream'));
    
    console.log(result.unpackOutput());
    

    从那里你可以弄清楚如何转换结果。

    【讨论】:

    • 很有魅力。你是英雄:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2021-01-14
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多