@logosrhema/trex-core
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@logosrhema/trex-core

Build Status npm version NPM Downloads

Table of contents

What is @logosrhema/trex-core

The @logosrhema/trex-core package provides a set of building blocks for integration with ERC3643 tokens. The ERC3643 protocol is an open-source suite of smart contracts that enables the issuance, management, and transfer of permissioned tokens.

Installation

  1. Install module

    npm i @logosrhema/trex-core --save

  2. reflect-metadata is required, install it too:

    npm install reflect-metadata --save

    and make sure to import it in a global place, like app.ts:

    import 'reflect-metadata';

Usage examples

React hooks

Token API

import { TokenContract } from "@logosrhema/trex-core";
import { useEffect, useState } from "react";
import { Signer } from "@ethersproject/abstract-signer";

export interface Token {
  owner: any;
  name: any;
  totalSupply: any;
  decimals: any;
  frozenTokens: any;
  realBalanceOf: any;
  balanceOf: any;
  paused: any;
  walletIsFrozen: any;
  identityRegistry: any;
  pause: () => Promise<void>;
  run: () => Promise<void>;
  unfreeze: (address: string) => Promise<void>;
  freeze: (address: string) => Promise<void>;
  compliance: () => Promise<any>;
  isWalletFrozen: (walletAddress: string) => Promise<boolean>;
  getFrozenTokens: (walletAddress: string) => Promise<boolean>;
  getBalance: (walletAddress: string) => Promise<any>;
  areTransferPartiesFrozen: (from: string, to: string) => Promise<void>;
  isEnoughSpendableBalance: (from: string, amount: number) => Promise<void>;
}

export const useToken = (signer: Signer | undefined, debug = false) => {
  const [token, setToken] = useState<any>();

  const getTkn = async (tokenAddress: string): Promise<Token | null> => {
    if (!signer) {
      return null;
    }

    const token = await TokenContract.init(tokenAddress, signer);

    setToken(token);

    if (!token) {
      return null;
    }

    return {
      owner: await token.owner(),
      name: await token.name(),
      totalSupply: await token.totalSupply(),
      decimals: await token.decimals(),
      frozenTokens: await token.frozenTokens(),
      realBalanceOf: await token.realBalanceOf(),
      balanceOf: await token.balanceOf(),
      paused: await token.paused(),
      walletIsFrozen: await token.walletIsFrozen(),
      identityRegistry: await token.identityRegistry(),
      pause: token.pause,
      run: token.run,
      unfreeze: token.unfreeze,
      freeze: token.freeze,
      compliance: token.compliance,
      isWalletFrozen: token.isWalletFrozen,
      getFrozenTokens: token.getFrozenTokens,
      getBalance: token.getBalance,
      areTransferPartiesFrozen: token.areTransferPartiesFrozen,
      isEnoughSpendableBalance: token.isEnoughSpendableBalance,
    };
  };

  useEffect(() => {
    if (!signer || !token) {
      return;
    }

    if (token.contract) {
      token.contract.on("Paused", () => {
        setToken({ ...token, paused: true });
      });

      token.contract.on("Unpaused", () => {
        setToken({ ...token, paused: false });
      });

      token.contract.on(
        "AddressFrozen",
        (
          walletAddressToFreeze: string,
          isFrozen: boolean,
          signerAddress: string
        ) => {
          console.log(walletAddressToFreeze, "is frozen", isFrozen);
        }
      );

      token.contract.on("error", (error: Error) => {
        console.log(error);
      });
    }

    if (debug) {
      signer.provider?.on("debug", (data: any) => console.log(...data));
    }
  }, [token]);

  return {
    getToken: getTkn,
  };
};

Transfer compliance

import { getTransferCompliance } from '@logosrhema/trex-core'

export const useTransferCompliance = () => {
  const {
    isTransferCompliant
  } = getTransferCompliance;

  return {
    isTransferCompliant
  };
};

Vue composables

Token API

import 'reflect-metadata';
import { ref } from 'vue';
import { Signer } from 'vue-dapp';
import { TokenContract } from '@logosrhema/trex-core';

export async function useToken(tokenAddress: string, signer: Signer) {

  const token = TokenContract.init(tokenAddress, signer);

  const isPaused = ref(false);
  isPaused.value = await token.paused();

  token.contract.on('Paused', () => {
    isPaused.value = true;
  });

  token.contract.on('Unpaused', () => {
    isPaused.value = false;
  });

  token.contract.on('AddressFrozen', (walletAddressToFreeze: string, isFrozen: boolean, signerAddress: string) => {
    console.log(walletAddressToFreeze, 'is frozen', isFrozen);
  });

  token.contract.on('error', (error: Error) => {
    console.log(error);
  })

  return {
    token,
    isPaused
  };
}

Transfer compliance

import { getTransferCompliance } from '@logosrhema/trex-core';

export function useTransferCompliance() {
  return getTransferCompliance
}

Readme

Keywords

Package Sidebar

Install

npm i @logosrhema/trex-core

Weekly Downloads

3

Version

1.1.0

License

MIT

Unpacked Size

62.1 kB

Total Files

37

Last publish

Collaborators

  • logosrhema