Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: better getUnspent #143

Merged
merged 9 commits into from
Sep 2, 2019
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,9 @@ 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 getUnspent(address: string | undefined | null, color: number, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
public getUnspent(address: string | undefined | null, tokenAddress: string, 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>;
public getColors(cb?: Callback<string[]>): Promise<string[]>;
Expand Down
114 changes: 33 additions & 81 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,25 @@ 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) {
return this.getUnspent(params);
}

getUnspentAll() {
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', []);
}
const paddedParams = (params) => {
const hasCb = typeof params[params.length - 1] === 'function';
const paramCount = hasCb ? params.length - 1 : params.length;

getValidatorInfo() {
return this.provider.send('validator_getAddress', []);
if (paramCount === 0) { // getUnspent()
return ['', '', ...params];
}

checkSpendingCondition(tx) {
return this.provider.send('checkSpendingCondition', [tx.hex()]).then(({ error, outputs }) => ({
error,
outputs: outputs.map(Output.fromJSON),
}));
if (paramCount === 1) { // getUnspent(address)
return [params[0], '', ...params.splice(1)];
}

getPeriodByBlockHeight(blockHeight) {
return this.provider.send('plasma_getPeriodByBlockHeight', [blockHeight]);
}
}
return params; // getUnspent(address, color)
};

export function extendWeb3(web3Instance) {
// `_extend` for web3 0.2x.x, `extend` for 1.x
Expand All @@ -104,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',
Expand Down Expand Up @@ -173,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;
}

Expand Down Expand Up @@ -234,7 +184,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<InputTx>} promise that resolves to youngest input tx and its index
*/
Expand All @@ -247,7 +197,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<Proof>} promise that resolves to period inclusion proof
*/
Expand Down Expand Up @@ -314,10 +264,12 @@ 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<Receipt>} 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);
});
Expand All @@ -334,10 +286,10 @@ export function consolidateUTXOs(utxos) {
/**
* Returns outputs for spending condition tx
*
* @param {ExtendedWeb3 | LeapEthers} plasma
* @param {LeapTransaction} tx
* @param {ExtendedWeb3 | LeapProvider} plasma
* @param {Tx} tx
*
* @returns {Promise<Output>}
* @returns {Promise<Array<Output>>}
*/
export function simulateSpendCond(plasma, tx) {
return plasma.checkSpendingCondition(tx);
Expand Down