Skip to content

Commit

Permalink
* handle if this becomes other object (#101)
Browse files Browse the repository at this point in the history
* add deprecated call log
  • Loading branch information
hewigovens authored Sep 21, 2020
1 parent fd24cd4 commit 8f3c374
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TrustWeb3Provider is available through [Jitpack](https://jitpack.io) and [GitHub

To install it:

Step 1. Add jitpack in your root build.gradle at the end of repositories:
Step 1. Add jitpack to `repositories` in your root `build.gradle` file:

```groovy
allprojects {
Expand All @@ -59,6 +59,31 @@ dependencies {

[Configuring Gradle for use with GitHub Packages](https://docs.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-gradle-for-use-with-github-packages)

Step 1. Add GitHub Packages to `repositories` in your root `build.gradle` file:

```groovy
allprojects {
repositories {
maven {
name = "GitHub Packages"
url = uri("https://maven.pkg.github.com/trustwallet/trust-web3-provider")
credentials {
username = System.getenv('GITHUB_USER')
password = System.getenv('GITHUB_TOKEN')
}
}
}
}
```

Step 2. Add the dependency

```groovy
dependencies {
implementation group: 'com.trustwallet', name: 'web3-provider', version: '0.4.2'
}
```
e
## Authors

[vikmeup](https://github.com/vikmeup)
Expand Down
4 changes: 2 additions & 2 deletions dist/trust-min.js
Git LFS file not shown
23 changes: 15 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class TrustWeb3Provider extends EventEmitter {
}

request(payload) {
var that = this || window.ethereum;
// this points to window in methods like web3.eth.getAccounts()
var that = this;
if (!(this instanceof TrustWeb3Provider)) {
that = window.ethereum
}
return that._request(payload, false);
}

Expand All @@ -55,9 +59,8 @@ class TrustWeb3Provider extends EventEmitter {
* @deprecated Use request({method: "eth_requestAccounts"}) instead.
*/
enable() {
// this may be undefined somehow
var that = this || window.ethereum;
return that.request({ method: "eth_requestAccounts", params: [] });
console.log('enable() is deprecated, please use window.ethereum.request({method: "eth_requestAccounts"}) instead.')
return this.request({ method: "eth_requestAccounts", params: [] });
}

/**
Expand Down Expand Up @@ -91,12 +94,18 @@ class TrustWeb3Provider extends EventEmitter {
* @deprecated Use request() method instead.
*/
sendAsync(payload, callback) {
console.log('sendAsync(data, callback) is deprecated, please use window.ethereum.request(data) instead.')
// this points to window in methods like web3.eth.getAccounts()
var that = this;
if (!(this instanceof TrustWeb3Provider)) {
that = window.ethereum
}
if (Array.isArray(payload)) {
Promise.all(payload.map(this._request.bind(this)))
Promise.all(payload.map(that._request.bind(that)))
.then(data => callback(null, data))
.catch(error => callback(error, null));
} else {
this._request(payload)
that._request(payload)
.then(data => callback(null, data))
.catch(error => callback(error, null));
}
Expand Down Expand Up @@ -149,8 +158,6 @@ class TrustWeb3Provider extends EventEmitter {
case "eth_newBlockFilter":
case "eth_newPendingTransactionFilter":
case "eth_uninstallFilter":
case "eth_getFilterChanges":
case "eth_getFilterLogs":
case "eth_subscribe":
throw new ProviderRpcError(
4200,
Expand Down
10 changes: 8 additions & 2 deletions src/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ describe("TrustWeb3Provider constructor tests", () => {
test("test eth_accounts", done => {
const provider = new Trust(config);
const web3 = new Web3(provider);
const addresses = ["0x5ee066cc1250e367423ed4bad3b073241612811f"];

web3.eth.getAccounts((error, accounts) => {
expect(accounts).toEqual(addresses);
done();
})

provider.request({method: "eth_accounts"}).then((accounts) => {
expect(accounts).toEqual(["0x5ee066cc1250e367423ed4bad3b073241612811f"]);
expect(accounts).toEqual(addresses);
done();
});

web3.currentProvider.sendAsync({method: "eth_accounts"}, (error, data) => {
expect(data.result).toEqual(["0x5ee066cc1250e367423ed4bad3b073241612811f"]);
expect(data.result).toEqual(addresses);
done();
});
});
Expand Down

0 comments on commit 8f3c374

Please sign in to comment.