UNPKG

4.3 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const Constant_1 = require("./Constant");
13const AsiLinkProvider_1 = require("./providers/AsiLinkProvider");
14const ChainRpcProvider_1 = require("./providers/ChainRpcProvider");
15const Compiler_1 = require("./contract/Compiler");
16const FileManager_1 = require("./contract/FileManager");
17let Module = require('./compiler/asimov.js');
18/**
19 * default remote compiler url
20 */
21const DefaultRemoteCompiler = 'https://cdn.asimov.work/asimov.js';
22/**
23 * High level Setting object. It is referenced by other high level objects including Templates, Contracts and Transactions.
24 */
25class Setting {
26 constructor(config) {
27 if (config) {
28 Setting._instance.init(config);
29 }
30 return Setting._instance;
31 }
32 /**
33 * Get global Setting singleton.
34 */
35 static getInstance() {
36 return this._instance;
37 }
38 /**
39 * Initialize Setting object.
40 * @param config Parameters to use when initializing Setting object.
41 */
42 init(config = {}) {
43 return __awaiter(this, void 0, void 0, function* () {
44 this.setPrivateKey(config.privateKey);
45 if (config.compiler) {
46 this.setSolidityCompiler(config.compiler);
47 }
48 if (config.bundleId) {
49 this.setAsiLink(config.bundleId);
50 }
51 if (config.url) {
52 this.setRpcServer(config.url);
53 }
54 if (config.fee) {
55 this.setTransactionFee(config.fee.amount, config.fee.asset);
56 }
57 if (config.remoteCompiler) {
58 Module = yield FileManager_1.FileManager.importRemoteScript(config.remoteCompiler);
59 this.setSolidityCompiler(new Compiler_1.Compiler(Module));
60 }
61 else if (!this.compiler) {
62 if (!Module) {
63 Module = yield FileManager_1.FileManager.importRemoteScript(DefaultRemoteCompiler);
64 }
65 this.setSolidityCompiler(new Compiler_1.Compiler(Module));
66 }
67 this.asiLinkProvider = new AsiLinkProvider_1.AsiLinkProvider();
68 });
69 }
70 /**
71 * Set solidity compiler.
72 * @param compiler the Compiler object.
73 */
74 setSolidityCompiler(compiler) {
75 this.compiler = compiler;
76 }
77 /**
78 * Set global private key.
79 * @param privateKey the private key.
80 */
81 setPrivateKey(privateKey) {
82 this.privateKey = privateKey;
83 }
84 /**
85 * Set AsiLink bundle Id.
86 * @param bundleId AsiLink bundle Id.
87 */
88 setAsiLink(bundleId) {
89 this.bundleId = bundleId;
90 }
91 /**
92 * Set global default transaction fee. If not set, it defaults to DefaultFee in Constant.ts.
93 * @param assetValue asset value, calculated in satoshi.
94 * @param assetType asset type, 12 byte hex format such as "000000000000000200000001".
95 */
96 setTransactionFee(assetValue = 0, assetType = Constant_1.DefaultAsset) {
97 let fee = {
98 amount: assetValue,
99 asset: assetType
100 };
101 this.fee = fee;
102 }
103 /**
104 * Set rpc url for ChainRPC provider.
105 * @param {string} url [description]
106 */
107 setRpcServer(url) {
108 this.chainRpcProvider = new ChainRpcProvider_1.ChainRpcProvider({
109 baseURL: url
110 });
111 }
112 /**
113 * Get global private key.
114 */
115 static GetPrivateKey() {
116 return Setting.getInstance().privateKey;
117 }
118}
119exports.Setting = Setting;
120Setting._instance = new Setting();
121//# sourceMappingURL=Setting.js.map
\No newline at end of file