-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathblockchain.js
More file actions
executable file
·124 lines (110 loc) · 3.34 KB
/
blockchain.js
File metadata and controls
executable file
·124 lines (110 loc) · 3.34 KB
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
122
123
124
import TronWeb from 'tronweb';
import Config from '../config';
import { BigNumber, openTransModal, setTransactionsData, randomSleep, myLocal } from './helper';
const chain = Config.chain;
const DATA_LEN = 64;
export const MAX_UINT256 = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
const privateKey = chain.privateKey;
const mainchain = new TronWeb({
fullHost: chain.fullHost,
privateKey
});
export const triggerSmartContract = async (address, functionSelector, options = {}, parameters = []) => {
try {
const tronweb = window.tronWeb;
const transaction = await tronweb.transactionBuilder.triggerSmartContract(
address,
functionSelector,
Object.assign({ feeLimit: 20 * 1e6 }, options),
parameters
);
if (!transaction.result || !transaction.result.result) {
throw new Error('Unknown trigger error: ' + JSON.stringify(transaction.transaction));
}
return transaction;
} catch (error) {
throw new Error(error);
}
};
export const sign = async transaction => {
try {
const tronweb = window.tronWeb;
const signedTransaction = await tronweb.trx.sign(transaction.transaction);
return signedTransaction;
} catch (error) {
console.log(error, 'signerr');
throw new Error(error);
}
};
export const sendRawTransaction = async signedTransaction => {
try {
const tronweb = window.tronWeb;
const result = await tronweb.trx.sendRawTransaction(signedTransaction);
return result;
} catch (error) {
throw new Error(error);
}
};
export const view = async (address, functionSelector, parameters = [], isDappTronWeb = true) => {
try {
let tronweb = mainchain;
if (!isDappTronWeb && window.tronWeb && window.tronWeb.ready) {
tronweb = window.tronWeb;
}
const result = await tronweb.transactionBuilder.triggerSmartContract(
address,
functionSelector,
{ _isConstant: true },
parameters
);
return result && result.result ? result.constant_result : [];
} catch (error) {
console.log(`view error ${address} - ${functionSelector}`, error.message ? error.message : error);
return [];
}
};
export const getTrxBalance = async (address, isDappTronWeb = false) => {
try {
let tronWeb = mainchain;
if (!isDappTronWeb && window.tronWeb && window.tronWeb.ready) {
tronWeb = window.tronWeb;
}
const balance = await tronWeb.trx.getBalance(address);
return {
balance: BigNumber(balance).div(Config.defaultPrecision),
success: true
};
} catch (err) {
console.log(`getPairBalance: ${err}`, address);
return {
balance: BigNumber(0),
success: false
};
}
};
export const getTransactionInfo = tx => {
const tronWeb = mainchain;
return new Promise((resolve, reject) => {
tronWeb.trx.getConfirmedTransaction(tx, (e, r) => {
if (!e) {
resolve(r);
} else {
reject(e, null);
}
});
});
};
export const getTRC20Balance = async (tokenAddress, userAddress) => {
console.log('params of getbalance: ', userAddress, tokenAddress);
const result = await view(tokenAddress, 'balanceOf(address)', [{ type: 'address', value: userAddress }]);
let value = BigNumber(0);
let success = false;
if (result.length) {
value = new BigNumber(result[0].slice(0, DATA_LEN), 16);
success = true;
}
return {
value,
success
};
};