체인의정석

DelegateCall에서 값을 가져오는 케이스 본문

블록체인/Solidity

DelegateCall에서 값을 가져오는 케이스

체인의정석 2022. 7. 8. 17:51
728x90
반응형

출저 : https://ethereum.stackexchange.com/questions/111916/hardhat-test-returns-transaction-instead-of-return-value

 

Hardhat Test returns transaction instead of return value

I just came from truffle to hardhat. Like the title says, I have a return value (address) of a contract and getting the transaction back. Any idea what I can do about that? it("Check all teste...

ethereum.stackexchange.com

 

1. 조회함수의 경우

To get the return value you should add callStatic to it.

>   const shareTokenAddress = await liqminter.connect(actorA).callStatic.createPair([...])

callStatic is a read-only operation and will not consume any Ether. It simulates what would happen in a transaction, but discards all the state changes when it is done.

 

2. 이벤트를 쓰는 경우

 

You need to use Events. Here is an idea:
const tx = await contract.transfer(...args); // 100ms
const rc = await tx.wait(); // 0ms, as tx is already confirmed
const event = rc.events.find(event => event.event === 'Transfer');
const [from, to, value] = event.args;
console.log(from, to, value);

Check it out https://ethereum.stackexchange.com/a/119856/92472

이벤트를 써서 체크하는 방법이 더 좋아보긴 한다.

 

https://stackoverflow.com/questions/60248647/return-value-from-a-deployed-smart-contract-via-a-smart-contract-to-a-smart-co

 

Return value from a deployed smart contract, via a smart contract, to a smart contract

I am trying to return a value using a function of a deployed smart contract on the blockchain. pragma solidity 0.6.2; contract Caller { address cont; function changeAdd(address _change) ...

stackoverflow.com

Do you try to use abi.decode function of Solidity.

In the below example, contract B calls setName of contract A, then decodes the result by using abi.decode function.

contract A {
    string public name;
    constructor(string memory tokenName) public {
        name = tokenName;
    }

    function setName(string memory newName) public returns ( string memory){
        name = newName;
        return newName;
    }
}


contract B  {
    event Log(string msg);
    string public myName;
    function call(address addr, string memory newName) public {
        bytes memory payload = abi.encodeWithSignature("setName(string)", newName);  
        (bool success, bytes memory result)= addr.call(payload);

        // Decode data
        string memory name = abi.decode(result, (string));

        myName = name;
        emit Log(name);
    }
}

3. decode 써서 로그 기록으로 남기기

728x90
반응형
Comments