# @ethereum-sourcify/contract-call-decoder

> Library to decode Ethereum smart contract calls into human-readable descriptions using ABI and NatSpec

Latest version **0.2.12** (published 2024-05-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ethereum-sourcify/contract-call-decoder
pnpm add @ethereum-sourcify/contract-call-decoder
yarn add @ethereum-sourcify/contract-call-decoder
bun add @ethereum-sourcify/contract-call-decoder
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.2.12 |
| Published | 2024-05-14 |
| First published | 2021-10-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 8 |
| Unpacked size | 68.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 952 |
| Maintainers | ethereumci, chriseth |
| Keywords | sourcify, ethereum, call, contract, smart-contract, metadata, solidity, ipfs |

## Links

- npm: https://www.npmjs.com/package/@ethereum-sourcify/contract-call-decoder
- Repository: https://github.com/ethereum/sourcify.git#master
- Homepage: https://github.com/ethereum/sourcify/tree/master#readme
- Issues: https://github.com/ethereum/sourcify/issues
- npm.io page: https://npm.io/package/@ethereum-sourcify/contract-call-decoder

## Dependencies (8)

- [isomorphic-fetch](https://npm.io/package/isomorphic-fetch.md) ^3.0.0
- [ethereum-provider](https://npm.io/package/ethereum-provider.md) ^0.7.7
- [@ethersproject/abi](https://npm.io/package/@ethersproject/abi.md) ^5.7.0
- [@ethersproject/bignumber](https://npm.io/package/@ethersproject/bignumber.md) ^5.7.0
- [@ethersproject/providers](https://npm.io/package/@ethersproject/providers.md) ^5.7.2
- [@ethersproject/transactions](https://npm.io/package/@ethersproject/transactions.md) ^5.7.0
- [@blossom-labs/rosette-radspec](https://npm.io/package/@blossom-labs/rosette-radspec.md) ^0.2.1
- [@ethereum-sourcify/bytecode-utils](https://npm.io/package/@ethereum-sourcify/bytecode-utils.md) ^1.2.8

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.2.12 (latest) — 2024-05-14
- 0.2.11 — 2024-03-28
- 0.2.10 — 2024-02-26
- 0.2.9 — 2024-02-22
- 0.2.8 — 2024-01-03
- 0.2.7 — 2023-12-19
- 0.2.6 — 2023-11-03
- 0.2.5 — 2023-10-19
- 0.2.4 — 2023-10-18
- 0.2.3 — 2023-09-04
- 0.2.2 — 2023-07-12
- 0.2.1 — 2022-12-16
- 0.2.0 — 2022-12-15
- 0.1.1 — 2021-11-08
- 0.1.0 — 2021-10-27

## README

# contract-call-decoder

Package to decode Ethereum transactions into human-readable format using the [ABI JSON](https://docs.soliditylang.org/en/latest/abi-spec.html#json) and the [NatSpec](https://docs.soliditylang.org/en/latest/natspec-format.html) documentation, which are both found in the [Solidity contract metadata](https://docs.soliditylang.org/en/develop/metadata.html).

The decoder will also evaluate the [NatSpec Dynamic Expressions](https://docs.soliditylang.org/en/develop/natspec-format.html#dynamic-expressions) meaning it will fill in the values of the parameters found in the call. So for the function:

```solidity
/// @dev Has to be called by the owner. The _index value `_index` can't be larger than the people array length.
function chooseFavoritePerson(uint256 _index) public returns (Person memory, uint) {
```

the decoding of `chooseFavoritePerson(1)` call will be:

```
Has to be called by the owner. The _index value 1 can't be larger than the people array length.
```

## Install

```
yarn add @ethereum-sourcify/contract-call-decoder
```

## Usage

Example below given for the `chooseFavoritePerson(3)` method of the contract `SimpleStorageNatSpec` (verified at Sepolia [0x09aFa1879fa654226D522f7099583d54ee8F18f4](https://repo.sourcify.dev/contracts/full_match/11155111/0x09aFa1879fa654226D522f7099583d54ee8F18f4/))

```ts
import {
  decodeContractCall,
  MetadataSources,
} from '@ethereum-sourcify/contract-call-decoder';

// ethers and web3 transactions are compatible
const tx = {
  to: '0x09aFa1879fa654226D522f7099583d54ee8F18f4',
  data: '0xae7cd3ce0000000000000000000000000000000000000000000000000000000000000001', // chooseFavoritePerson(3)
};

// Using metadata fetched from Sourcify API https://repo.sourcify.dev...
let decodedObj: DecodedContractCall;

// async function
decodedObj = await decodeContractCall(tx, { chainId: 11155111 });

import provider from 'eth-provider';

// Using metadata fetched from the embeded IPFS hash inside contract's bytecode
decodedObj = await decodeContractCall(tx, {
  source: MetadataSources.BytecodeMetadata,
  rpcProvider: provider('https://rpc.sepolia.dev');, // RPC Provider to fetch the contract bytecode
});
```

Returned `DecodedContractCall` is slightly different than the `userdoc` and `devdoc` output in the metadata
and grouped under "contract" and the "method" being called.

```js
// Output:
{
  "contract": {
    // @author field above the contract
    "author": "John Doe",
    // @title field above the contract
    "title": "A simple example contract to demonstrate NatSpec",
    // @dev field above the contract
    "details": "This message is intended for contract developers. Add technical details etc. here",
    // @custom:experimental
    "custom": {
      "experimental": "This is an experimental tag."
    }
  },
  "method": {
    // Required, Canonical function selector string
    "selector": "chooseFavoritePerson(uint256)",
    // Required
    "abi": {...},
    // @dev field above the function
    "details": "Has to be called by the owner. The _index value 1 can't be larger than the people array length.",
    // @param fields
    "params": {
      "_index": "The index of the favorite person"
    },
    // @return fields
    "returns": {
      "_0": "Newly chosen favorite Person",
      "_1": "The index of the new favorite Person",
    },
    // @notice field
    "notice": "Chooses the person at index 1 as the favorite person",
    // TODO: This output is incorrect?
    // Required
    "decodedParams": [
      1n,
    ],
    // @custom:status
    "custom": {
      "status": "production-ready"
    }
  }
}
```

Right now the contract-call-decoder uses `@blossom-labs/rosette-radspec` to interpret the notice, but it's experimental. This feature might change in the future.

---
_Source: https://npm.io/package/@ethereum-sourcify/contract-call-decoder · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
