Skip to content

Commit 37c1819

Browse files
authored
Fix: better getUnspent (#143)
* feat: make optional * chore: deprecate getUnspentAll and getUnspentByAddress * breaking: drop LeapEthers in favour of LeapProvider * docs: minor corrections to js docs * chore: deprecate sendSignedTransaction * fix: proper typescript required arg can't go after the optional one * fix: allow to be defined but null :) * fix: typedef for getUnspent for tokenAddress instead of the color * support getUnspent() instead of getUnspentAll. Deprecate all getUnspent*
1 parent 19bd174 commit 37c1819

File tree

2 files changed

+36
-83
lines changed

2 files changed

+36
-83
lines changed

index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,9 @@ declare module "leap-core" {
327327
};
328328

329329
class ExtendedWeb3 extends Web3 {
330-
public getUnspent(address: string, color: number, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
331-
public getUnspent(address: string, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
330+
public getUnspent(address: string | undefined | null, color: number, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
331+
public getUnspent(address: string | undefined | null, tokenAddress: string, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
332+
public getUnspent(address?: string, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
332333
public getUnspentAll(cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
333334
public getColor(tokenContractAddress: string, cb?: Callback<number>): Promise<number>;
334335
public getColors(cb?: Callback<string[]>): Promise<string[]>;

lib/helpers.js

Lines changed: 33 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,25 @@ import Outpoint from './outpoint';
1212
import Period from './period';
1313
import Tx from './transaction';
1414

15-
function getLowerCase(value) {
16-
if (value) {
17-
return value.toLowerCase();
18-
}
19-
return value;
20-
}
21-
2215
const fromRaw = u => ({
2316
output: u.output,
2417
outpoint: Outpoint.fromRaw(u.outpoint),
2518
});
2619

27-
export class LeapEthers {
28-
/**
29-
* @param { import("ethers").providers.JsonRpcProvider } provider
30-
*/
31-
constructor(provider) {
32-
this.provider = provider;
33-
}
34-
35-
getUnspent(...params) {
36-
return this.provider
37-
.send('plasma_unspent', params.map(getLowerCase))
38-
.then(unspent => unspent.map(fromRaw));
39-
}
40-
41-
getUnspentByAddress(...params) {
42-
return this.getUnspent(params);
43-
}
44-
45-
getUnspentAll() {
46-
return this.provider
47-
.send('plasma_unspent', [])
48-
.then(unspent => unspent.map(fromRaw));
49-
}
50-
51-
getColor(...params) {
52-
return this.provider.send('plasma_getColor', params.map(getLowerCase));
53-
}
54-
55-
getColors() {
56-
return this.provider.send('plasma_getColors', []);
57-
}
58-
59-
status() {
60-
return this.provider.send('plasma_status', []);
61-
}
62-
63-
getConfig() {
64-
return this.provider.send('plasma_getConfig', []);
65-
}
20+
const paddedParams = (params) => {
21+
const hasCb = typeof params[params.length - 1] === 'function';
22+
const paramCount = hasCb ? params.length - 1 : params.length;
6623

67-
getValidatorInfo() {
68-
return this.provider.send('validator_getAddress', []);
24+
if (paramCount === 0) { // getUnspent()
25+
return ['', '', ...params];
6926
}
7027

71-
checkSpendingCondition(tx) {
72-
return this.provider.send('checkSpendingCondition', [tx.hex()]).then(({ error, outputs }) => ({
73-
error,
74-
outputs: outputs.map(Output.fromJSON),
75-
}));
28+
if (paramCount === 1) { // getUnspent(address)
29+
return [params[0], '', ...params.splice(1)];
7630
}
7731

78-
getPeriodByBlockHeight(blockHeight) {
79-
return this.provider.send('plasma_getPeriodByBlockHeight', [blockHeight]);
80-
}
81-
}
32+
return params; // getUnspent(address, color)
33+
};
8234

8335
export function extendWeb3(web3Instance) {
8436
// `_extend` for web3 0.2x.x, `extend` for 1.x
@@ -104,17 +56,7 @@ export function extendWeb3(web3Instance) {
10456
methods: [
10557
new extend.Method({
10658
...getUnspent,
107-
name: 'getUnspentByAddress',
108-
params: 1,
109-
}),
110-
new extend.Method({
111-
...getUnspent,
112-
name: 'getUnspentByAddressColor',
113-
}),
114-
new extend.Method({
115-
...getUnspent,
116-
name: 'getUnspentAll',
117-
params: 0,
59+
name: '_getUnspent',
11860
}),
11961
new extend.Method({
12062
name: 'getColor',
@@ -173,16 +115,24 @@ export function extendWeb3(web3Instance) {
173115
],
174116
});
175117

176-
web3Instance.getUnspent = (...params) => {
177-
const last = params[params.length - 1];
178-
const hasCb = typeof last === 'function';
179-
if (params.length === (hasCb ? 2 : 1)) {
180-
return web3Instance.getUnspentByAddress(...params);
181-
}
118+
web3Instance.getUnspentAll = (...params) => {
119+
console.warn('DEPRECATED: use getUnspent() instead'); // eslint-disable-line no-console
120+
return web3Instance.getUnspent(...params);
121+
};
182122

183-
return web3Instance.getUnspentByAddressColor(...params);
123+
web3Instance.getUnspentByAddress = (...params) => {
124+
console.warn('DEPRECATED: use getUnspent(address) instead'); // eslint-disable-line no-console
125+
return web3Instance.getUnspent(...params);
184126
};
185127

128+
web3Instance.getUnspentByAddressColor = (...params) => {
129+
console.warn('DEPRECATED: use getUnspent(address, color) instead'); // eslint-disable-line no-console
130+
return web3Instance.getUnspent(...params);
131+
};
132+
133+
web3Instance.getUnspent = (...params) =>
134+
web3Instance._getUnspent(...paddedParams(params)); // eslint-disable-line no-underscore-dangle
135+
186136
return web3Instance;
187137
}
188138

@@ -234,7 +184,7 @@ export function getTxWithYoungestBlock(txs) {
234184
* Returns the youngest input for a given tx.
235185
*
236186
* Youngest input is the one which references tx with biggest block number.
237-
* @param {ExtendedWeb3} plasma instance of Leap Web3
187+
* @param {ExtendedWeb3|LeapProvider} plasma instance of Leap Web3
238188
* @param {Tx} tx
239189
* @returns {Promise<InputTx>} promise that resolves to youngest input tx and its index
240190
*/
@@ -247,7 +197,7 @@ export function getYoungestInputTx(plasma, tx) {
247197
/**
248198
* Creates proof of period inclusion for a given tx
249199
*
250-
* @param {ExtendedWeb3} plasma instance of Leap Web3
200+
* @param {ExtendedWeb3|LeapProvider} plasma instance of Leap Web3
251201
* @param {LeapTransaction} tx
252202
* @returns {Promise<Proof>} promise that resolves to period inclusion proof
253203
*/
@@ -314,10 +264,12 @@ const send = (plasma, txHex) => {
314264
* Sends a signed transaction to the node
315265
*
316266
* @param {ExtendedWeb3} plasma instance of Leap Web3 or JSONRPCProvider of Ethers.js
317-
* @param {LeapTransaction} txHex - transaction to send
267+
* @param {string} txHex - transaction to send
318268
* @returns {Promise<Receipt>} promise that resolves to a transaction receipt
319269
*/
320270
export function sendSignedTransaction(plasma, txHex) {
271+
// eslint-disable-next-line no-console
272+
console.warn('DEPRECATED: use Web3#sendSignedTransaction or LeapProvider#sendTransaction instead');
321273
return send(plasma, txHex).then((resp) => {
322274
return awaitForReceipt(resp, Promise.resolve(), plasma, 0);
323275
});
@@ -334,10 +286,10 @@ export function consolidateUTXOs(utxos) {
334286
/**
335287
* Returns outputs for spending condition tx
336288
*
337-
* @param {ExtendedWeb3 | LeapEthers} plasma
338-
* @param {LeapTransaction} tx
289+
* @param {ExtendedWeb3 | LeapProvider} plasma
290+
* @param {Tx} tx
339291
*
340-
* @returns {Promise<Output>}
292+
* @returns {Promise<Array<Output>>}
341293
*/
342294
export function simulateSpendCond(plasma, tx) {
343295
return plasma.checkSpendingCondition(tx);

0 commit comments

Comments
 (0)