-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathLand.ts
387 lines (331 loc) · 9.9 KB
/
Land.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { BigNumber } from '@ethersproject/bignumber'
import env from '../env'
import API from './API'
import Options from './Options'
export type GetListOptions = {
status?: 'open' | 'cancelled' | 'sold'
sortBy?: 'price' | 'created_at' | 'block_time_updated_at' | 'expires_at'
sortOrder?: 'asc' | 'desc'
limit?: number
offset?: number
}
export type GetImageOptions = {
width?: number
height?: number
size?: number
publications?: boolean
}
export type GetMapImageOptions = GetImageOptions & {
center?: [number, number]
selected?: [number, number][]
}
export type Position = string | [number, number]
export type GetMapImageV2Options = Omit<GetImageOptions, 'publications'> & {
center?: Position
selected?: Position[]
}
type Token = {
id: string
name: string
description: string
image: string
external_url: string
background_color: string
}
export type Parcel = Token & {
attributes: {
trait_type: 'X' | 'Y' | 'Distance to District' | 'Distance to Road'
value: number
display_type: 'number'
}[]
}
export type Estate = Token & {
attributes: {
trait_type: 'Size' | 'Distance to District' | 'Distance to Road'
value: number
display_type: 'number'
}[]
}
export type Tile = {
id: string
x: number
y: number
type: 'owned' | 'unowned' | 'plaza' | 'road' | 'district'
top: boolean
left: boolean
topLeft: boolean
updatedAt: number
name?: string
owner?: string
estateId?: string
tokenId?: string
price?: number
}
export type MapContent = {
assets: {
parcels: Parcel[]
estates: Estate[]
}
total: number
}
const CLEAR_LOW = BigNumber.from(
'0xffffffffffffffffffffffffffffffff00000000000000000000000000000000'
)
const CLEAR_HIGH = BigNumber.from(
'0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff'
)
const FACTOR = BigNumber.from('0x100000000000000000000000000000000')
const FACTOR_LOW = BigNumber.from(
'0x10000000000000000000000000000000000000000000000000000000000000000'
)
const REVERSE_FACTOR = BigNumber.from('0x1000000000000000000000000000000')
export default class Land extends API {
static Url =
env('LAND_API', '') || // @deprecated
env('LAND_URL', 'https://api.decentraland.org')
static Cache = new Map<string, Land>()
/**
* TODO(#323): remove on v6
* @deprecated use getInstance instead
*/
static get() {
return this.getInstance()
}
static getInstance() {
return this.getInstanceFrom(env('LAND_URL', this.Url))
}
/**
* TODO(#323): remove on v6
* @deprecated use getInstanceFrom instead
*/
static from(baseUrl: string) {
return this.getInstanceFrom(baseUrl)
}
static getInstanceFrom(baseUrl: string) {
if (!this.Cache.has(baseUrl)) {
this.Cache.set(baseUrl, new Land(baseUrl))
}
return this.Cache.get(baseUrl)!
}
static encodePosition(position: Position): string {
if (typeof position === 'string') {
return position
} else {
return position.slice(0, 2).join(',')
}
}
static decodePosition(position: Position): [number, number] {
if (typeof position === 'string') {
return position.split(',').slice(0, 2).map(Number) as [number, number]
} else {
return position
}
}
static encodeParcelId(coordinates: [number, number]): string {
const [x, y] = coordinates
if (
!Number.isFinite(x) ||
!Number.isFinite(y) ||
-1000000 >= x ||
x >= 1000000 ||
-1000000 >= y ||
y >= 1000000
) {
throw new RangeError(`The coordinates should be inside bounds`)
}
const absX = BigNumber.from(Math.abs(x))
const absY = BigNumber.from(Math.abs(y))
const uintX = x < 0 ? FACTOR_LOW.sub(absX) : absX
const uintY = y < 0 ? FACTOR.sub(absY) : absY
const bX = BigNumber.from(uintX).mul(FACTOR).and(CLEAR_LOW)
const bY = BigNumber.from(uintY).and(CLEAR_HIGH)
return bX.or(bY).toString()
}
static decodeParcelId(parcelId: string): [number, number] {
const bn = BigNumber.from(parcelId)
const bnX = bn.div(FACTOR)
const bnY = bn.mod(FACTOR)
const x = bnX.gte(REVERSE_FACTOR)
? bnX.sub(FACTOR).toNumber()
: bnX.toNumber()
const y = bnY.gte(REVERSE_FACTOR)
? bnY.sub(FACTOR).toNumber()
: bnY.toNumber()
if (
!Number.isFinite(x) ||
!Number.isFinite(y) ||
-1000000 >= x ||
x >= 1000000 ||
-1000000 >= y ||
y >= 1000000
) {
throw new RangeError(`The coordinates should be inside bounds`)
}
return [x, y]
}
async fetch<T extends {}>(url: string, options: Options = new Options({})) {
const result = await super.fetch<{ ok: boolean; data: T }>(url, options)
return result.data
}
async getPositionName(position: [number, number]) {
const tile = await this.getTile(position)
if (tile && tile.name && tile.name !== 'Roads') {
return tile.name
}
const parcel = await this.getParcel(position)
if (
parcel &&
parcel.name &&
parcel.name !== `Parcel ${position.join(',')}`
) {
return parcel.name
}
return 'Decentraland'
}
async getParcel(position: [number, number]): Promise<Parcel> {
const [x, y] = position
return this.fetch(`/v2/parcels/${x}/${y}`)
}
async getEstate(id: number | string): Promise<Estate> {
return this.fetch(`/v2/estates/${id}`)
}
async getParcels(options: GetListOptions = {}): Promise<Parcel[]> {
const { sortBy: sort_by, sortOrder: sort_order } = options
return this.fetch(
`/v2/parcels` + this.query({ ...options, sort_by, sort_order })
)
}
async getEstates(options: GetListOptions = {}): Promise<Estate[]> {
const { sortBy: sort_by, sortOrder: sort_order } = options
return this.fetch(
`/v2/estates` + this.query({ ...options, sort_by, sort_order })
)
}
/** @deprecated */
async getMapContent(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
nw: [number, number],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
se: [number, number]
): Promise<MapContent> {
throw new Error(`Endpoint /v1/map is deprecated`)
}
/**
* Get a map of tiles
* @param position1
* @param position2
* @param options
* @returns
*/
async getTiles(
position1: [number, number],
position2: [number, number],
options: { include?: (keyof Tile)[]; exclude?: (keyof Tile)[] } = {}
): Promise<Record<string, Tile>> {
const params = new URLSearchParams({
x1: String(position1[0]),
y1: String(position1[1]),
x2: String(position2[0]),
y2: String(position2[1]),
})
if (options.include && options.include.length > 0) {
params.append('include', options.include.join(','))
}
if (options.exclude && options.exclude.length > 0) {
params.append('include', options.exclude.join(','))
}
return this.fetch(`/v2/tiles?` + params.toString())
}
async getTile(
position: [number, number],
options: { include?: (keyof Tile)[]; exclude?: (keyof Tile)[] } = {}
): Promise<Tile | null> {
const tiles = await this.getTiles(position, position, options)
return tiles[position.join(',')] || null
}
encodeParcelId(coordinates: [number, number]): string {
return Land.encodeParcelId(coordinates)
}
decodeParcelId(parcelId: string): [number, number] {
return Land.decodeParcelId(parcelId)
}
/** @deprecated use getMapImage instead */
getImage(options: GetMapImageOptions = {}) {
const { selected: rawSelected } = options
const selected =
rawSelected && rawSelected.map((position) => position.join(',')).join(';')
return this.url(`/v1/map.png` + this.query({ ...options, selected }))
}
getParcelImage(coordinates: [number, number], options: GetImageOptions = {}) {
const [x, y] = coordinates
return this.url(`/v1/parcels/${x}/${y}/map.png` + this.query(options))
}
getEstateImage(id: number | string, options: GetImageOptions = {}) {
return this.url(`/v1/estates/${id}/map.png` + this.query(options))
}
getMapImage(options: GetMapImageV2Options = {}) {
const params = new URLSearchParams()
let width: number
let height: number
if (!!options.width && !!options.height) {
height = options.height
width = options.width
} else if (!!options.width && !options.height) {
height = options.width
width = options.width
} else if (!options.width && !!options.height) {
height = options.height
width = options.height
} else {
height = 1024
width = 1024
}
params.set('height', String(height))
params.set('width', String(width))
if (options.selected && options.selected.length) {
params.set(
'selected',
options.selected
.map((position) => Land.encodePosition(position))
.join(';')
)
}
if (options.center) {
params.set('center', Land.encodePosition(options.center))
}
if (options.size) {
params.set('center', String(options.size))
}
if (
(!options.center || !options.size) &&
options.selected &&
options.selected.length
) {
const Xs = options.selected.map(
(position) => Land.decodePosition(position)[0]
)
const Ys = options.selected.map(
(position) => Land.decodePosition(position)[1]
)
const maxX = Math.max(...Xs)
const minX = Math.min(...Xs)
const maxY = Math.max(...Ys)
const minY = Math.min(...Ys)
if (!options.center) {
const centerX = Math.floor((minX + maxX) / 2)
const centerY = Math.floor((minY + maxY) / 2)
params.set('center', `${centerX},${centerY}`)
}
if (!options.size) {
const sizeX = maxX - minX
const sizeY = maxY - minY
const size = Math.floor(
Math.min(height, width) / Math.max(sizeX, sizeY)
)
params.set('size', String(Math.min(Math.max(size, 5), 20)))
}
}
const qs = params.toString()
return this.url('/v2/map.png' + (qs ? '?' : '') + qs)
}
}