Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tonweb-contract

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tonweb-contract

Interaction with smart contracts used in TonWeb

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

tonweb-contract

This is a sub package of TonWeb.

The idea is to interact with smart contracts as if they were JavaScript objects.

tonweb.Contract is abstract class for all smart contract objects in TonWeb.

Install

tonweb-contract is already included in the main package:

npm install tonweb

import TonWeb from "tonweb";
TonWeb.Contract;

const tonweb = new TonWeb();
tonweb.Contract;

You can use the tonweb-contract separately:

npm install tonweb-contract

import {Contract} from "tonweb-contract"

Implement your custom contract

ABI and json interface of contract not yet invented in TON, so you need extend tonweb.Contract and compose messages to contract yourself.

import {Contract} from 'web3-eth-contract';

export class MyContract extends Contract {
    constructor(provider, options) {
        options.code = hexToBytes('abcd..');
        super(provider, options);

        this.method.myMethod = ...
    }

    // @override
    createDataCell() {
    }
    
    // @override
    createSigningMessage(options) {
    }
}

Deployment functionality is implemented in the Contract class, you only need to override createDataCell and createSigningMessage methods.

tonweb.Contract contains several static functions to help compose messages:

  • Contract.createStateInit

  • Contract.createInternalMessageHeader

  • Contract.createExternalMessageHeader

  • Contract.createCommonMsgInfo

You can see an example of extending Contract class and using these functions in the code tonweb-contract-wallet

Common Interface

/**
* @param provider    {HttpProvider}
* @param options    {{code?: Uint8Array, address?: Address, wc?: number}}
*/
const contract = new Contract(provider, options)

const address: Address = contract.getAddress();


const deployMethod = contract.deploy(keyPair.secretKey);

const query: Cell = await deployMethod.getQuery(); // get init external message as Cell

await deplotMethod.estimateFee(); // get estimate fee of deploy 

await deployMethod.send(); // send init external message to blockchain


contract.methods; // object contains all methods of this smart contract

const myMethod = contract.methods.myMethod(myParams);

const query: Cell = await myMethod.getQuery(); // get external message 

await myMethod.estimateFee(); // get estimate fee 

await myMethod.send(); // send to blockchain 


const myGetMethod = contract.methods.myGetMethod(myParams);

const result = await myGetMethod.call(); // invoke get-method of this smart contract

Authors

@rulon and @tolyayanot

Keywords

FAQs

Package last updated on 09 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc