multiversx / mx-specs

MultiversX specs documentation.
https://elrond.com
GNU General Public License v3.0
33 stars 33 forks source link

Meta Transaction Examples. #10

Closed loic-combis closed 1 year ago

loic-combis commented 2 years ago

Hi there!

Currently building a dApp on Elrond, I'm really interested in using meta-transactions explained here: https://github.com/ElrondNetwork/elrond-specs/blob/main/sc-meta-transactions.md. Unfortunately, I couldn't find a real-world example. Is there any documentation that shows how such a transaction can be made with one of your SDKs?

Thanks so much!

HmFlashy commented 2 years ago

Hello, glad there is already an issue about that. I am also looking for an example or documentation to help me get through meta transactions Could someone help on this subject? Thanks elrond, keep the good work !

MWFIAE commented 2 years ago

Here would be a function that creates a meta transaction out of a valid (signed) inner transaction.

    /**
     * Generate a relay transaction to pay the gas for our users.
     *
     * @param {Transaction} innerTransaction The transaction to relay
     * @returns {Transaction} Transaction containing the relayed transaction
     * @memberof ElrondService
     */
    public buildRelayedTransaction(innerTransaction: Transaction): Transaction {
        const serializedTransaction = {
            nonce: innerTransaction.getNonce().valueOf(),
            value: parseInt(innerTransaction.getValue().toString()),
            receiver: hexToBase64(innerTransaction.getReceiver().hex()),
            sender: hexToBase64(innerTransaction.getSender().hex()),
            gasPrice: innerTransaction.getGasPrice().valueOf(),
            gasLimit: innerTransaction.getGasLimit().valueOf(),
            data: innerTransaction.getData().encoded(),
            chainID: utf8ToBase64(innerTransaction.getChainID().valueOf()),
            version: innerTransaction.getVersion().valueOf(),
            signature: hexToBase64(innerTransaction.getSignature().hex()),
        };

        const payload = TransactionPayload.contractCall()
            .setFunction(new ContractFunction("relayedTx"))
            .addArg(BytesValue.fromUTF8(JSON.stringify(serializedTransaction)))
            .build();

        const transaction = new Transaction({
            receiver: innerTransaction.getSender(),
            value: Balance.Zero(),
            gasLimit: new GasLimit(
                NetworkConfig.getDefault().MinGasLimit.valueOf() +
                    innerTransaction.getGasLimit().valueOf() +
                    payload.length() * NetworkConfig.getDefault().GasPerDataByte
            ),
            data: payload,
        });

        return transaction;
    }

But to keep this issue alive: I would be very interested in how to correctly construct a relayedTxV2 🙏 Got signature errors with the way I tried. I'm assuming that my gas calculation might be of or I sign the wrong transaction data somehow 🤔

elrond-developer commented 2 years ago

Hello community/team,

I would like to inquire about this feature as well so that I may create a demo video for the rest of the community to use.

Thank you.

MWFIAE commented 2 years ago

Hi @elrond-developer

for relaytx v1 you can use the function I posted above.

For relaytx v2 there is now a utility in erdjs you can use 🥳 https://github.com/ElrondNetwork/elrond-sdk-erdjs/blob/main/src/relayedTransactionV2Builder.ts

https://github.com/ElrondNetwork/elrond-sdk-erdjs/blob/main/src/relayedTransactionV2Builder.spec.ts

Note that relaytx v2 can only be used if you want to make a smart contract call or esdt transfers. It's not possible to send egld via them :)

elrond-developer commented 2 years ago

Thanks Martin,

So we cant send egld in relaytx v2, but i presume that we can send wrapped egld? in addition, although we cannot send egld via relaytx v2, the relayer will still be able to pay in egld for the tx fee for the requester correct?

MWFIAE commented 2 years ago

@elrond-developer Yes wrapped egld are an ESDT so they can be sent via relay transactions :) Also yes, the gas fees will be paid by the relayer with his egld :) (I mean that's the point of it all in the first place😄)