depay-blockchain-client
Advanced tools
Comparing version 2.0.0 to 2.3.0
@@ -6,2 +6,3 @@ 'use strict'; | ||
var ethers = require('ethers'); | ||
var depayCryptoWallets = require('depay-crypto-wallets'); | ||
@@ -62,6 +63,3 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
if (_optionalChain$1([window, 'optionalAccess', _ => _.ethereum])) { | ||
let accounts = await window.ethereum.request({ method: 'eth_accounts' }); | ||
if (accounts instanceof Array) { | ||
newAccount = accounts[0]; | ||
} | ||
newAccount = await depayCryptoWallets.getWallet().account(); | ||
} | ||
@@ -85,8 +83,8 @@ | ||
let contractCall = ({ address, api, method, params, provider }) => { | ||
let contract = new ethers__default['default'].Contract(address, api, provider); | ||
let paramsToContractArgs = ({ contract, method, params }) => { | ||
let fragment = contract.interface.fragments.find((fragment) => { | ||
return fragment.name == method | ||
}); | ||
let args = fragment.inputs.map((input, index) => { | ||
return fragment.inputs.map((input, index) => { | ||
if (Array.isArray(params)) { | ||
@@ -97,3 +95,8 @@ return params[index] | ||
} | ||
}); | ||
}) | ||
}; | ||
let contractCall = ({ address, api, method, params, provider }) => { | ||
let contract = new ethers__default['default'].Contract(address, api, provider); | ||
let args = paramsToContractArgs({ contract, method, params }); | ||
return contract[method](...args) | ||
@@ -116,3 +119,6 @@ }; | ||
let parseUrl = (url) => { | ||
var parseUrl = (url) => { | ||
if (typeof url == 'object') { | ||
return url | ||
} | ||
let deconstructed = url.match(/(?<blockchain>\w+):\/\/(?<address>[\w\d]+)\/(?<method>[\w\d]+)/); | ||
@@ -140,2 +146,28 @@ return deconstructed.groups | ||
let estimate = async ({ address, method, api, params, value }) => { | ||
let account = await depayCryptoWallets.getWallet().account(); | ||
if (!account) { | ||
throw 'No wallet connected!' | ||
} | ||
let provider = new ethers.ethers.providers.Web3Provider(window.ethereum); | ||
let signer = provider.getSigner(); | ||
let contract = new ethers.ethers.Contract(address, api, provider); | ||
let args = paramsToContractArgs({ contract, method, params }); | ||
return contract.connect(signer).estimateGas[method](...args) | ||
}; | ||
let request$1 = async function (url, options) { | ||
let { blockchain, address, method } = parseUrl(url); | ||
let { api, params, value } = options || {}; | ||
switch (blockchain) { | ||
case 'ethereum': | ||
return estimate({ address, method, api, params, value }) | ||
default: | ||
throw 'Unknown blockchain: ' + blockchain | ||
} | ||
}; | ||
async function provider$1 (blockchain) { | ||
@@ -150,4 +182,5 @@ switch (blockchain) { | ||
exports.estimate = request$1; | ||
exports.provider = provider$1; | ||
exports.request = request; | ||
exports.resetCache = resetCache; |
@@ -1,2 +0,3 @@ | ||
import ethers from 'ethers'; | ||
import ethers, { ethers as ethers$1 } from 'ethers'; | ||
import { getWallet } from 'depay-crypto-wallets'; | ||
@@ -53,6 +54,3 @@ function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }let cacheStore = {}; | ||
if (_optionalChain$1([window, 'optionalAccess', _ => _.ethereum])) { | ||
let accounts = await window.ethereum.request({ method: 'eth_accounts' }); | ||
if (accounts instanceof Array) { | ||
newAccount = accounts[0]; | ||
} | ||
newAccount = await getWallet().account(); | ||
} | ||
@@ -76,8 +74,8 @@ | ||
let contractCall = ({ address, api, method, params, provider }) => { | ||
let contract = new ethers.Contract(address, api, provider); | ||
let paramsToContractArgs = ({ contract, method, params }) => { | ||
let fragment = contract.interface.fragments.find((fragment) => { | ||
return fragment.name == method | ||
}); | ||
let args = fragment.inputs.map((input, index) => { | ||
return fragment.inputs.map((input, index) => { | ||
if (Array.isArray(params)) { | ||
@@ -88,3 +86,8 @@ return params[index] | ||
} | ||
}); | ||
}) | ||
}; | ||
let contractCall = ({ address, api, method, params, provider }) => { | ||
let contract = new ethers.Contract(address, api, provider); | ||
let args = paramsToContractArgs({ contract, method, params }); | ||
return contract[method](...args) | ||
@@ -107,3 +110,6 @@ }; | ||
let parseUrl = (url) => { | ||
var parseUrl = (url) => { | ||
if (typeof url == 'object') { | ||
return url | ||
} | ||
let deconstructed = url.match(/(?<blockchain>\w+):\/\/(?<address>[\w\d]+)\/(?<method>[\w\d]+)/); | ||
@@ -131,2 +137,28 @@ return deconstructed.groups | ||
let estimate = async ({ address, method, api, params, value }) => { | ||
let account = await getWallet().account(); | ||
if (!account) { | ||
throw 'No wallet connected!' | ||
} | ||
let provider = new ethers$1.providers.Web3Provider(window.ethereum); | ||
let signer = provider.getSigner(); | ||
let contract = new ethers$1.Contract(address, api, provider); | ||
let args = paramsToContractArgs({ contract, method, params }); | ||
return contract.connect(signer).estimateGas[method](...args) | ||
}; | ||
let request$1 = async function (url, options) { | ||
let { blockchain, address, method } = parseUrl(url); | ||
let { api, params, value } = options || {}; | ||
switch (blockchain) { | ||
case 'ethereum': | ||
return estimate({ address, method, api, params, value }) | ||
default: | ||
throw 'Unknown blockchain: ' + blockchain | ||
} | ||
}; | ||
async function provider$1 (blockchain) { | ||
@@ -141,2 +173,2 @@ switch (blockchain) { | ||
export { provider$1 as provider, request, resetCache }; | ||
export { request$1 as estimate, provider$1 as provider, request, resetCache }; |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('ethers')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'ethers'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BlockchainClient = {}, global.ethers)); | ||
}(this, (function (exports, ethers) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('ethers'), require('depay-crypto-wallets')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'ethers', 'depay-crypto-wallets'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BlockchainClient = {}, global.ethers, global.CryptoWallets)); | ||
}(this, (function (exports, ethers, depayCryptoWallets) { 'use strict'; | ||
@@ -61,6 +61,3 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
if (_optionalChain$1([window, 'optionalAccess', _ => _.ethereum])) { | ||
let accounts = await window.ethereum.request({ method: 'eth_accounts' }); | ||
if (accounts instanceof Array) { | ||
newAccount = accounts[0]; | ||
} | ||
newAccount = await depayCryptoWallets.getWallet().account(); | ||
} | ||
@@ -84,8 +81,8 @@ | ||
let contractCall = ({ address, api, method, params, provider }) => { | ||
let contract = new ethers__default['default'].Contract(address, api, provider); | ||
let paramsToContractArgs = ({ contract, method, params }) => { | ||
let fragment = contract.interface.fragments.find((fragment) => { | ||
return fragment.name == method | ||
}); | ||
let args = fragment.inputs.map((input, index) => { | ||
return fragment.inputs.map((input, index) => { | ||
if (Array.isArray(params)) { | ||
@@ -96,3 +93,8 @@ return params[index] | ||
} | ||
}); | ||
}) | ||
}; | ||
let contractCall = ({ address, api, method, params, provider }) => { | ||
let contract = new ethers__default['default'].Contract(address, api, provider); | ||
let args = paramsToContractArgs({ contract, method, params }); | ||
return contract[method](...args) | ||
@@ -115,3 +117,6 @@ }; | ||
let parseUrl = (url) => { | ||
var parseUrl = (url) => { | ||
if (typeof url == 'object') { | ||
return url | ||
} | ||
let deconstructed = url.match(/(?<blockchain>\w+):\/\/(?<address>[\w\d]+)\/(?<method>[\w\d]+)/); | ||
@@ -139,2 +144,28 @@ return deconstructed.groups | ||
let estimate = async ({ address, method, api, params, value }) => { | ||
let account = await depayCryptoWallets.getWallet().account(); | ||
if (!account) { | ||
throw 'No wallet connected!' | ||
} | ||
let provider = new ethers.ethers.providers.Web3Provider(window.ethereum); | ||
let signer = provider.getSigner(); | ||
let contract = new ethers.ethers.Contract(address, api, provider); | ||
let args = paramsToContractArgs({ contract, method, params }); | ||
return contract.connect(signer).estimateGas[method](...args) | ||
}; | ||
let request$1 = async function (url, options) { | ||
let { blockchain, address, method } = parseUrl(url); | ||
let { api, params, value } = options || {}; | ||
switch (blockchain) { | ||
case 'ethereum': | ||
return estimate({ address, method, api, params, value }) | ||
default: | ||
throw 'Unknown blockchain: ' + blockchain | ||
} | ||
}; | ||
async function provider$1 (blockchain) { | ||
@@ -149,2 +180,3 @@ switch (blockchain) { | ||
exports.estimate = request$1; | ||
exports.provider = provider$1; | ||
@@ -151,0 +183,0 @@ exports.request = request; |
{ | ||
"name": "depay-blockchain-client", | ||
"moduleName": "BlockchainClient", | ||
"version": "2.0.0", | ||
"version": "2.3.0", | ||
"description": "A client to fetch blockchain data by either using the connected wallet or falling back to e.g. RPC.", | ||
@@ -35,2 +35,3 @@ "main": "dist/cjs/index.js", | ||
"dependencies": { | ||
"depay-crypto-wallets": "^2.4.0", | ||
"ethers": "^5.3.1" | ||
@@ -54,3 +55,3 @@ }, | ||
"cypress": "^6.2.1", | ||
"depay-web3mock": "^4.3.0", | ||
"depay-web3mock": "^4.4.0", | ||
"eslint": "^7.15.0", | ||
@@ -57,0 +58,0 @@ "eslint-config-prettier": "^7.0.0", |
@@ -85,3 +85,3 @@ Stop relying on connected wallets when fetching data for dApps. | ||
### cache | ||
#### cache | ||
@@ -108,3 +108,3 @@ The `cache` attribute of any request allows you to cache requests: | ||
### resetCache | ||
#### resetCache | ||
@@ -125,2 +125,39 @@ Make sure you reset cache between your tests to prevent cached states affect other tests: | ||
### estimate | ||
Allows you to estimate transactions before they happen to determine if they are possible and how much they will cost: | ||
```javascript | ||
import { estimate } from 'depay-blockchain-client' | ||
let cost = await estimate('ethereum://0xae60aC8e69414C2Dc362D0e6a03af643d1D85b92/route', { | ||
api: [{"inputs":[{"internalType":"address","name":"_configuration","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"configuration","outputs":[{"internalType":"contract DePayRouterV1Configuration","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"pluginAddress","type":"address"}],"name":"isApproved","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint256[]","name":"amounts","type":"uint256[]"},{"internalType":"address[]","name":"addresses","type":"address[]"},{"internalType":"address[]","name":"plugins","type":"address[]"},{"internalType":"string[]","name":"data","type":"string[]"}],"name":"route","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdraw","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}], | ||
params: { | ||
path: ['0x1cBb83EbcD552D5EBf8131eF8c9CD9d9BAB342bC'], | ||
amounts: ['160000000000000000', '160000000000000000', '1626096776'], | ||
addresses: ['0x4e260bB2b25EC6F3A59B478fCDe5eD5B8D783B02'], | ||
plugins: ['0x99F3F4685a7178F26EB4F4Ca8B75a1724F1577B9'], | ||
data: [] | ||
}, | ||
value: 0 | ||
} | ||
// 22111100000 | ||
``` | ||
Instead of a BigNumber estimating the cost of the transaction, `estimate` rejects the promise in case the transaction is not possible to be executed. | ||
## URL | ||
You can either use a URL like `<blockchain>://<address>/<method>` that gets deconstructed internally or you pass a deconstructed object directly: | ||
```javascript | ||
request('ethereum://0x7a250d5630b4cf539739df2c5dacb4c659f2488d/getAmountsOut', options) | ||
``` | ||
or | ||
```javascript | ||
request({ blockchain: 'ethereum', address: '0x7a250d5630b4cf539739df2c5dacb4c659f2488d', method: 'getAmountsOut'}, options) | ||
``` | ||
## Development | ||
@@ -127,0 +164,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73746
434
174
2
+ Addeddepay-crypto-wallets@^2.4.0
+ Addeddepay-blockchains@1.0.1(transitive)
+ Addeddepay-crypto-wallets@2.5.0(transitive)