import { RPC , transformers } from 'ckb-js-toolkit' ; import { CHAIN_SPECS } from './constants' ; import { Config } from './interfaces' ; import { Address , Amount , SUDT , Transaction } from './models' ; import { DefaultSigner , Signer } from './signers' ; import { Collector } from './collectors' ; import { SimpleBuilder , Builder , SimpleSUDTACPBuilder , SimpleSUDTBuilder , } from './builders' ; import { Provider } from './providers' ; import { SUDTCollector } from './collectors/sudt-collector' ; export enum ChainID { ckb , ckb_testnet , ckb_dev } /**
 * The default main class of pw-core
 */ export default class PWCore { static config : Config ; static chainId : ChainID ; static provider : Provider ; static defaultCollector : Collector | SUDTCollector ; private readonly _rpc : RPC ; constructor ( nodeUrl ) { this . _rpc = new RPC ( nodeUrl ) ; } /**
     * Initialize the environment required by pw-core
     */ async init ( provider , defaultCollector , chainId ? , config ? ) { if ( chainId ) { if ( ! ( chainId in ChainID ) ) { throw new Error ( `invalid chainId ${ chainId }` ) ; } PWCore . chainId = chainId ; } else { const info = await this . rpc . get_blockchain_info ( ) ; PWCore . chainId = { ckb : ChainID . ckb , ckb_testnet : ChainID . ckb_testnet , ckb_dev : ChainID . ckb_dev , } [ info . chain ] ; } if ( PWCore . chainId === ChainID . ckb_dev ) { if ( ! config ) { throw new Error ( 'config must be provided for dev chain' ) ; } PWCore . config = config ; } else { PWCore . config = { ... [ CHAIN_SPECS . Lina , CHAIN_SPECS . Aggron ] [ PWCore . chainId ] , ... config , } ; } if ( provider instanceof Provider ) { PWCore . provider = await provider . init ( ) ; } else { throw new Error ( 'provider must be provided' ) ; } if ( defaultCollector instanceof Collector ) { PWCore . defaultCollector = defaultCollector ; } else { throw new Error ( 'defaultCollector must be provided' ) ; } return this ; } /**
     * Return a RPC instance defined in package 'ckb-js-toolkit'
     */ get rpc ( ) : RPC { return this . _rpc ; } param address param amount param feeRate async send ( address , amount , feeRate ? ) { const simpleBuilder = new SimpleBuilder ( address , amount , feeRate ) ; return this . sendTransaction ( simpleBuilder ) ; } param toSend param signer async sendTransaction ( toSend , signer ? ) { const tx = toSend instanceof Builder ? await toSend . build ( ) : toSend ; tx . validate ( ) ; if ( ! signer ) { signer = new DefaultSigner ( PWCore . provider ) ; } return this . rpc . send_transaction ( transformers . TransformTransaction ( await signer . sign ( tx ) ) ) ; } param sudt param address param amount param feeRate returns async sendSUDT ( sudt , address , amount , createAcp ? , signer ? , feeRate ? ) { const builder = createAcp ? new SimpleSUDTBuilder ( sudt , address , amount , feeRate ) : new SimpleSUDTACPBuilder ( sudt , address , amount , feeRate ) ; return this . sendTransaction ( builder , signer ) ; } }