-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tokens.ts
142 lines (122 loc) · 3.35 KB
/
tokens.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Copyright (c) 2018-present, Leap DAO (leapdao.org)
*
* This source code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3
* found in the LICENSE file in the root directory of this source tree.
*/
import { observable, action, computed, reaction, IObservableArray } from 'mobx';
import autobind from 'autobind-decorator';
import { range, NFT_COLOR_BASE, isNFT } from '../utils';
import { TokenStore } from './token';
import { exitHandlerStore } from './exitHandler';
export class TokensStore {
@observable
public list: IObservableArray<TokenStore>;
private erc20TokenCount: number;
private nftTokenCount: number;
constructor() {
this.erc20TokenCount = 0;
this.nftTokenCount = 0;
reaction(() => exitHandlerStore.contract, this.init);
reaction(
() => exitHandlerStore.contract,
() => {
exitHandlerStore.contract.events.NewToken(
{},
this.loadTokens.bind(this)
);
}
);
}
@autobind
private init() {
this.loadTokens();
}
@autobind
@action
private addTokens(tokens: TokenStore[]) {
if (!this.list) {
this.list = observable.array([]);
}
tokens.forEach(token => {
this.list.push(token);
});
}
@computed
public get ready() {
if (!this.list) {
return false;
}
return !this.list.some(token => !token.ready);
}
public tokenIndexForAddress(address: string) {
if (!this.list) {
return -1;
}
return this.list.findIndex(
t => t.address.toLowerCase() === address.toLowerCase()
);
}
public tokenForAddress(address: string): TokenStore {
const index = this.tokenIndexForAddress(address);
if (index < 0 || index >= this.list.length) {
return undefined;
}
return this.list[index];
}
public tokenIndexForColor(color: number) {
if (!this.list) {
return -1;
}
return isNFT(color)
? this.erc20TokenCount + (color - NFT_COLOR_BASE)
: color;
}
public tokenForColor(color: number) {
const index = this.tokenIndexForColor(color);
if (index < 0 || index >= this.list.length) {
return undefined;
}
return this.list[index];
}
private loadTokenColorRange(from: number, to: number) {
return (range(from, to) as number[]).map(color => {
return exitHandlerStore.contract.methods
.tokens(color)
.call()
.then(({ 0: address }) => {
return new TokenStore(address, color);
});
});
}
public loadTokens() {
return Promise.all([
exitHandlerStore.contract.methods
.erc20TokenCount()
.call()
.then(r => Number(r)),
exitHandlerStore.contract.methods
.nftTokenCount()
.call()
.then(r => Number(r)),
])
.then(([erc20TokenCount, nftTokenCount]) => {
// load new tokens for ERC20 and ERC721 color ranges
const tokens = Promise.all([
...this.loadTokenColorRange(
this.erc20TokenCount,
erc20TokenCount - 1
),
...this.loadTokenColorRange(
NFT_COLOR_BASE + this.nftTokenCount,
NFT_COLOR_BASE + nftTokenCount - 1
),
]);
this.erc20TokenCount = erc20TokenCount;
this.nftTokenCount = nftTokenCount;
return tokens;
})
.then(this.addTokens);
}
}
export const tokensStore = new TokensStore();