|
| 1 | +/* |
| 2 | + * Copyright 2020-2021 Hyperledger Cactus Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * BalanceManagement.ts |
| 6 | + */ |
| 7 | + |
| 8 | +import { |
| 9 | + LPInfoHolder, |
| 10 | + ConfigUtil, |
| 11 | +} from "@hyperledger/cactus-cmd-socketio-server"; |
| 12 | +import { |
| 13 | + VerifierFactory, |
| 14 | + VerifierFactoryConfig, |
| 15 | +} from "@hyperledger/cactus-verifier-client"; |
| 16 | + |
| 17 | +const config: any = ConfigUtil.getConfig(); |
| 18 | +import { getLogger } from "log4js"; |
| 19 | +const moduleName = "BalanceManagement"; |
| 20 | +const logger = getLogger(`${moduleName}`); |
| 21 | +logger.level = config.logLevel; |
| 22 | + |
| 23 | +export class BalanceManagement { |
| 24 | + private connectInfo: LPInfoHolder = null; // connection information |
| 25 | + private readonly verifierFactory: VerifierFactory; |
| 26 | + |
| 27 | + constructor() { |
| 28 | + this.connectInfo = new LPInfoHolder(); |
| 29 | + this.verifierFactory = new VerifierFactory( |
| 30 | + this.connectInfo.ledgerPluginInfo as VerifierFactoryConfig, |
| 31 | + config.logLevel, |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + getBalance(account: string): Promise<any> { |
| 36 | + return new Promise((resolve, reject) => { |
| 37 | + // for LedgerOperation |
| 38 | + // const execData = {"referedAddress": account}; |
| 39 | + // const ledgerOperation: LedgerOperation = new LedgerOperation("getNumericBalance", "", execData); |
| 40 | + |
| 41 | + // for Neo |
| 42 | + const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object. |
| 43 | + const method = { type: "web3Eth", command: "getBalance" }; |
| 44 | + const template = "default"; |
| 45 | + const args = { args: [account] }; |
| 46 | + // const method = "default"; |
| 47 | + // const args = {"method": {type: "web3Eth", command: "getBalance"}, "args": {"args": [account]}}; |
| 48 | + |
| 49 | + this.verifierFactory |
| 50 | + .getVerifier("84jUisrs") |
| 51 | + .sendSyncRequest(contract, method, args) |
| 52 | + .then((result) => { |
| 53 | + const response = { |
| 54 | + status: result.status, |
| 55 | + amount: parseFloat(result.data), |
| 56 | + }; |
| 57 | + resolve(response); |
| 58 | + }) |
| 59 | + .catch((err) => { |
| 60 | + logger.error(err); |
| 61 | + reject(err); |
| 62 | + }); |
| 63 | + }); |
| 64 | + } |
| 65 | +} |
0 commit comments