Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add Tx Service data decoder guide #489

Merged
merged 8 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 1 addition & 38 deletions pages/core-api/api-safe-transaction-service.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -166,45 +166,8 @@ Supported and configured networks on `safe-eth-py`:
- [**Etherscan** configured networks](https://github.com/safe-global/safe-eth-py/blob/master/gnosis/eth/clients/etherscan_client.py#L24)
- [**Blockscout** configured networks](https://github.com/safe-global/safe-eth-py/blob/master/gnosis/eth/clients/blockscout_client.py#L21)

### Endpoints

- `POST /v1/data-decoder/` decodes a transaction `data` passed on the body for a `to` contract address.

**Example transaction decoder**

```bash
curl -X 'POST' \
'https://safe-transaction-sepolia.safe.global/api/v1/data-decoder/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-H 'X-CSRFToken: Gx1aRa8kIJGIAfReLAWwr9Q6dHv22dFt7VprdipLryHcxpfhk9aV0UDAhNz8gGYz' \
-d '{
"data": "0x095ea7b3000000000000000000000000e6fc577e87f7c977c4393300417dcc592d90acf8ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"to": "0x4127839cdf4F73d9fC9a2C2861d8d1799e9DF40C"
}'
```

Output:

```
{
"method": "approve",
"parameters": [
{
"name": "spender",
"type": "address",
"value": "0xe6fC577E87F7c977c4393300417dCC592D90acF8"
},
{
"name": "value",
"type": "uint256",
"value": "115792089237316195423570985008687907853269984665640564039457584007913129639935"
}
]
}
```

This decoded data is also included as `dataDecoded` in the `GET /multisig-transactions`, `GET /module-transactions` and `GET /all-transactions` endpoints.
The following [Guides](../core-api/transaction-service-guides/data-decoder.mdx) show how to decode contract interactions data of a transaction from the Safe Transaction Service.
tanay1337 marked this conversation as resolved.
Show resolved Hide resolved

## Running and Maintenance

Expand Down
1 change: 1 addition & 0 deletions pages/core-api/transaction-service-guides/_meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"transactions": "Transactions",
"data-decoder": "Data decoder",
"messages": "Messages",
"delegates": "Delegates"
}
109 changes: 109 additions & 0 deletions pages/core-api/transaction-service-guides/data-decoder.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { Tabs, Steps } from 'nextra/components'

# Transaction data decoder

This guide shows how to interact with the Safe Transaction Service API to decode contract interactions data of a transaction.
falvaradorodriguez marked this conversation as resolved.
Show resolved Hide resolved

The different steps are implemented using [Curl](https://github.com/curl/curl) requests and the [Safe\{Core\} SDK](https://github.com/safe-global/safe-core-sdk) TypeScript library.

## Prerequisites

1. [Node.js and npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm#using-a-node-version-manager-to-install-nodejs-and-npm) when using the Safe\{Core\} SDK.
2. Have a Safe account.

## Steps

<Steps>
### Install dependencies

{/* <!-- vale off --> */}

<Tabs items={['TypeScript', 'Python']}>
<Tabs.Tab>
```bash
yarn add @safe-global/api-kit
```
</Tabs.Tab>
<Tabs.Tab>
```bash
pip install safe-eth-py web3
```
</Tabs.Tab>
</Tabs>

{/* <!-- vale on --> */}

### Imports

{/* <!-- vale off --> */}

<Tabs items={['TypeScript', 'Python']}>
<Tabs.Tab>
```typescript
import SafeApiKit from '@safe-global/api-kit'
```
</Tabs.Tab>
<Tabs.Tab>
```python
from eth_typing import HexStr
from gnosis.eth import EthereumClient, EthereumNetwork
from gnosis.safe.api.transaction_service_api import TransactionServiceApi
```
</Tabs.Tab>
</Tabs>

{/* <!-- vale on --> */}

### Decode transaction data

{/* <!-- vale off --> */}

<Tabs items={['TypeScript', 'Python', 'Curl']}>
<Tabs.Tab>
```typescript
// Initialize the API Kit
const apiKit = new SafeApiKit({
chainId: 11155111n
})

const data = "0x095ea7b3000000000000000000000000e6fc577e87f7c977c4393300417dcc592d90acf8ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
const dataDecoded = await apiKit.decodeData(data);

// This decoded data is also included as dataDecoded in the response of the getMultisigTransactions, getModuleTransactions and getAllTransactions methods of the apiKit.
falvaradorodriguez marked this conversation as resolved.
Show resolved Hide resolved
```
</Tabs.Tab>
<Tabs.Tab>
```python
# Instantiate the Transaction Service API
ethereum_client = EthereumClient(config.get("RPC_URL"))
transaction_service_api = TransactionServiceApi(
EthereumNetwork.SEPOLIA,
ethereum_client=ethereum_client
)

# Get decoded data
data = HexStr("0x095ea7b3000000000000000000000000e6fc577e87f7c977c4393300417dcc592d90acf8ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
to = "0x4127839cdf4F73d9fC9a2C2861d8d1799e9DF40C" # Optional
data_decoded = transaction_service_api.decode_data(data, to)

# This decoded data is also included as dataDecoded in the response of the get_safe_transaction and get_transactions methods of the TransactionServiceApi.
```
</Tabs.Tab>
<Tabs.Tab>
```bash
curl -X 'POST' \
'https://safe-transaction-sepolia.safe.global/api/v1/data-decoder/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-H 'X-CSRFToken: Gx1aRa8kIJGIAfReLAWwr9Q6dHv22dFt7VprdipLryHcxpfhk9aV0UDAhNz8gGYz' \
-d '{
"data": "0x095ea7b3000000000000000000000000e6fc577e87f7c977c4393300417dcc592d90acf8ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
}'

# This decoded data is also included as dataDecoded in the GET /multisig-transactions, GET /module-transactions and GET /all-transactions endpoints.
falvaradorodriguez marked this conversation as resolved.
Show resolved Hide resolved
```
</Tabs.Tab>
</Tabs>

{/* <!-- vale on --> */}
</Steps>
Loading