dms-sdk-common
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

DMS SDK Common

dms-sdk-common provides general purpose components to use across the entire Aragon SDK:

  • Interfaces
  • Constants
  • Types
  • Enumerations
  • Helpers

Installation

Use npm or yarn to install dms-sdk-common.

npm install dms-sdk-common
yarn add dms-sdk-common

Usage

Constants

Encoding

Converting buffers and hex strings

import {hexToBytes, bytesToHex} from "dms-sdk-common";

const buff = hexToBytes("0xffffffff");
// [255, 255, 255, 255]

const str0x = bytesToHex(new Uint8Array([100, 100, 100, 100, 100, 100]));
// "0x646464646464"

const str = bytesToHex(new Uint8Array([100, 100, 100, 100, 100, 100]), true);
// "646464646464"

Converting between buffers and big integers

import {bigIntToBuffer, bigIntToLeBuffer} from "dms-sdk-common";

const bi = BigInt("111122223333444455556666777788889999000011112222333344445555666677778888999900")
const buff = bigIntToBuffer(bi);
// Uint8Array(32) [ 245, 172, 243,  22, 170, 44,  13,  78, ..., 220 ]

const buffLe = bigIntToLeBuffer(bi);
// Uint8Array(32) [ 220,  67, 63, 224,  21,  50, 154, 103, ..., 245 ]
import {bufferToBigInt, bufferLeToBigInt} from "dms-sdk-common";

const buff = new Uint8Array([
    245, 172, 243, 22, 170, 44, 13, 78,
    106, 70, 70, 147, 249, 71, 137, 190,
    155, 21, 186, 14, 206, 88, 97, 129,
    103, 154, 50, 21, 224, 63, 67, 220
]);
const hex = bufferToBigInt(buff);
// 111122223333444455556666777788889999000011112222333344445555666677778888999900n

const buffLe = new Uint8Array([
    220, 67, 63, 224, 21, 50, 154, 103,
    129, 97, 88, 206, 14, 186, 21, 155,
    190, 137, 71, 249, 147, 70, 70, 106,
    78, 13, 44, 170, 22, 243, 172, 245
]);
const hex = bufferLeToBigInt(buffLe);
// 111122223333444455556666777788889999000011112222333344445555666677778888999900n

Percent ratios stored on-chain

Encodes ratio values between 0 and 1 translated into an integer range with the given digit precision

import {encodeRatio, decodeRatio} from "dms-sdk-common";

let digits = 1
encodeRatio(0.5, digits) // 5
encodeRatio(0.53625, digits) // 5
encodeRatio(0.57625, digits) // 6

digits = 4
encodeRatio(0.5, digits) // 5000
encodeRatio(0.53625, digits) // 5362
encodeRatio(0.57625, digits) // 5762

Hex prefix guards

import {strip0x, ensure0x} from "dms-sdk-common";

strip0x("1234")
// "1234"
strip0x("0x1234")
// "1234"

ensure0x("1234")
// "0x1234"
ensure0x("0x1234")
// "0x1234"

Proposal ID's

For nonce based proposal ID's

import {encodeProposalId, decodeProposalId} from "dms-sdk-common";

encodeProposalId(pluginAddr, nonce)
// 0x1234567890123456789012345678901234567890_0x7

decodeProposalId("0x1234567890123456789012345678901234567890_0x7")
// { pluginAddress: "0x12345...", nonce: 7 }

Errors

Promises

Types

Readme

Keywords

none

Package Sidebar

Install

npm i dms-sdk-common

Weekly Downloads

4

Version

1.1.0

License

MIT

Unpacked Size

189 kB

Total Files

17

Last publish

Collaborators

  • bosagoradev
  • hyeonyeobkim
  • youngsoo.jung
  • michaelkim20