Nethereum / Nethereum

Ethereum .Net cross platform integration library
http://nethereum.com
MIT License
2.17k stars 725 forks source link

Using csharp code generator with function requiring "from" #683

Closed maiorfi closed 3 years ago

maiorfi commented 3 years ago

It seems that "from" address parameter can be used only with "function class" generated code, while it could be useful to use the leaner mode represented by methods exposed by service (i.e. the ones that do not return transaction receipts).

Is there anything I am missing?

Thanks!

juanfranblanco commented 3 years ago

@maiorfi can you post some example as I don't know what you mean

maiorfi commented 3 years ago

Taking for example a smart contract defined as:

pragma solidity >=0.7.0 <0.9.0;

contract DocHashStorage {

    struct DocData {
        string doc_name;
        string doc_type;
        string doc_hash;
    }

    mapping(string => DocData) private _docMap;

    function storeDoc(string calldata docId, string calldata docName, string calldata docType, string calldata docHash) public {

        DocData storage dd = _docMap[docId];

        dd.doc_hash = docHash;
        dd.doc_name = docName;
        dd.doc_type = docType;
    }

    function retrieveDoc(string calldata docId) public view returns (DocData memory){

        return _docMap[docId];
    }

    function deleteDoc(string calldata docId) public {

        delete _docMap[docId];
    }
}

Instead of generating this:

public Task<string> StoreDocRequestAsync(string docId, string docName, string docType, string docHash)
        {
            var storeDocFunction = new StoreDocFunction();

            storeDocFunction.DocId = docId;
            storeDocFunction.DocName = docName;
            storeDocFunction.DocType = docType;
            storeDocFunction.DocHash = docHash;

            return ContractHandler.SendRequestAsync(storeDocFunction);
        }

...it would be useful to have something that takes a "from" parameter, too, since it is required (if I am not missing anything), like in:

public Task<string> StoreDocRequestAsync(string docId, string docName, string docType, string docHash, string fromAddress=null)
        {
            var storeDocFunction = new StoreDocFunction();
            storeDocFunction.DocId = docId;
            storeDocFunction.DocName = docName;
            storeDocFunction.DocType = docType;
            storeDocFunction.DocHash = docHash;

            if (fromAddress != null)
            {
                storeDocFunction.FromAddress = fromAddress;
            }

            return ContractHandler.SendRequestAsync(storeDocFunction);
        }
juanfranblanco commented 3 years ago

Ah I got you now, yeah maybe in the next release as things will change with 1559.

juanfranblanco commented 3 years ago

@maiorfi Also note that if From is not set it will use the Account address, used to initialised Web3. Now, most important those need to match anyway as the txn will be signed with that. Edit: The from is handy when there is a call method and checks the msg.sender.

juanfranblanco commented 3 years ago

Also note that with eip1559 we will have more parametes, and if we end up having transaction types, this will not fit in here as it will end up being a small mess.

maiorfi commented 3 years ago

Ok, thanks.