Skip to content

Commit 683d45e

Browse files
xiaowheatiwbrhwfy
authored andcommitted
Export exchange, metaMask, privateKey modules (#271)
1 parent 1462ab9 commit 683d45e

File tree

8 files changed

+64
-48
lines changed

8 files changed

+64
-48
lines changed

packages/lightcone_v2.js/src/grpc/grpcClientService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class GrpcClientService {
4646
// Verfied
4747
public async getDexConfigurations(): Promise<DexConfigurations> {
4848
const empty: Empty = new Empty();
49+
4950
return new Promise<DexConfigurations>((resolve: Function, reject: Function): void => {
5051
this.client.getDexConfigurations(empty, null, (err: grpcWeb.Error, res: DexConfigurations) => {
5152
if (err) {

packages/lightcone_v2.js/src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ import { grpcClientService } from './grpc/grpcClientService';
22

33
import common from './lib/wallet/common';
44
import ethereum from './lib/wallet/ethereum';
5-
// Looks like we don't need WalletUtils
6-
// It also causes an error
7-
// "export 'default' (reexported as 'WalletUtils')
8-
// was not found in './WalletUtils'
9-
// import WalletUtils from './WalletUtils';
105
import ContractUtils from './lib/wallet/ethereum/contracts/Contracts';
116
import EthRpcUtils from './lib/wallet/ethereum/eth';
127
import Utils from './lib/wallet/common/utils';
138

9+
import { exchange } from './sign/exchange';
10+
import { MetaMask } from './wallet/metaMask';
11+
import { privateKey } from './wallet/privateKey';
1412

1513
export {
1614
grpcClientService,
1715
common,
1816
ethereum,
19-
// WalletUtils,
17+
exchange,
18+
MetaMask,
19+
privateKey,
2020
ContractUtils,
2121
EthRpcUtils,
2222
Utils
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import tokens from './tokens'
22

3-
export {tokens};
4-
5-
export const configs = require('./config.json.js');
6-
export const paths = require('./path.json.js');
7-
export const wallets = require('./dpath.json.js');
8-
export const projects = require('./projects.json.js');
3+
import configs from './config.json';
4+
import paths from './path.json';
5+
import wallets from './dpath.json';
6+
import projects from './projects.json';
97

8+
export {
9+
tokens,
10+
configs,
11+
paths,
12+
wallets,
13+
projects
14+
}

packages/lightcone_v2.js/src/lib/wallet/config/settings.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {configs} from './data'
1+
import { configs } from './data'
22

3-
const set = (settings) => {
3+
const set = (settings: any) => {
44
localStorage.settings = JSON.stringify(settings)
55
};
66
const latestContract = configs.contracts[configs.contracts.length - 1];
77
const relays = configs.relays;
88
let sortedRelays = relays.map((item, i) => {
9-
item.id = i;
9+
item['id'] = i;
1010
return item
1111
});
1212
const get = () => {
@@ -56,7 +56,7 @@ const getContractVersion = () => {
5656
}
5757
};
5858

59-
const setTokensConfig = (tokens) => {
59+
const setTokensConfig = (tokens: any) => {
6060
localStorage.tokensConfig = JSON.stringify(tokens)
6161
};
6262

@@ -68,7 +68,7 @@ const getTokensConfig = () => {
6868
}
6969
};
7070

71-
const setMarketPairs = (markets) => {
71+
const setMarketPairs = (markets: any) => {
7272
localStorage.marketsConfig = JSON.stringify(markets)
7373
};
7474

@@ -80,7 +80,7 @@ const getMarketPairs = () => {
8080
}
8181
};
8282

83-
const setMarketR = (markets) => {
83+
const setMarketR = (markets: any) => {
8484
localStorage.marketR = JSON.stringify(markets)
8585
};
8686

@@ -92,7 +92,7 @@ const getMarketR = () => {
9292
}
9393
};
9494

95-
const setConfigs = (configs) => {
95+
const setConfigs = (configs: any) => {
9696
localStorage.configs = JSON.stringify(configs)
9797
};
9898

packages/lightcone_v2.js/src/lib/wallet/ethereum/walletAccount.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ export class MetaMaskAccount extends WalletAccount {
282282
if (this.web3 && this.web3.eth.accounts[0]) {
283283
return this.web3.eth.accounts[0];
284284
} else {
285-
throw new Error('Not found MetaMask');
285+
// SDK shouldn't throw any error
286+
console.warn('Not found MetaMask');
286287
}
287288
}
288289

@@ -291,6 +292,7 @@ export class MetaMaskAccount extends WalletAccount {
291292
if (!result['error']) {
292293
return result['result'];
293294
} else {
295+
// SDK shouldn't throw any error
294296
throw new Error(result['error']['message']);
295297
}
296298
}
@@ -300,6 +302,7 @@ export class MetaMaskAccount extends WalletAccount {
300302
if (!result['error']) {
301303
return result['result'];
302304
} else {
305+
// SDK shouldn't throw any error
303306
throw new Error(result['error']['message']);
304307
}
305308
}
@@ -309,6 +312,7 @@ export class MetaMaskAccount extends WalletAccount {
309312
if (!result['error']) {
310313
return result['result'];
311314
} else {
315+
// SDK shouldn't throw any error
312316
throw new Error(result['error']['message']);
313317
}
314318
}

packages/lightcone_v2.js/src/sign/exchange.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import BN = require('bn.js');
2-
import { generateKeyPair, sign } from 'src/lib/sign/eddsa';
3-
import config from 'src/lib/wallet/config';
4-
5-
import {grpcClientService} from 'src/grpc/grpcClientService';
6-
import {ethereum} from 'src/lib/wallet';
7-
import * as fm from 'src/lib/wallet/common/formatter';
8-
import Eth from "src/lib/wallet/ethereum/eth";
9-
import Transaction from "src/lib/wallet/ethereum/transaction";
10-
import {WalletAccount} from 'src/lib/wallet/ethereum/walletAccount';
11-
import {Order, TokenAmounts} from 'src/proto_gen/data_order_pb';
2+
import { generateKeyPair, sign } from '../lib/sign/eddsa';
3+
import config from '../lib/wallet/config';
4+
5+
import {grpcClientService} from '../grpc/grpcClientService';
6+
import {ethereum} from '../lib/wallet';
7+
import * as fm from '../lib/wallet/common/formatter';
8+
import Eth from '../lib/wallet/ethereum/eth';
9+
import Transaction from '../lib/wallet/ethereum/transaction';
10+
import {WalletAccount} from '../lib/wallet/ethereum/walletAccount';
11+
import {DexAccount, OrderInfo, Signature} from '../model/types';
12+
import {Order, TokenAmounts} from '../proto_gen/data_order_pb';
1213
import {
1314
AccountID,
1415
Amount,
@@ -18,13 +19,12 @@ import {
1819
EdDSASignature,
1920
OrderID,
2021
TokenID
21-
} from 'src/proto_gen/data_types_pb';
22+
} from '../proto_gen/data_types_pb';
2223
import {
2324
Account,
2425
GetNextOrderIdReq,
2526
SimpleOrderCancellationReq
26-
} from 'src/proto_gen/service_dex_pb';
27-
import {DexAccount, OrderInfo, Signature} from '../model/types';
27+
} from '../proto_gen/service_dex_pb';
2828

2929
export class Exchange {
3030

@@ -102,8 +102,10 @@ export class Exchange {
102102
}
103103

104104
public async createAccount(wallet: WalletAccount, gasPrice: number) {
105+
// TODO: need to check if gasPrice is a reasonable value
105106
if (this.accounts.get(wallet) == null) {
106107
const keyPair = generateKeyPair();
108+
console.log(keyPair)
107109
this.createOrUpdateAccount(keyPair.publicKeyX, keyPair.publicKeyY, gasPrice).then((rawTx: Transaction) => {
108110
const signedTx = wallet.signEthereumTx(rawTx);
109111
wallet.sendTransaction(new Eth(''), signedTx).then(() => { // TODO: config
@@ -126,6 +128,8 @@ export class Exchange {
126128
}
127129

128130
public async createOrUpdateAccount(publicX: string, publicY: string, gasPrice: number) {
131+
// FIXME: ethereum.abi.Contracts.ExchangeContract.encodeInputs returns error
132+
// Unhandled Rejection (TypeError): name.startsWith is not a function
129133
const data = ethereum.abi.Contracts.ExchangeContract.encodeInputs('createOrUpdateAccount', {
130134
pubKeyX: publicX,
131135
pubKeyY: publicY

packages/lightcone_v2.js/src/wallet/metaMask.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Web3 from 'web3';
22

3-
import Eth from "src/lib/wallet/ethereum/eth";
4-
import Transaction from "src/lib/wallet/ethereum/transaction";
5-
import {MetaMaskAccount} from 'src/lib/wallet/ethereum/walletAccount';
6-
import { fromMetaMask } from 'src/lib/wallet/WalletUtils';
7-
import {exchange} from 'src/sign/exchange';
3+
import Eth from '../lib/wallet/ethereum/eth';
4+
import Transaction from '../lib/wallet/ethereum/transaction';
5+
import {MetaMaskAccount} from '../lib/wallet/ethereum/walletAccount';
6+
import { fromMetaMask } from '../lib/wallet/WalletUtils';
7+
import {exchange} from '../sign/exchange';
88

9+
// TODO: implement callback to integrate MetaMask
910
export class MetaMask {
1011

1112
public web3: Web3;
@@ -45,4 +46,5 @@ export class MetaMask {
4546
}
4647
}
4748

48-
export const metaMask: MetaMask = new MetaMask();
49+
// Avoid to use metaMask because MetaMask is not ready when the website is loaded.
50+
// export const metaMask: MetaMask = new MetaMask();

packages/lightcone_v2.js/src/wallet/privateKey.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// @ts-ignore
2-
import {ethereum} from 'src/lib/wallet';
3-
import * as fm from 'src/lib/wallet/common/formatter';
4-
import config from 'src/lib/wallet/config';
5-
import * as datas from 'src/lib/wallet/config/data';
6-
import Contracts from 'src/lib/wallet/ethereum/contracts/Contracts';
7-
import Eth from "src/lib/wallet/ethereum/eth";
8-
import Transaction from "src/lib/wallet/ethereum/transaction";
9-
import {KeyAccount} from 'src/lib/wallet/ethereum/walletAccount';
10-
import {exchange} from 'src/sign/exchange';
2+
import {ethereum} from '../lib/wallet';
3+
import * as fm from '../lib/wallet/common/formatter';
4+
import config from '../lib/wallet/config';
5+
import * as datas from '../lib/wallet/config/data';
6+
import Contracts from '../lib/wallet/ethereum/contracts/Contracts';
7+
import Eth from '../lib/wallet/ethereum/eth';
8+
import Transaction from '../lib/wallet/ethereum/transaction';
9+
import {KeyAccount} from '../lib/wallet/ethereum/walletAccount';
10+
import {exchange} from '../sign/exchange';
1111

1212
export class PrivateKey {
1313

0 commit comments

Comments
 (0)