@saturn-chain/smart-contract
TypeScript icon, indicating that this package has built-in type declarations

1.0.19 • Public • Published

smart-contract: takes a set of compiled solidity contracts to execute them in JS

Usage:

npm i --save @saturn-chain/smart-contract

Includes typescript types declaration

Documentation

import { SmartContracts } from "smart-contract";
// combined.json is the result of the solidity compilation with a command line like
// solc -o contracts --combined-json abi,bin --overwrite src/*.sol
const SC = SmartContracts.load(require("contracts/combined.json"));

// Fictive implementation of the EthProviderInterface
import { Impl } from "impl-function";
const intf = new Impl();

const MyContract = SC.get("MyContract");

// deploy the smart contract into the chain using the interface implementation
// uses the newi() returned function to receive the compiled data 
const instance = await MyContract.deploy(intf.newi({}), param1, param2);
// display the address where the smart contract instance has been deployed
console.log("Created instance", instance.deployedAt);

// creaate a contract instance of an already deployed smart contract at a given address
const instance2 = MyContract.at("0x123...999")

// call a function of the instance (just reading the chain state)
// solidity MyContract has a function totalSupply with no parameter
const value = await instance.totalSupply(intf.call());

// send a transaction to the chain to be mined
// solidity MyContract has a transfer(amount, recipient) function
const tx = await instance.transfer(intf.send({maxGas: 100000}), 1000, "0x123...999");

// get historical events
// solidity MyContract has an event Transfer(from, to, amount)
instance.events.Transfer(intf.get({
  fromBlock: 100
}), {from:"0x123...999"});

// subscribe to all events 
const ev = instance.allEvents(intf.sub, {})
// register a log handler
.on("log", console.log)
// register the error handler
.on("error", err=>{
  console.error(err);
  // trigger the end of the subscription, no more event received
  ev.removeAllListeners();
  })

Readme

Keywords

none

Package Sidebar

Install

npm i @saturn-chain/smart-contract

Weekly Downloads

25

Version

1.0.19

License

ISC

Unpacked Size

22.7 kB

Total Files

5

Last publish

Collaborators

  • guenoledc