UNPKG

openchain-sdk-yxl-ts

Version:

OpenChain SDK for browser

70 lines (57 loc) 2.17 kB
import Account from './account/index.js'; import Token from './token/index.js'; import Block from './blockchain/block.js'; import Transaction from './blockchain/transaction.js'; import Operation from './operation/index.js'; import Contract from './contract/index.js'; import errors from './exception/index.js'; import Util from './util/index.js'; class OpenChainSDK { constructor(options) { if (options && options.inited) { this.options = options; } else { this.options = OpenChainSDK.initOptions(options); } this.options.errors = errors; this.account = new Account(this.options); this.transaction = new Transaction(this.options); this.token = new Token(this.options); this.block = new Block(this.options); this.operation = new Operation(this.options); this.contract = new Contract(this.options); this.util = new Util(this.options); } static initOptions(options) { if (typeof options !== 'object' || options === null) { throw new Error('options is require, it must be an object'); } if (typeof options.host !== 'string' || !options.host) { throw new Error('host must be a non-empty string'); } const chainId = options.chainId || 0; const timeout = options.timeout || 15 * 1000; if (typeof chainId !== 'number') { throw new Error('chainId must be a number'); } if (typeof timeout !== 'number') { throw new Error('timeout must be a number'); } const opts = {}; Object.keys(options).forEach(key => { if (options[key] !== undefined) { opts[key] = options[key]; } }); opts.secure = opts.secure || false; opts.chainId = chainId; opts.timeout = timeout; opts.inited = true; return opts; } } // 为了保持与原有代码的兼容性,我们添加一个工厂函数 const createOpenChainSDK = (options) => { return new OpenChainSDK(options); }; export { OpenChainSDK as default, createOpenChainSDK };