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

feat: allow to sendTransaction and get TransactionReceipt #1

Merged
merged 3 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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