# Encoding

### cheque data encoding

<table><thead><tr><th width="110">Field Name</th><th width="89">Length</th><th width="97">Bit Index</th><th width="92">Reverse</th><th width="97">Mask</th><th>Note</th></tr></thead><tbody><tr><td>distributionId</td><td>32</td><td>0+32</td><td>224</td><td>4294967295</td><td>Bound distribution ID; uniquely identifies a distribution activity.</td></tr><tr><td>participationLimit</td><td>8</td><td>32+8</td><td>216</td><td>255</td><td>Participant restriction: 1. Any user, 2. Checked, 3. Others. Determine who can participate in the distribution activity.</td></tr><tr><td>minHardCap</td><td>8</td><td>40+8</td><td>208</td><td>255</td><td>Minimum hard cap value for the system, a positive integer from 1 to 100, where 1 equals 1/100; minimum funding target for the distribution activity.</td></tr><tr><td>maxHardCap</td><td>8</td><td>48+8</td><td>200</td><td>255</td><td>Maximum hard cap value for the system, a positive integer from 1 to 100, where 1 equals 1/100; maximum funding target for the distribution activity.</td></tr><tr><td>releaseRatio</td><td>8</td><td>56+8</td><td>192</td><td>255</td><td>Release ratio, a positive integer from 1 to 100, where 1 equals 1/100; determines the proportion of funds released to users.</td></tr><tr><td>releaseCycle</td><td>32</td><td>64+32</td><td>160</td><td>18446744073709551615</td><td>Unlocking period, 10-digit timestamp; time period for fund unlocking.</td></tr><tr><td>padding</td><td>64</td><td>96+64</td><td>128</td><td>18446744073709551615</td><td>Reserved for future extension; reserved field, currently unused, for future expansion.</td></tr><tr><td>financingAmount</td><td>64</td><td>128+64</td><td>64</td><td>18446744073709551615</td><td>Funding capital; total amount of funds involved in the distribution activity.</td></tr><tr><td>maxPerUser</td><td>64</td><td>192+64</td><td>0</td><td>18446744073709551615</td><td>Maximum amount of funds a single user can participate with during the check process.</td></tr></tbody></table>

#### **Explanation:**

* **Bit Index and Reverse Column:** Provide the position and offset of each field within the bit field.
* **Mask Column:** Provides the mask values used for bitwise operations to extract specific fields from the overall data.
* **Annotation Column:** Provides detailed descriptions of each field and their roles within the system.
* **Length in Table:** Indicates the number of bits each field occupies within the bit field.
* **Bit Index and Reverse Column:** Used to calculate the exact position of each field within the bit field.
* **Mask Column:** Used for bitwise operations on the bit field to ensure extraction or modification of relevant bits only.

### JavaScript Encoding and Decoding Test

```solidity

/** Struct ChequeStruct is used to store information about the cheque.
struct ChequeStruct {
    uint32 distributionId; // Distribution ID to which the cheque is linked
    uint8 participationLimit; // Participant limit: 1 any user, 2 expert, 3 other
    uint8 minHardCap; // Minimum system hard cap, integer from 1 to 100 where 1=1/100
    uint8 maxHardCap; // Maximum system hard cap, integer from 1 to 100 where 1=1/100
    uint8 releaseRatio; // Release ratio, integer from 1 to 100 where 1=1/100
    uint32 releaseCycle; // Unlock cycle in seconds; 1 minute = 60 seconds, 1 day = 86400 seconds
    uint32 padding; // Padding data, meaningless, set to 0
    uint64 financingAmount; // Financing amount; set to 0 if not needed, 100 tokens = 100 * 1e18 / 1e10
    uint64 maxPerUser; // Maximum per user check limit, 100 tokens = 100 * 1e18 / 1e10
}
*/


var bigInt = require("big-integer");

function hideDataDis1(distribution) {
    let targetId = bigInt(distribution.targetId);
    let participationLimit = bigInt(distribution.participationLimit);
    let minHardCap = bigInt(distribution.minHardCap);
    let maxHardCap = bigInt(distribution.maxHardCap);
    let releaseRatio = bigInt(distribution.releaseRatio);
    let releaseCycle = bigInt(distribution.releaseCycle);
    let padding = bigInt(distribution.padding);
    let financingAmount = bigInt(distribution.financingAmount);
    let maxPerUser = bigInt(distribution.maxPerUser);
    // Incorrect bit shifting direction
    let hiddenData = targetId.shiftLeft(8)
        .or(participationLimit).shiftLeft(8)
        .or(minHardCap).shiftLeft(8)
        .or(maxHardCap).shiftLeft(8)
        .or(releaseRatio).shiftLeft(32)
        .or(releaseCycle).shiftLeft(32)
        .or(padding).shiftLeft(64)
        .or(financingAmount).shiftLeft(64)
        .or(maxPerUser);

    return hiddenData.toString(10);
}

function revealDataDis1(hiddenData) {
    let data = bigInt(hiddenData);

    let maxPerUser = data.and('18446744073709551615');
    data = data.shiftRight(64);

    let financingAmount = data.and('18446744073709551615');
    data = data.shiftRight(64);

    let padding = data.and('4294967295');
    data = data.shiftRight(32);

    let releaseCycle = data.and('4294967295');
    data = data.shiftRight(32);

    let releaseRatio = data.and('255');
    data = data.shiftRight(8);

    let maxHardCap = data.and('255');
    data = data.shiftRight(8);

    let minHardCap = data.and('255');
    data = data.shiftRight(8);

    let participationLimit = data.and('255');
    data = data.shiftRight(8);

    let targetIdLower = data.and('4294967295').toString();
    data = data.shiftRight(32);

    let targetIdUpper = data.and('4294967295').toString();
    let targetId = targetIdUpper + targetIdLower;

    return {
        targetId: parseInt(targetId),
        participationLimit: parseInt(participationLimit.toString()),
        minHardCap: parseInt(minHardCap.toString()),
        maxHardCap: parseInt(maxHardCap.toString()),
        releaseRatio: parseInt(releaseRatio.toString()),
        releaseCycle: parseInt(releaseCycle.toString()),
        padding: parseInt(padding.toString()),
        financingAmount: parseInt(financingAmount.toString()),
        maxPerUser: parseInt(maxPerUser.toString())
    };
}

// Test data
let chequeData = {
    targetId: 12345,
    participationLimit: 1,
    minHardCap: 5,
    maxHardCap: 10,
    releaseRatio: 20,
    releaseCycle: 1000000000,
    padding: 123,
    financingAmount: 5000000000,
    maxPerUser: 10000000000
};

let encodedData = hideDataDis1(chequeData);
// 332820648991343397452793519134652897661010861321068696474042534556591104
console.log("Encoded Data:", encodedData);

let decodedData = revealDataDis1(encodedData);
console.log("Decoded Data:", decodedData);



```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://synbo-labs.gitbook.io/docs/contract-docs/cheque/encoding.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
