npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@decent-bet/connex-entities

v3.2.2

Published

Connex entity contracts allows mapping of contract entities

Downloads

24

Readme

Connex Entities

Connex entity contracts allows mapping of contract entities and uses a middleware (Vue plugin) to connect to Comet and Connex.

Middleware

Vue

npm i -S @decent-bet/vue-connex-entities

More info: https://github.com/decent-bet/vue-connex-entities

Adding Contract Entities to plugin middleware

Vue.use(ConnexEntityContract, {
    entities: [
      { name: 'Energy', contract: EnergyTokenContract }, 
      { name: 'DBET', contract: DBETVETTokenContract }
    ]
});

Requesting Connex and Comet

When required, request access to the external wallet and middleware will create the contract entities in the Vue instance variable $contractEntities.

The following instance variables are available:

  • $requestExternalWalletAccess: Request access to external wallet
  • $contractEntities: The contract entities created after external wallet access sucess.
  async loginComet() {
    await this.$requestExternalWalletAccess();
    const myAccount = this.$contractEntities.Energy.defaultAccount;
    const vthoBalance = await this.$contractEntities.Energy.balanceOf(myAccount);
  }

Contract Import

const EnergyTokenContractAbi = require('./Energy');

const EnergyContractImport: ContractImport = {
  raw: {
    abi: EnergyTokenContractAbi.abi
  },
  address: {
    '0x27': '0x0000000000000000000000000000456E65726779',
    '0x4a': '0x0000000000000000000000000000456E65726779'
  }
};

API

@ConnexContract

A contract entity maps to a contract import using the @ConnexContract, which has an IConnexContract interface and OnConnexReady mixin.


@ConnexContract({
  import: EnergyContractImport
})
export class EnergyTokenContract {
  constructor() {
    super();
  }
}

IConnexContract properties

  • connex
  • chainTag
  • defaultAccount

OnConnexReady events

  • onConnexReady(connex, chainTag, defaultAccount)

In most cases the base class will handle the basic scenario where a vendor wallet sets the connex and other properties.

Contract Methods

@GetMethod

Returns a Connex.Thor.Method instance.

  @GetMethod({
    nameOrAbi: 'balanceOf',
    address: () => EnergyContractImport.address[ConnexService.chainTag]
  })
  public balanceOfMethod(): any {}
@Read

Returns a Promise with the result of the VMOutput call execution.

  @Read({
    nameOrAbi: 'balanceOf',
    returns: 'decoded',
    address: () => EnergyContractImport.address[ConnexService.chainTag]
  })
  public balanceOf(address: string): any {}

or

  @Read()
  public balanceOf(address: string): any {}
@Write

Returns a Promise with the result of a signing execution.

  @Write({
    nameOrAbi: 'transfer',
    gas: 90_000
  })
  public transferMethod(address: string, wei: BigNumber): any {}
@MultiClauseWrite
  @MultiClauseWrite({
    nameOrAbi: 'transfer',
    gas: 90_000
  })
  public transferMethod(address: string, wei: BigNumber, world: BigNumber): any {
    return (m: Connex.Thor.Method) => {
      return [{
        comment: 'Hello',
        ...m.asClause(address, wei)
      },{
        comment: 'World',
        ...m.asClause(world)
      }];
    }
  }
Using validations

Returns an error if a validation fails, otherwise continues with call execution.

  @Write({
    nameOrAbi: 'transfer',
    gas: 90_000,
    validations: {
      address: 'address',
      wei: 'bignumber'
    }
  })
  public transferMethod(address: string, wei: BigNumber): any {}

Contract Events

@AccountEventFilter

A contract event filter, it can run once or by polling. A thunk function is required which gets a Filter instance. Returns an Observable if interval is used, else a Promise.

  @AccountEventFilter({
    nameOrAbi: 'Transfer',
    interval: 10_000,
  })
  public getTransfers$(indices: Array<object>, options: AccountEventFilterOptions) {
    return (filter: Connex.Thor.Filter<'event'>) => {
      filter.order('asc');

      return filter.apply(0, 5);
    };
  }

  @AccountEventFilter({
    nameOrAbi: 'Transfer',
    address: () => EnergyContractImport.address[ConnexService.chainTag],
  })
  public getTransfers(indices: Array<object>, options: AccountEventFilterOptions) {
    return (filter: Connex.Thor.Filter<'event'>) => {
      filter
        .order('asc')
        .range({
          unit: 'block',
          from: fromBlock,
          to
        });

      return filter.apply(0, 5);
    };
  }
Properties
  • nameOrAbi(abi.Event.Definition | abi.Function.Definition | string): ABI definition name or ABI definition. Defaults to method name.
  • address(string): Contract address.
  • interval(number): Polls event every n seconds.
  • skipIndices: Skip calling event with indexed arguments.
  • validations(object): Validate arguments.

Blockchain Events

@BlockchainEventFilter

Polls an event or transfer. A thunk function is required which gets a Filter instance. Returns an Observable if interval is used, else a Promise.

  @BlockchainEventFilter({
    interval: 10_000,
    kind: 'transfer'
  })
  public getAllTransfers$() {
    return (filter: Connex.Thor.Filter<'transfer'>) => {
      filter.order('asc');

      return filter.apply(0, 5);
    };
  }
Properties
  • nameOrAbi(abi.Event.Definition | abi.Function.Definition | string): ABI definition name or ABI definition. Defaults to method name.
  • kind(string): Log type, either 'transfer' or 'event'.

Utils

@GetEventSignature

Returns an event signature

  @GetEventSignature()
  public Transfer() {
  }
@GetMethodSignature

Returns a method signature

  @GetMethodSignature()
  public transfer() {
  }

RxJS Operators

blockConfirmationUntil

Waits until transaction is confirmed

    const resp = await token.approve(...);
    const logs: any = await token.approvalLog$([]);
    // creates observable 
    const blockConfirmation = blockConfirmationUntil(resp.txid);

    // once complete, continue with other pipes
    return blockConfirmation
      .pipe(
        switchMap(_ => logs)
      )
      .subscribe(async (i: any) => {
        console.log(i);
      });
waitForLog

Takes a contract write function promise and an event log function promise and waits until confirmation to return logs back

    const approve = token.approve(...);
    const logs: any = token.approvalLog$([]);
    return waitForLog(approve, logs)
      .subscribe((log: any) => { ... });

Best Practices

Use vuex-connex-entities for state management integration

With this plugin you can create contracs in a type safe way without using undocumented this._vm inside an action to access $contractEntities.

Contract entities should be lean

Contract entities helps with working with Connex and Thor, any business logic should be decoupled.

Use a shared module to package contract entities

A shared module contains ABI assets and/or contract entities classes for a set of contracts, the shared module can be deployed to npm or a git submodule and linked to one or more projects.