MetaMask / metamask-docs

Developer documentation for MetaMask
https://docs.metamask.io
Apache License 2.0
636 stars 908 forks source link

Fixed example of using sendTransaction #1297

Closed shanejonas closed 2 months ago

shanejonas commented 2 months ago

Description

The code example in this provider api markdown isn't valid code. This fixes that.

github-actions[bot] commented 2 months ago

Preview published: fix/send-transaction-example

httpJunkie commented 2 months ago

This pull request corrects the structure of the code snippet by properly formatting the params object within the .request() method call.

Changes Made

Adjusted the code snippet to ensure that the params object is properly formatted within the .request() method call. Moved the params object inside the .request() method call to adhere to correct syntax.

Before:

params: [
  {
    from: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
    to: "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
    // 30400
    gas: "0x76c0",
    // 10000000000000
    gasPrice: "0x9184e72a000",
    // 2441406250
    value: "0x9184e72a",
    data: "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
  },
];

provider // Or window.ethereum if you don't support EIP-6963.
  .request({
    method: "eth_sendTransaction",
    params,
  })
  .then((result) => {
    // The result varies by RPC method.
    // For example, this method returns a transaction hash hexadecimal string upon success.
  })
  .catch((error) => {
    // If the request fails, the Promise rejects with an error.
  });

After:

provider // Or window.ethereum if you don't support EIP-6963.
  .request({
    method: "eth_sendTransaction",
    params: [
      {
        from: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
        to: "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
        // 30400
        gas: "0x76c0",
        // 10000000000000
        gasPrice: "0x9184e72a000",
        // 2441406250
        value: "0x9184e72a",
        data: "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
      },
    ]
  })
  .then((result) => {
    // The result varies by RPC method.
    // For example, this method returns a transaction hash hexadecimal string upon success.
  })
  .catch((error) => {
    // If the request fails, the Promise rejects with an error.
  });

Reason for Change

The previous version of the code snippet had the params object defined outside of the .request() method call, which is not valid syntax either.

params: [] isnt valid, should be: const params = [{stuffinhere}]

And it's best to just have it in the request object to be super clear.

This change ensures that the code is correctly structured, making it easier to understand.

github-actions[bot] commented 2 months ago

Preview published: fix/send-transaction-example