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
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ 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, color: number, cb?: Callback<Array<Unspent>>): Promise<Array<Unspent>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about token address instead of color?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

address: string | undefined | null will be better (you use it with null in docs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both are great points!

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
77 changes: 8 additions & 69 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,74 +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) {
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', []);
}

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
Expand Down Expand Up @@ -234,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<InputTx>} promise that resolves to youngest input tx and its index
*/
Expand All @@ -247,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<Proof>} promise that resolves to period inclusion proof
*/
Expand Down Expand Up @@ -314,10 +251,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 +273,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