Sign and Broadcast Transaction

To complete any staking or withdrawal operation on Osmosis, sign the unsigned transaction provided by the Staking API and broadcast it to the network.

1. Prepare the transaction

Retrieve an unsigned serialized transaction from the relevant API endpoint, e.g., staking/stake or staking/withdraw.

2. Sign the transaction

Sign the transaction using your preferred signing method or the example of code below.

   import { SignerFactory, Signers } from "@p2p-org/signer-sdk";

   const signer = SignerFactory.createSigner(Signers.Osmosis, {
     osmosisNodeRpc: "https://osmosis-node-rpc.com",
     networkName: "osmosis-testnet",
     osmosisAddress: "osmo1exampleaddress",
     osmosisMnemonic: "your osmosis mnemonic here",
   });

   const transactionData = {
     // your transaction data here, e.g.
     accountNumber: "123",
     chainId: "osmosis-testnet",
     fee: {
       amount: [{ amount: "1000", denom: "uosmo" }],
       gas: "200000",
     },
     msgs: [
       {
         type: "osmosis/stake",
         value: {
           delegator_address: "osmo1exampleaddress",
           validator_address: "osmovaloper1examplevalidator",
           amount: {
             amount: "1000000",
             denom: "uosmo",
           },
         },
       },
     ],
     sequence: "1",
   };

   signer.sign(transactionData).then((signedTx) => console.log(signedTx));
  1. Broadcast the signed transaction to the Osmosis network by making a POST request to /api/v2/osmosis/{network}/transactions/send.

    Example request (for osmo-test-5 network):

    curl --request POST \
         --url https://api-test.p2p.org/api/v2/osmosis/osmo-test-5/transactions/send \
         --header 'accept: application/json' \
         --header 'authorization: Bearer <token>' \
         --header 'content-type: application/json' \
         --data '
         {
      	 "signedTransaction": "0a9f010a9c010a372f636f736d6f732e646973747269627574696f6e2e763162657461312e4d7367576974686472617744656c656761746f7252657761726412610a2b6f736d6f3166336475787533747274706d6c6b716b76746837373734767770616e6d77716e30666c64723712326f736d6f76616c6f7065723163797439333079783078636d707936386b32363063646c72743867367634336434617432373012680a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2103a3f4504804072284290dfa67038a17cbfd63c8da8dd51100b4adc4c1884ba3c112040a020801180b12140a0e0a05756f736d6f1205313230323910c3d70e1a400b3f00d8ca7816adc29fa60e303681091c0fb670d2d6695bb995944cbe7e76c25cbda3339578e13114104901ed25d7e1cc36aa3d54d2ba566a469fc366ca5d1f"
    		 }'
    • signedTransaction — signed transaction in Base64 encrypted format which needs to be broadcast to the network.

    Example response:

    {
        "error": null,
        "result": {
            "status": "success",
            "blockId": 42125690,
            "fee": 0.012029,
            "gas": {
                "used": 164234,
                "wanted": 240579
            },
            "transactionHash": "55B9D6E2A68B7E10BDE74F8E5982AE8B1D6EF0AD58D479F1007220029243CDB0"
        }
    }
    • status — transaction status: success, failed.
    • blockId — unique identifier of the block in which the transaction has been included.
    • fee — total fee in OSMO charged for processing the transaction.
    • gas — computational effort required to execute the transaction, measured in gas units.
      • used — amount of gas spent for the transaction.
      • wanted — maximum gas limit that the transaction initiator was willing to consume for the transaction.
    • transactionHash — hash of the transaction.

    What's next?