Как получить возвращаемое значение функции умного контракта, когда она вызывается с помощью cacheSend() в drizzle

Я просто попытался создать простой DApp, используя трюфель, реакцию и дождь. Мой вопрос заключается в том, как получить возвращаемое значение функции смарт-контракта, когда она вызывается с помощью cacheSend() в моросящий дождь? Как и ожидалось, при вызове функций cacheCall() и cacheSend() в контракте они должны отправить требуемую транзакцию и вернуть соответствующий хэш транзакции.

В моем коде cacheCall() отправляет желаемую транзакцию и возвращает соответствующий хэш транзакции, и когда я его консолью.log, это отображается на консоли. так что с cacheCall() проблем нет

// this works fine
const dataKey1 = contract.methods.GetNumber.cacheCall();
console.log(dataKey1);

Но cacheSend() отправляет нужную транзакцию, но не возвращает хэш соответствующей транзакции!! Вместо этого он возвращает значение 0, когда я console.log возвращаю значение cacheSend()

// this does not work
const dataKey2 = contract.methods.setNumber.cacheSend(3);
console.log(dataKey2);

Вот умный контракт:

pragma solidity ^0.5.0;

contract Test {

    uint public number = 10;

    function setNumber(uint _x) public returns (uint)
    {
        number = number+_x;

        return number + 5;
    }

    function GetNumber() public view returns (uint)
    {
        return number;
    }
}

Внешний интерфейс должен выглядеть следующим образом:

The return value of GetNumber function = 13 
The return value of setNumber function = 18

Это код

import React from "react";

class TestDrizzle extends React.Component {
  state = { dataKey1: null, dataKey2: null };

  componentDidMount() {
    const { drizzle, drizzleState } = this.props;
    const contract = drizzle.contracts.Test;

    // let drizzle know we want to call the `GetNumber` method with `value`
    // this works fine
    const dataKey1 = contract.methods.GetNumber.cacheCall();
    console.log(dataKey1);

    // let drizzle know we want to call the `setNumber` method with `value`
    // this does not work
    const dataKey2 = contract.methods.setNumber.cacheSend(3);
    console.log(dataKey2);


    this.setState({ dataKey1, dataKey2 });
  }

  render() {
    // get the contract state from drizzleState
    const { Test } = this.props.drizzleState.contracts;

    // using the saved `dataKey1`, get the return value of GetNumber function
    const result_1 = Test.GetNumber[this.state.dataKey1];

    // using the saved `dataKey2`, get the return value of setNumber function
    const result_2 = Test.setNumber[this.state.dataKey2];

    return (
      <p>
        The return value of GetNumber function = {result_1 && result_1.value}{" "}
        {<br />}
        The return value of setNumber function = {result_2 && result_2.value}
      </p>
    );
  }
}

export default TestDrizzle;

Буду признателен, если вы посмотрите на мой проект https://github.com/fbalwy/Drizzle-tutorial.git

0 ответов

Другие вопросы по тегам