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

1.2.11 • Public • Published

@wavesenterpise/contract-core

Implements JS Contract SDK core functionality, rpc services, tools and utilities.

Getting Started

Run following in command line:

npm i @wavesenterpise/contract-core

Create contract.ts as follows

import {
  Action,
  Contract,
  ContractMapping,
  ContractValue,
  IncomingTx,
  JsonVar,
  logger,
  Param,
  Params,
  Tx,
  Var,
  Block,
  Sender,
  BlockInfo,
} from '@wavesenterprise/contract-core'
import Long from 'long'

@Contract()
export default class #{contractName} {

  log = logger(this)

  @Var()
  counter!: ContractValue<number>

  @JsonVar()
  participants!: ContractMapping<UserData>

  @Action({ onInit: true })
  init(@Params() params: Record<string, unknown>) {
    this.counter.set(0)
    this.log.info('all params', params)
  }

  @Action({ preload: ['counter'] })
  async increment(@Tx() tx: IncomingTx, @Param('by') by: Long) {
    const { senderPublicKey, sender } = tx
    const counter = await this.counter.get()
    let participant = await this.participants.tryGet(senderPublicKey)
    if (!participant) {
      participant = {
        publicKey: senderPublicKey,
        address: sender,
        amount: 0,
      }
    }
    participant.amount += by.toNumber()
    this.counter.set(counter + by.toNumber())
    this.participants.set(senderPublicKey, participant)
  }
  
  async saveInfo(@Sender() sender: string, @Block() currentBlock: BlockInfo) {
    // 
  }
}

To initialize contract use:

import { initContract } from '@wavesenterprise/contract-core'

initContract({
  contractPath: __dirname + '/contract.js', // path to compiled contract
  concurrencyLevel: 1,
})

License

This project is licensed under the MIT License.

Readme

Keywords

none

Package Sidebar

Install

npm i @wavesenterprise/contract-core

Weekly Downloads

6

Version

1.2.11

License

MIT

Unpacked Size

433 kB

Total Files

312

Last publish

Collaborators

  • stfy
  • sergeymart
  • nbugaev
  • wavesadmin
  • aklyavlin
  • dimalitvinov
  • mikhail.milekhin