Skip to content

Commit

Permalink
feat: add getUnspentByAddress RPC helper and fix typedefs (#110)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
troggy authored May 31, 2019
1 parent cf7bfd8 commit a002a07
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ declare module "leap-core" {
}

class ExtendedWeb3 extends Web3 {
public getUnspent(address: string, color: number, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
public getUnspent(address: string, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
public getUnspentAll(cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
public getColor(tokenContractAddress: string, cb?: Callback<number>): Promise<number>;
Expand Down
2 changes: 1 addition & 1 deletion lib/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
25 changes: 24 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', [])
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit a002a07

Please sign in to comment.