From cea6353106a25ca8b5d8c3e17a139b0b029fba40 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Fri, 30 Aug 2019 18:57:51 +0300 Subject: [PATCH 1/9] feat: make optional --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index d271778..c00e1d7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -327,8 +327,8 @@ declare module "leap-core" { }; class ExtendedWeb3 extends Web3 { - public getUnspent(address: string, color: number, cb?: Callback>): Promise>; - public getUnspent(address: string, cb?: Callback>): Promise>; + public getUnspent(address?: string, color: number, cb?: Callback>): Promise>; + public getUnspent(address?: string, cb?: Callback>): Promise>; public getUnspentAll(cb?: Callback>): Promise>; public getColor(tokenContractAddress: string, cb?: Callback): Promise; public getColors(cb?: Callback): Promise; From 637500c4ac25e88b82ae285e4f93fc2505b04327 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Fri, 30 Aug 2019 18:58:16 +0300 Subject: [PATCH 2/9] chore: deprecate getUnspentAll and getUnspentByAddress --- lib/helpers.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/helpers.js b/lib/helpers.js index 721b74a..8435ab4 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -39,10 +39,12 @@ export class LeapEthers { } getUnspentByAddress(...params) { + console.warn('DEPRECATED: use getUnspent(address) instead'); // eslint-disable-line no-console return this.getUnspent(params); } getUnspentAll() { + console.warn('DEPRECATED: use getUnspent() instead'); // eslint-disable-line no-console return this.provider .send('plasma_unspent', []) .then(unspent => unspent.map(fromRaw)); From aedf900d97eb19ee94f51550da515ee10fe72639 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Fri, 30 Aug 2019 19:05:14 +0300 Subject: [PATCH 3/9] breaking: drop LeapEthers in favour of LeapProvider --- lib/helpers.js | 67 +------------------------------------------------- 1 file changed, 1 insertion(+), 66 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 8435ab4..4cc922c 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -12,76 +12,11 @@ import Outpoint from './outpoint'; import Period from './period'; import Tx from './transaction'; -function getLowerCase(value) { - if (value) { - return value.toLowerCase(); - } - return value; -} - const fromRaw = u => ({ output: u.output, outpoint: Outpoint.fromRaw(u.outpoint), }); -export class LeapEthers { - /** - * @param { import("ethers").providers.JsonRpcProvider } provider - */ - constructor(provider) { - this.provider = provider; - } - - getUnspent(...params) { - return this.provider - .send('plasma_unspent', params.map(getLowerCase)) - .then(unspent => unspent.map(fromRaw)); - } - - getUnspentByAddress(...params) { - console.warn('DEPRECATED: use getUnspent(address) instead'); // eslint-disable-line no-console - return this.getUnspent(params); - } - - getUnspentAll() { - console.warn('DEPRECATED: use getUnspent() instead'); // eslint-disable-line no-console - return this.provider - .send('plasma_unspent', []) - .then(unspent => unspent.map(fromRaw)); - } - - getColor(...params) { - return this.provider.send('plasma_getColor', params.map(getLowerCase)); - } - - getColors() { - return this.provider.send('plasma_getColors', []); - } - - status() { - return this.provider.send('plasma_status', []); - } - - getConfig() { - return this.provider.send('plasma_getConfig', []); - } - - getValidatorInfo() { - return this.provider.send('validator_getAddress', []); - } - - checkSpendingCondition(tx) { - return this.provider.send('checkSpendingCondition', [tx.hex()]).then(({ error, outputs }) => ({ - error, - outputs: outputs.map(Output.fromJSON), - })); - } - - getPeriodByBlockHeight(blockHeight) { - return this.provider.send('plasma_getPeriodByBlockHeight', [blockHeight]); - } -} - export function extendWeb3(web3Instance) { // `_extend` for web3 0.2x.x, `extend` for 1.x const extend = web3Instance._extend || web3Instance.extend; // eslint-disable-line no-underscore-dangle, max-len @@ -336,7 +271,7 @@ export function consolidateUTXOs(utxos) { /** * Returns outputs for spending condition tx * - * @param {ExtendedWeb3 | LeapEthers} plasma + * @param {ExtendedWeb3 | LeapProvider} plasma * @param {LeapTransaction} tx * * @returns {Promise} From df6daf80b9e580da9e2d53cb268633b6c3b5b6f0 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Fri, 30 Aug 2019 19:09:07 +0300 Subject: [PATCH 4/9] docs: minor corrections to js docs --- lib/helpers.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 4cc922c..02a0fb8 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -171,7 +171,7 @@ export function getTxWithYoungestBlock(txs) { * Returns the youngest input for a given tx. * * Youngest input is the one which references tx with biggest block number. - * @param {ExtendedWeb3} plasma instance of Leap Web3 + * @param {ExtendedWeb3|LeapProvider} plasma instance of Leap Web3 * @param {Tx} tx * @returns {Promise} promise that resolves to youngest input tx and its index */ @@ -184,7 +184,7 @@ export function getYoungestInputTx(plasma, tx) { /** * Creates proof of period inclusion for a given tx * - * @param {ExtendedWeb3} plasma instance of Leap Web3 + * @param {ExtendedWeb3|LeapProvider} plasma instance of Leap Web3 * @param {LeapTransaction} tx * @returns {Promise} promise that resolves to period inclusion proof */ @@ -251,7 +251,7 @@ const send = (plasma, txHex) => { * Sends a signed transaction to the node * * @param {ExtendedWeb3} plasma instance of Leap Web3 or JSONRPCProvider of Ethers.js - * @param {LeapTransaction} txHex - transaction to send + * @param {string} txHex - transaction to send * @returns {Promise} promise that resolves to a transaction receipt */ export function sendSignedTransaction(plasma, txHex) { @@ -272,9 +272,9 @@ export function consolidateUTXOs(utxos) { * Returns outputs for spending condition tx * * @param {ExtendedWeb3 | LeapProvider} plasma - * @param {LeapTransaction} tx + * @param {Tx} tx * - * @returns {Promise} + * @returns {Promise>} */ export function simulateSpendCond(plasma, tx) { return plasma.checkSpendingCondition(tx); From 5b2cab48682f3dece7aac0a691539c685777e578 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Fri, 30 Aug 2019 19:10:02 +0300 Subject: [PATCH 5/9] chore: deprecate sendSignedTransaction --- lib/helpers.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/helpers.js b/lib/helpers.js index 02a0fb8..b985b55 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -255,6 +255,8 @@ const send = (plasma, txHex) => { * @returns {Promise} promise that resolves to a transaction receipt */ export function sendSignedTransaction(plasma, txHex) { + // eslint-disable-next-line no-console + console.warn('DEPRECATED: use Web3#sendSignedTransaction or LeapProvider#sendTransaction instead'); return send(plasma, txHex).then((resp) => { return awaitForReceipt(resp, Promise.resolve(), plasma, 0); }); From cdc9de99a29ebaf70ad80bd1331f9b761dea62c4 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Mon, 2 Sep 2019 13:07:39 +0200 Subject: [PATCH 6/9] fix: proper typescript required arg can't go after the optional one --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index c00e1d7..9f57604 100644 --- a/index.d.ts +++ b/index.d.ts @@ -327,7 +327,7 @@ declare module "leap-core" { }; class ExtendedWeb3 extends Web3 { - public getUnspent(address?: string, color: number, cb?: Callback>): Promise>; + public getUnspent(address: string | undefined, color: number, cb?: Callback>): Promise>; public getUnspent(address?: string, cb?: Callback>): Promise>; public getUnspentAll(cb?: Callback>): Promise>; public getColor(tokenContractAddress: string, cb?: Callback): Promise; From 5e580b16d40ae655abb340e45a8ece8bd1ed6d1f Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Mon, 2 Sep 2019 14:17:05 +0200 Subject: [PATCH 7/9] fix: allow to be defined but null :) --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 9f57604..b029050 100644 --- a/index.d.ts +++ b/index.d.ts @@ -327,7 +327,7 @@ declare module "leap-core" { }; class ExtendedWeb3 extends Web3 { - public getUnspent(address: string | undefined, color: number, cb?: Callback>): Promise>; + public getUnspent(address: string | undefined | null, color: number, cb?: Callback>): Promise>; public getUnspent(address?: string, cb?: Callback>): Promise>; public getUnspentAll(cb?: Callback>): Promise>; public getColor(tokenContractAddress: string, cb?: Callback): Promise; From 1d43491a4d2113ff2f31e5fc71e13ff0e1e98675 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Mon, 2 Sep 2019 14:19:17 +0200 Subject: [PATCH 8/9] fix: typedef for getUnspent for tokenAddress instead of the color --- index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/index.d.ts b/index.d.ts index b029050..5be711c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -328,6 +328,7 @@ declare module "leap-core" { class ExtendedWeb3 extends Web3 { public getUnspent(address: string | undefined | null, color: number, cb?: Callback>): Promise>; + public getUnspent(address: string | undefined | null, tokenAddress: string, cb?: Callback>): Promise>; public getUnspent(address?: string, cb?: Callback>): Promise>; public getUnspentAll(cb?: Callback>): Promise>; public getColor(tokenContractAddress: string, cb?: Callback): Promise; From 29672d6d55336c9e7e6591237ee2b4f8a200cb25 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Mon, 2 Sep 2019 15:38:39 +0200 Subject: [PATCH 9/9] support getUnspent() instead of getUnspentAll. Deprecate all getUnspent* --- lib/helpers.js | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index b985b55..a4be770 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -17,6 +17,21 @@ const fromRaw = u => ({ outpoint: Outpoint.fromRaw(u.outpoint), }); +const paddedParams = (params) => { + const hasCb = typeof params[params.length - 1] === 'function'; + const paramCount = hasCb ? params.length - 1 : params.length; + + if (paramCount === 0) { // getUnspent() + return ['', '', ...params]; + } + + if (paramCount === 1) { // getUnspent(address) + return [params[0], '', ...params.splice(1)]; + } + + return params; // getUnspent(address, color) +}; + export function extendWeb3(web3Instance) { // `_extend` for web3 0.2x.x, `extend` for 1.x const extend = web3Instance._extend || web3Instance.extend; // eslint-disable-line no-underscore-dangle, max-len @@ -41,17 +56,7 @@ export function extendWeb3(web3Instance) { methods: [ new extend.Method({ ...getUnspent, - name: 'getUnspentByAddress', - params: 1, - }), - new extend.Method({ - ...getUnspent, - name: 'getUnspentByAddressColor', - }), - new extend.Method({ - ...getUnspent, - name: 'getUnspentAll', - params: 0, + name: '_getUnspent', }), new extend.Method({ name: 'getColor', @@ -110,16 +115,24 @@ export function extendWeb3(web3Instance) { ], }); - web3Instance.getUnspent = (...params) => { - const last = params[params.length - 1]; - const hasCb = typeof last === 'function'; - if (params.length === (hasCb ? 2 : 1)) { - return web3Instance.getUnspentByAddress(...params); - } + web3Instance.getUnspentAll = (...params) => { + console.warn('DEPRECATED: use getUnspent() instead'); // eslint-disable-line no-console + return web3Instance.getUnspent(...params); + }; - return web3Instance.getUnspentByAddressColor(...params); + web3Instance.getUnspentByAddress = (...params) => { + console.warn('DEPRECATED: use getUnspent(address) instead'); // eslint-disable-line no-console + return web3Instance.getUnspent(...params); }; + web3Instance.getUnspentByAddressColor = (...params) => { + console.warn('DEPRECATED: use getUnspent(address, color) instead'); // eslint-disable-line no-console + return web3Instance.getUnspent(...params); + }; + + web3Instance.getUnspent = (...params) => + web3Instance._getUnspent(...paddedParams(params)); // eslint-disable-line no-underscore-dangle + return web3Instance; }