@saturn-chain/dlt-tx-data-functions

Defines types to link `@saturn-chain/smart-contract` with different implementations accessing the chain. These functions takes the ethereum smart contract formatted data of the transactions and events filters and expect that the implementation of this l

Usage no npm install needed!

<script type="module">
  import saturnChainDltTxDataFunctions from 'https://cdn.skypack.dev/@saturn-chain/dlt-tx-data-functions';
</script>

README

dlt-tx-data-functions: defines data types and interfaces

Defines types to link @saturn-chain/smart-contract with different implementations accessing the chain.
These functions takes the ethereum smart contract formatted data of the transactions and events filters and expect that the implementation of this library will send it properly to the network.

Two implementation exists as of now:

  • @saturn-chain/web3-function that communicates directly to a web3 provider. Transaction signatures are delegated to either the node or the provider
  • @saturn-chain/dlt-function that communicates to the dlt services via the @saturn-chain/dlt-rest-api library and expect transactions to be signed by the @saturn-chain/wallet-custody-rest-api.

Usage:

npm i --save @saturn-chain/dlt-tx-data-functions

Includes typescript types declaration

Documentation

export class YourImplementation implements EthProviderInterface {
  
  async account(index: number=0): Promise<string> {
    // returns the address available at index or the first available address if index invalid
  }

  call(options?: CallOptions): CallSendFunction {
    // function used to send a smart contract call (read only the smart contract state)
    const defaultOptions: CallOptions = { maxGas: 100000 };
    const opts = { ...defaultOptions, ...(options || {}) };
    return async (target, data): Promise<CallSendResult> => {
      // target is the address of the smart contract to be called
      // data is the encoded data to be sumitted
      // return the following structure with result as the result of the smart contract function called in raw format
      return { isTx: false, result: res };
    };
  }
  send(options?: SendOptions): CallSendFunction {
    // function used to submit a smart contract transaction that is expected to be mined
    const defaultOptions: SendOptions = { maxGas: 100000 };
    const opts = { ...defaultOptions, ...(options || {}) };
    return async (target, data): Promise<CallSendResult> => {
      // target is the address of the smart contract to be called
      // data is the encoded data to be sumitted
      // return the following structure with result as the transaction hash 
      return { isTx: true, result: txhash };
    };
  }
  newi(options?: SendOptions): DeployCallbackFunction {
    // function used to deploy a smart contract code to the chain
    const defaultOptions: SendOptions = { maxGas: 500000 };
    const opts = { ...defaultOptions, ...(options || {}) };
    return async (name, data): Promise<string | undefined> => {
      // name is the name of the smart contract as defined in solidity code
      // data is the compiled bytecode of the smart contract
      // should returned the contract address in the chain
      return contractCreated;
    };
  }


  sub(options?: LogsOptions): RequestEventsFunction {
    // function used to subscribe to Event Log from the chain
    const opts = {
      fromBlock: "latest",
      ...(options || {}),
    };
    return async (address, event, topics, receiver) => {
      // address is the optional smart contract address from which event should be captured
      // event is the name of the event, not always usefull since the first topic is the hash of the event signature
      // topics is an array of string or string[] interpreted by ethereum filters
      // receiver is an EventEmitter that should be used to pass the received log structure
      receiver.emit("log", {...}, 0 /* log index*/, 1 /* total logs to receive*/)
      // receiver will notify if the subscription should be called by raising the event removeListener
      receiver.on("removeListener", async (ev, listner)=>{
          if(ev === "log") {
            // remove subscription
          }
        })
      // receiver can be passed errors
      receiver.emit("error", error)
    }
  }

  get(options?: LogsOptions): RequestEventsFunction {
    // function used to query Event Log direclty
    const opts = {
      fromBlock: "earliest",
      toBlock: "latest",
      ...(options || {}),
    };
    return async (address, event, topics, receiver) => {
      // same behaviour than for the sub above 
      // except that there is no subscription so the 'removeListener' is never raised
    }
  }
}