From a002a07430fa797ff92cd0dc4305255fa303e425 Mon Sep 17 00:00:00 2001 From: Kosta Korenkov Date: Fri, 31 May 2019 17:12:02 +0300 Subject: [PATCH] feat: add getUnspentByAddress RPC helper and fix typedefs (#110) * feat: add getUnspentByAddress RPC helper and fix typedefs - add a getUnspentByAddress RCP helper. With the recent change to getUnspent, there is no helper to get unspents of all the colors for a given address. This feature is used in Bridge UI. - fix typedef for getUnspent * fix: fastSell to use updated getUnspent * feat: add workaround to support old getUnspent extended web3 --- index.d.ts | 1 + lib/exit.js | 2 +- lib/helpers.js | 25 ++++++++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index a794ff5..af6978b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -300,6 +300,7 @@ 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 getUnspentAll(cb?: Callback>): Promise>; public getColor(tokenContractAddress: string, cb?: Callback): Promise; diff --git a/lib/exit.js b/lib/exit.js index 49db571..60e1948 100644 --- a/lib/exit.js +++ b/lib/exit.js @@ -90,7 +90,7 @@ export default class Exit { } static fastSellAmount(account, amount, color, plasmaChain, rootChain, marketMakerUrl, signer) { - return Promise.all([plasmaChain.getUnspent(account), plasmaChain.getConfig()]).then(([unspent, nodeConfig]) => { + return Promise.all([plasmaChain.getUnspent(account, color), plasmaChain.getConfig()]).then(([unspent, nodeConfig]) => { const exitingUtxoTransfer = Tx.transferFromUtxos( unspent, account, nodeConfig.exitHandlerAddr, amount, color, ); diff --git a/lib/helpers.js b/lib/helpers.js index fbcb4a1..dff49f6 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -50,6 +50,10 @@ export class LeapEthers { .then(unspent => unspent.map(fromRaw)); } + getUnspentByAddress(...params) { + return this.getUnspent(params); + } + getUnspentAll() { return this.provider .send('plasma_unspent', []) @@ -99,7 +103,15 @@ export function extendWeb3(web3Instance) { extend({ methods: [ - new extend.Method(getUnspent), + new extend.Method({ + ...getUnspent, + name: 'getUnspentByAddress', + params: 1, + }), + new extend.Method({ + ...getUnspent, + name: 'getUnspentByAddressColor', + }), new extend.Method({ ...getUnspent, name: 'getUnspentAll', @@ -144,6 +156,17 @@ 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); + } + + return web3Instance.getUnspentByAddressColor(...params); + }; + return web3Instance; }