Skip to content

Commit

Permalink
Merge pull request #1 from leapdao/feat/v1
Browse files Browse the repository at this point in the history
feat: allow to sendTransaction and get TransactionReceipt
  • Loading branch information
troggy committed Jul 29, 2019
2 parents 2cad7ed + 09fb44c commit 8ea799f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
# leap-provider
# leap-provider

Ethers.js provider to work with Leap Network.

## Install

```sh
yarn add leap-provider
```

## Usage

Connect to a Wallet:

```js
const LeapProvider = require('leap-provider');
const provider = new LeapProvider('https://testnet-node.leapdao.org');

// can be passed in a Wallet
const plasmaWallet = new ethers.Wallet(privKey, provider);

// or connected to existing wallet
const otherWallet = Wallet.createRandom();
otherWallet.connect(provider);
```

Send transaction to Leap Network:

```js
const LeapProvider = require('leap-provider');
const provider = new LeapProvider('https://testnet-node.leapdao.org');

// create some LeapTransaction
const tx = Tx.transfer(
...
);

// tx should be signed
tx.signAll(wallet.privateKey);

// send via provider
const resp = await wallet.provider.sendTransaction(tx);

// wait for inclusion, you will get a proper TransactionReceipt once tx is included in a block
const receipt = await resp.wait();
console.log(receipt);
```
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const ethers = require('ethers');

class LeapProvider extends ethers.providers.JsonRpcProvider {

/**
* Overrides `BaseProvider.sendTransaction` to exclude tx parsing
* and resolveProperties.
*
* Given signedTransaction is non-standard, it is a LeapTransaction
* 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
*/
sendTransaction(signedTransaction) {
return this.ready.then(() => {
let params = { signedTransaction: signedTransaction.hex() };
return this.perform('sendTransaction', params).then((hash) => {
return this._wrapTransaction({ hash: signedTransaction.hash() }, hash);
}, (error) => {
error.transaction = { raw: signedTransaction.hex() };
error.transactionHash = signedTransaction.hash();
throw error;
});
});
}

}

module.exports = LeapProvider;
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "leap-provider",
"version": "1.0.0",
"description": "Ethers.js Provider for Leap Network",
"main": "index.js",
"repository": "[email protected]:leapdao/leap-provider.git",
"author": "Kosta Korenkov <[email protected]>",
"license": "MIT",
"peerDependencies": {
"ethers": "~4.0"
}
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


0 comments on commit 8ea799f

Please sign in to comment.