---
title: Contracts Module
slug: signum/contracts-module
docTags: 
createdAt: 2022-08-30T21:24:02.000Z
---

::Image[]{src="https://api.archbee.com/api/optimize/l2AIpSUU6srSQmG1-K2bb/th9l3GRFlPNtHXKIda1u-_contract.png" size="20" width="512" height="512" position="center" showCaption="false"}

| API Reference                                                                                    | [SignumJS Contracts](https://signum-network.github.io/signumjs/modules/contracts.html)                                                                             |
| ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| NPM Package                                                                                      | [@signumjs/contracts](https://www.npmjs.com/package/@signumjs/contracts)                                                                                           |
| CDN URL[](https://cdn.jsdelivr.net/npm/@signumjs/crypto@1.0.0-rc.31/dist/signumjs.crypto.min.js) | [https://cdn.jsdelivr.net/npm/@signumjs/contracts/dist/signumjs.contracts.min.js](https://cdn.jsdelivr.net/npm/@signumjs/contracts/dist/signumjs.contracts.min.js) |
| IIFE namespace                                                                                   | `sig$contracts`                                                                                                                                                    |



## Installation

:::CodeblockTabs
npm

```shell
npm i @signumjs/contracts
```

yarn

```shell
yarn add @signumjs/contracts
```

\<script/>

```html
<script src='https://cdn.jsdelivr.net/npm/@signumjs/contracts/dist/signumjs.contracts.min.js'></script>

<!--  namespace is sig$contracts -->
<script >
     // ...
     // contract fetched previously
    const view = new sig$contracts.ContractDataView(contract); 
    // using named indices is better for readability
    const ContractDataIndex = {
         Owner: 1,
         Price: 2,
         // ...
    }
    const contractOwner = view.getVariableAsDecimal(ContractDataIndex.Owner)
</script>
```
:::

## Basic Usage

The contracts module provides utility functions and classes to interact with Signum Smart Contracts, i.e. &#x20;

- generate method calls,&#x20;
- inspect contract data
- initializes a contracts data stack

The following example is an excerpt from the [example collection](https://github.com/signum-network/signumjs/tree/main/examples) in the Github repository. See the full example [here](< https://github.com/signum-network/signumjs/blob/main/examples/nodejs/advanced/contracts/viewContractData.js>).

```javascript
const {LedgerClientFactory, Address} = require('@signumjs/core')
const {ContractDataView} = require('@signumjs/contracts')

// For better readability we use name constants
const NftContractDataIndex = {
    Owner: 0,
    Status: 1,
    CurrentPrice: 2,
    PlatformAddress: 3,
    PlatformFee: 4,
    RoyaltiesFee: 5,
    RoyaltiesOwner: 6,
    HighestBidder: 19,
    TotalTimesSold: 27,
    TotalBidsReceived: 28,
    TotalRoyaltiesFee: 29,
    TotalPlatformFee: 30,
    TotalLikes: 31,
}

// using a dedicated class for contract data view is a nice pattern
class NftDataView {
    constructor(contract) {
        this.id = contract.at;
        // just pass the contract to the viewer class
        this.view = new ContractDataView(contract);
    }

    getId() {
        return this.id;
    }

    getOwnerId() {
        return this.view.getVariableAsDecimal(NftContractDataIndex.Owner);
    }

  // ... see the full example here: 
  // https://github.com/signum-network/signumjs/blob/main/examples/nodejs/advanced/contracts/viewContractData.js
}

// little helper
function asAddress(id, prefix){
    return Address.fromNumericId(id, prefix).getReedSolomonAddress()
}

async function showNftData(nftAddress) {
    try {
        // here we instantiate the ledger client
        const ledger = LedgerClientFactory.createClient({
            nodeHost: "https://europe.signum.network"
        });


        // this way we not only convert the contracts address to its id, but this method also asserts
        // that we have a formally valid address. It's not guaranteed that the address really exists in the chain
        const nftId = Address.create(nftAddress).getNumericId()

        const nftContract = await ledger.contract.getContract(nftId)
        const nftDataView = new NftDataView(nftContract)

        console.info(`Stats of NFT '${asAddress(nftId)}'`)
        console.info('----------------------------------------')
        console.info('Creator:', nftContract.creatorRS)
        console.info('Current Owner:', asAddress(nftDataView.getOwnerId(),prefix))
        console.info('Current Price:', nftDataView.getCurrentPrice().getSigna(), 'SIGNA')
        console.info('Current Status:', nftDataView.getStatus())
        console.info('Number of Sales:', nftDataView.getTotalTimesSold())
        console.info('Likes:', nftDataView.getTotalLikes())
        // ...
    } catch (e) {
        // If the API returns an exception,
        // the return error object is of type HttpError
        handleError(e);
    }
}

(async () => {
    await showNftData("S-PWUR-HWPD-3GRX-8N86V");
})();
```
