-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
113 lines (93 loc) · 3.31 KB
/
index.js
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
const Account = require('./src/entity/Account');
const TokenList = require('./src/entity/TokenList');
const add0x = require('./src/utils/add-0x');
const GethClient = require('./src/GethClient');
const recoverPrivateKey = require('./src/utils/recover-private-key.js');
const newAccount = require('./src/utils/new-account.js');
const TransactionResult = require('./src/TransactionResult');
const initContract = require('./src/utils/init-contract.js');
const toHex = require('./src/utils/to-hex');
const config = require('./config');
const KEYS = {
mainchain: {
token: 'masterchainSNMAddress',
market: 'marketAddress',
gate: 'gatekeeperMasterchainAddress',
oracleUSD: 'oracleUsdAddress',
faucet: 'testnetFauсetAddress',
},
sidechain: {
token: 'sidechainSNMAddress',
market: 'marketAddress',
gate: 'gatekeeperSidechainAddress',
oracleUSD: 'oracleUsdAddress',
faucet: 'testnetFauсetAddress',
}
};
function createSonmFactory(remoteEthNodeUrl, chainId = 'live', privateChain = false, params = {}) {
const chainKey = chainId + (privateChain ? '_private' : '');
const chainConfig = config[chainKey];
const gethClient = new GethClient(remoteEthNodeUrl, chainId, privateChain);
/**
* create API entity Account
* @param {string} remoteEthNodeUrl
* @param {string} address
* @param {string} privateKey
*/
async function createAccount(address) {
await getAddresses();
const address0x = add0x(address);
const ctrArguments = {
gethClient,
config: chainConfig,
sonmTokenAddress: chainConfig.contractAddress.token,
address0x,
};
Object.assign(ctrArguments, params);
const account = new Account(ctrArguments);
account.initContracts(chainConfig.contractAddress);
return account;
}
function createTxResult(hash) {
return new TransactionResult(hash, gethClient);
}
function setPrivateKey(privateKey) {
gethClient.setPrivateKey(privateKey);
}
function getSonmTokenAddress() {
return chainConfig.contractAddress.token;
}
async function createTokenList() {
await getAddresses();
const tokenList = new TokenList({
gethClient,
});
await tokenList.initSonmToken(chainConfig.contractAddress.token);
return tokenList;
}
async function getAddresses() {
if (Object.keys(chainConfig.contractAddress).length <= 1) {
const addressRegistry = initContract('addressRegistry', new GethClient(config[`${chainId}_private`].url, chainId, true), config[`${chainId}_private`].contractAddress.addressRegistry);
const keys = privateChain ? KEYS.sidechain : KEYS.mainchain;
for (const key in keys) {
chainConfig.contractAddress[key] = (await addressRegistry.call('read', [Buffer.from(keys[key])], '0x' + Array(41).join('0'), toHex(1000000))).toLowerCase();
}
}
}
return {
gethClient,
createAccount,
createTxResult,
setPrivateKey,
getSonmTokenAddress,
createTokenList,
};
};
module.exports = {
createSonmFactory,
utils: {
recoverPrivateKey,
add0x,
newAccount,
},
};