forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssetManagement.ts
121 lines (111 loc) · 4.06 KB
/
AssetManagement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* AssetManagement.ts
*/
import { LPInfoHolder } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/util/LPInfoHolder";
import { Verifier } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/verifier/Verifier";
import { ContractInfoHolder } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/util/ContractInfoHolder";
import { ConfigUtil } from "../../packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/util/ConfigUtil";
const fs = require("fs");
const path = require("path");
const config: any = ConfigUtil.getConfig();
import { getLogger } from "log4js";
const moduleName = "AssetManagement";
const logger = getLogger(`${moduleName}`);
logger.level = config.logLevel;
export class AssetManagement {
private connectInfo: LPInfoHolder = null; // connection information
private contractInfoholder: ContractInfoHolder = null; // contract information
private verifierEthereum: Verifier = null;
constructor() {
this.connectInfo = new LPInfoHolder();
this.contractInfoholder = new ContractInfoHolder();
}
addAsset(amount: string): Promise<any> {
return new Promise((resolve, reject) => {
if (this.verifierEthereum === null) {
logger.debug("create verifierEthereum");
const ledgerPluginInfo: string =
this.connectInfo.getLegerPluginInfo("84jUisrs");
this.verifierEthereum = new Verifier(ledgerPluginInfo);
}
// for Neo
const contractInfo: string =
this.contractInfoholder.getContractInfo("AssetContract");
const contractInfoObj: {} = JSON.parse(contractInfo);
const coinbase = contractInfoObj["_eth"]["coinbase"];
const address = contractInfoObj["address"];
const abi = contractInfoObj["abi"];
const contract = {
address: address,
abi: abi,
};
const method = {
type: "contract",
command: "addAsset",
function: "sendTransaction",
};
const template = "default";
const args = { args: [amount, { from: coinbase }] };
// const method = "default";
// const args = {"method": {type: "contract", command: "addAsset", function: "sendTransaction"}, "args": {"args": [amount, {from: coinbase}]}};
this.verifierEthereum
.sendSyncRequest(contract, method, args)
.then((result) => {
const response = {
status: result.status,
"Transaction hash": result.data,
};
resolve(response);
})
.catch((err) => {
logger.error(err);
reject(err);
});
});
}
getAsset(): Promise<any> {
return new Promise((resolve, reject) => {
if (this.verifierEthereum === null) {
logger.debug("create verifierEthereum");
const ledgerPluginInfo: string =
this.connectInfo.getLegerPluginInfo("84jUisrs");
this.verifierEthereum = new Verifier(ledgerPluginInfo);
}
// for Neo
const contractInfo: string =
this.contractInfoholder.getContractInfo("AssetContract");
const contractInfoObj: {} = JSON.parse(contractInfo);
const address = contractInfoObj["address"];
const abi = contractInfoObj["abi"];
const contract = {
address: address,
abi: abi,
};
const method = {
type: "contract",
command: "getAsset",
function: "call",
};
const template = "default";
const args = { args: [] };
// const method = "default";
// const args = {"method": {type: "contract", command: "getAsset", function: "call"}, "args": {"args": []}};
this.verifierEthereum
.sendSyncRequest(contract, method, args)
.then((result) => {
const response = {
status: result.status,
asset: parseFloat(result.data),
};
resolve(response);
})
.catch((err) => {
logger.error(err);
reject(err);
});
});
}
}