Skip to content

Commit

Permalink
feat: merge plasma_ helpers from LeapEthers
Browse files Browse the repository at this point in the history
  • Loading branch information
troggy committed Aug 30, 2019
1 parent acd4df7 commit 5f35b23
Show file tree
Hide file tree
Showing 3 changed files with 367 additions and 24 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"author": "Kosta Korenkov <[email protected]>",
"license": "MIT",
"scripts": {
"build": "tsc -d src/index.ts --outDir dist/"
"build": "tsc -t ES5 --esModuleInterop -d src/index.ts --outDir dist/"
},
"peerDependencies": {
"ethers": "~4.0"
},
"dependencies": {
"typescript": "^3.6.2"
"ethers": "~4.0",
"leap-core": "^0.35.0"
},
"devDependencies": {
"ethers": "~4.0"
"typescript": "^3.6.2",
"ethers": "~4.0",
"leap-core": "^0.35.0"
}
}
104 changes: 88 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,104 @@
import { JsonRpcProvider } from 'ethers/providers/json-rpc-provider';
import { Transaction } from 'ethers/utils/transaction';
import {
Outpoint, Output, Unspent, NodeConfig,
ValidatorInfo, Type, SpendCondSimResult,
Tx
} from 'leap-core';

const toLowerCase = val =>
val && val.toLowerCase ? val.toLowerCase() : val;

const fromRaw = (u): Unspent => ({
output: u.output,
outpoint: Outpoint.fromRaw(u.outpoint),
});

const hexlify = (tx: Tx<Type> | string): string => {
if ((tx as Tx<Type>).hex) {
return (tx as Tx<Type>).hex();
}
return tx as string;
};

class LeapProvider extends JsonRpcProvider {

getUnspent(address?: string, color?: string|number): Promise<Array<Unspent>> {
return this.send(
'plasma_unspent',
[toLowerCase(address), toLowerCase(color)]
)
.then(unspent => unspent.map(fromRaw));
}

getUnspentByAddress(address: string): Promise<Array<Unspent>> {
console.warn('DEPRECATED: use getUnspent(address) instead'); // eslint-disable-line no-console
return this.getUnspent(address);
}

getUnspentAll(): Promise<Array<Unspent>> {
console.warn('DEPRECATED: use getUnspent() instead'); // eslint-disable-line no-console
return this.getUnspent();
}

getColor(tokenAddress: string): Promise<number> {
return this.send('plasma_getColor', [toLowerCase(tokenAddress)]);
}

getColors(): Promise<string[]> {
return this.send('plasma_getColors', []);
}

status(): Promise<string> {
return this.send('plasma_status', []);
}

getConfig() : Promise<NodeConfig> {
return this.send('plasma_getConfig', []);
}

getValidatorInfo(): Promise<ValidatorInfo> {
return this.send('validator_getAddress', []);
}

checkSpendingCondition(tx: Tx<Type.SPEND_COND>): Promise<SpendCondSimResult> {
return this.send('checkSpendingCondition', [tx.hex()])
.then(({ error, outputs }) => ({
error,
outputs: outputs.map(Output.fromJSON),
}));
}

getPeriodByBlockHeight(blockHeightOrTag: number | string) {
return this.send('plasma_getPeriodByBlockHeight', [blockHeightOrTag]);
}

/**
* Overrides `BaseProvider.sendTransaction` to exclude tx parsing
* and resolveProperties.
*
* Given signedTransaction is non-standard, it is a LeapTransaction
* Given signedTransaction is non-standard, it is a raw Tx object
* as defined in leap-core, so we cannot parse.
* We don't need resolveProperties as LeapTransaction cannot
* have property promise.
*
* See: https://github.com/ethers-io/ethers.js/blob/7075c8c2352ec306b5679da6fbe7c2ddf7bd65d1/src.ts/providers/base-provider.ts#L873
* @param {LeapTransaction} signedTransaction signed tx
* @param {Tx<Type>|string|Promise<string>} signedTxOrHex signed Leap Tx or raw hex string of signed Leap Tx
*/
sendTransaction(signedTransaction) {
return this.ready.then(() => {
let params = { signedTransaction: signedTransaction.hex() };
return this.perform('sendTransaction', params).then((hash) => {
return this._wrapTransaction(
{ hash: signedTransaction.hash() } as Transaction,
hash
);
sendTransaction(signedTxOrHex: Tx<Type> | string | Promise<string>) {
return Promise.all([
Promise.resolve(signedTxOrHex as PromiseLike<any>),
this.ready
]).then(([signedTxOrHex]) => {
const signedTransaction = hexlify(signedTxOrHex);
return this.perform('sendTransaction', { signedTransaction })
.then((hash) => {
return this._wrapTransaction(
{ hash } as Transaction,
hash
);
}, (error) => {
error.transaction = { raw: signedTransaction.hex() };
error.transactionHash = signedTransaction.hash();
throw error;
const tx = Tx.fromRaw(signedTransaction);
error.transaction = { raw: signedTransaction };
error.transactionHash = tx.hash();
throw error;
});
});
}
Expand Down
Loading

0 comments on commit 5f35b23

Please sign in to comment.