Skip to content

Commit

Permalink
add call static (#11)
Browse files Browse the repository at this point in the history
* add call static

* v10.16.0: adds callStatic to request
  • Loading branch information
10xSebastian authored Jun 21, 2023
1 parent aa4371a commit 46a13b3
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 24 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ request('ethereum://0x7a250d5630b4cf539739df2c5dacb4c659f2488d/getAmountsOut', {
}).then((value)=>console.log(value))
```

##### EVM: callStatic

If a function fragment is of type stateMutability `nonpayable`, you can request data the same way:

```javascript
import { request } from '@depay/web3-client'

request('ethereum://0x61fFE014bA17989E743c5F6cB21bF9697530B21e/quoteExactInput', {
api: [{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_WETH9","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH9","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"uint256","name":"amountIn","type":"uint256"}],"name":"quoteExactInput","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint160[]","name":"sqrtPriceX96AfterList","type":"uint160[]"},{"internalType":"uint32[]","name":"initializedTicksCrossedList","type":"uint32[]"},{"internalType":"uint256","name":"gasEstimate","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160"}],"internalType":"struct IQuoterV2.QuoteExactInputSingleParams","name":"params","type":"tuple"}],"name":"quoteExactInputSingle","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint160","name":"sqrtPriceX96After","type":"uint160"},{"internalType":"uint32","name":"initializedTicksCrossed","type":"uint32"},{"internalType":"uint256","name":"gasEstimate","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"uint256","name":"amountOut","type":"uint256"}],"name":"quoteExactOutput","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint160[]","name":"sqrtPriceX96AfterList","type":"uint160[]"},{"internalType":"uint32[]","name":"initializedTicksCrossedList","type":"uint32[]"},{"internalType":"uint256","name":"gasEstimate","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160"}],"internalType":"struct IQuoterV2.QuoteExactOutputSingleParams","name":"params","type":"tuple"}],"name":"quoteExactOutputSingle","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint160","name":"sqrtPriceX96After","type":"uint160"},{"internalType":"uint32","name":"initializedTicksCrossed","type":"uint32"},{"internalType":"uint256","name":"gasEstimate","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"int256","name":"amount0Delta","type":"int256"},{"internalType":"int256","name":"amount1Delta","type":"int256"},{"internalType":"bytes","name":"path","type":"bytes"}],"name":"uniswapV3SwapCallback","outputs":[],"stateMutability":"view","type":"function"}],
params: {
path: ethers.utils.solidityPack(["address","uint24","address"],['0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb',3000,'0x6b175474e89094c44da98b954eedeac495271d0f']),
amountIn: '100000000000000000000'
}
}).then((value)=>console.log(value))
```

##### Solana: request data

###### Solana: getAccountInfo
Expand Down
2 changes: 1 addition & 1 deletion dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="icon" type="image/png" href="https://depay.com/favicon.png"/>
<title>Development</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdn.ethers.io/lib/ethers-5.6.umd.min.js" type="application/javascript"></script>
<script src="https://cdn.ethers.io/lib/ethers-5.7.umd.min.js" type="application/javascript"></script>
<script crossorigin src="https://unpkg.com/@depay/solana-web3.js@1"></script>
<script crossorigin src="https://unpkg.com/@depay/web3-blockchains@8"></script>
<script src="tmp/index.dev.js"></script>
Expand Down
11 changes: 8 additions & 3 deletions dist/esm/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,14 @@ let paramsToContractArgs = ({ contract, method, params }) => {
};

const contractCall = ({ address, api, method, params, provider, block }) => {
let contract = new ethers.Contract(address, api, provider);
let args = paramsToContractArgs({ contract, method, params });
return contract[method](...args, { blockTag: block })
const contract = new ethers.Contract(address, api, provider);
const args = paramsToContractArgs({ contract, method, params });
const fragment = contract.interface.fragments.find((fragment)=>fragment.name === method);
if(fragment && fragment.stateMutability === 'nonpayable') {
return contract.callStatic[method](...args, { blockTag: block })
} else {
return contract[method](...args, { blockTag: block })
}
};

const balance = ({ address, provider }) => {
Expand Down
11 changes: 8 additions & 3 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,14 @@ let paramsToContractArgs = ({ contract, method, params }) => {
};

const contractCall = ({ address, api, method, params, provider, block }) => {
let contract = new ethers.Contract(address, api, provider);
let args = paramsToContractArgs({ contract, method, params });
return contract[method](...args, { blockTag: block })
const contract = new ethers.Contract(address, api, provider);
const args = paramsToContractArgs({ contract, method, params });
const fragment = contract.interface.fragments.find((fragment)=>fragment.name === method);
if(fragment && fragment.stateMutability === 'nonpayable') {
return contract.callStatic[method](...args, { blockTag: block })
} else {
return contract[method](...args, { blockTag: block })
}
};

const balance$1 = ({ address, provider }) => {
Expand Down
11 changes: 8 additions & 3 deletions dist/umd/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,14 @@
};

const contractCall = ({ address, api, method, params, provider, block }) => {
let contract = new ethers.ethers.Contract(address, api, provider);
let args = paramsToContractArgs({ contract, method, params });
return contract[method](...args, { blockTag: block })
const contract = new ethers.ethers.Contract(address, api, provider);
const args = paramsToContractArgs({ contract, method, params });
const fragment = contract.interface.fragments.find((fragment)=>fragment.name === method);
if(fragment && fragment.stateMutability === 'nonpayable') {
return contract.callStatic[method](...args, { blockTag: block })
} else {
return contract[method](...args, { blockTag: block })
}
};

const balance = ({ address, provider }) => {
Expand Down
11 changes: 8 additions & 3 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,14 @@
};

const contractCall = ({ address, api, method, params, provider, block }) => {
let contract = new ethers.ethers.Contract(address, api, provider);
let args = paramsToContractArgs({ contract, method, params });
return contract[method](...args, { blockTag: block })
const contract = new ethers.ethers.Contract(address, api, provider);
const args = paramsToContractArgs({ contract, method, params });
const fragment = contract.interface.fragments.find((fragment)=>fragment.name === method);
if(fragment && fragment.stateMutability === 'nonpayable') {
return contract.callStatic[method](...args, { blockTag: block })
} else {
return contract[method](...args, { blockTag: block })
}
};

const balance$1 = ({ address, provider }) => {
Expand Down
2 changes: 1 addition & 1 deletion package.evm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-client-evm",
"moduleName": "Web3Client",
"version": "10.15.1",
"version": "10.16.0",
"description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.",
"main": "dist/umd/index.evm.js",
"module": "dist/esm/index.evm.js",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-client",
"moduleName": "Web3Client",
"version": "10.15.1",
"version": "10.16.0",
"description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
Expand Down Expand Up @@ -45,7 +45,7 @@
"@babel/preset-env": "^7.12.7",
"@depay/solana-web3.js": "^1.24.0",
"@depay/web3-blockchains": "^8.1.1",
"@depay/web3-mock": "^14.12.1",
"@depay/web3-mock": "^14.13.0",
"@rollup/plugin-commonjs": "^22.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
Expand Down
2 changes: 1 addition & 1 deletion package.solana.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-client-solana",
"moduleName": "Web3Client",
"version": "10.15.1",
"version": "10.16.0",
"description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.",
"main": "dist/umd/index.solana.js",
"module": "dist/esm/index.solana.js",
Expand Down
11 changes: 8 additions & 3 deletions src/platforms/evm/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { getConfiguration } from '../../configuration'
import { paramsToContractArgs } from './contract'

const contractCall = ({ address, api, method, params, provider, block }) => {
let contract = new ethers.Contract(address, api, provider)
let args = paramsToContractArgs({ contract, method, params })
return contract[method](...args, { blockTag: block })
const contract = new ethers.Contract(address, api, provider)
const args = paramsToContractArgs({ contract, method, params })
const fragment = contract.interface.fragments.find((fragment)=>fragment.name === method)
if(fragment && fragment.stateMutability === 'nonpayable') {
return contract.callStatic[method](...args, { blockTag: block })
} else {
return contract[method](...args, { blockTag: block })
}
}

const balance = ({ address, provider }) => {
Expand Down
59 changes: 59 additions & 0 deletions tests/units/platforms/evm/request/callStatic.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ethers } from 'ethers'
import { mock, resetMocks, connect } from '@depay/web3-mock'
import { request, getProvider, resetCache } from 'src/'
import { supported } from 'src/blockchains'

describe('callStatic', () => {

supported.evm.forEach((blockchain)=>{

describe(blockchain, ()=> {
beforeEach(resetMocks)
beforeEach(resetCache)

let provider
const accounts = ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045']

beforeEach(async ()=>{
resetMocks()
resetCache()
provider = await getProvider(blockchain)
mock({ blockchain, provider, accounts: { return: accounts } })
})

it('should request blockchain data via callStatic', async ()=> {

let api = [{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_WETH9","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH9","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"uint256","name":"amountIn","type":"uint256"}],"name":"quoteExactInput","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint160[]","name":"sqrtPriceX96AfterList","type":"uint160[]"},{"internalType":"uint32[]","name":"initializedTicksCrossedList","type":"uint32[]"},{"internalType":"uint256","name":"gasEstimate","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160"}],"internalType":"struct IQuoterV2.QuoteExactInputSingleParams","name":"params","type":"tuple"}],"name":"quoteExactInputSingle","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint160","name":"sqrtPriceX96After","type":"uint160"},{"internalType":"uint32","name":"initializedTicksCrossed","type":"uint32"},{"internalType":"uint256","name":"gasEstimate","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"uint256","name":"amountOut","type":"uint256"}],"name":"quoteExactOutput","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint160[]","name":"sqrtPriceX96AfterList","type":"uint160[]"},{"internalType":"uint32[]","name":"initializedTicksCrossedList","type":"uint32[]"},{"internalType":"uint256","name":"gasEstimate","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint160","name":"sqrtPriceLimitX96","type":"uint160"}],"internalType":"struct IQuoterV2.QuoteExactOutputSingleParams","name":"params","type":"tuple"}],"name":"quoteExactOutputSingle","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint160","name":"sqrtPriceX96After","type":"uint160"},{"internalType":"uint32","name":"initializedTicksCrossed","type":"uint32"},{"internalType":"uint256","name":"gasEstimate","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"int256","name":"amount0Delta","type":"int256"},{"internalType":"int256","name":"amount1Delta","type":"int256"},{"internalType":"bytes","name":"path","type":"bytes"}],"name":"uniswapV3SwapCallback","outputs":[],"stateMutability":"view","type":"function"}]
const path = ethers.utils.solidityPack(["address","uint24","address"],['0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb',3000,'0x6b175474e89094c44da98b954eedeac495271d0f'])

mock({
blockchain,
provider,
request: {
to: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e',
api,
method: 'quoteExactInput',
params: [path, '100000000000000000000'],
return: ['16708429622155241142', ['193785372217458754790946913570'], [0], '81243']
}
})

let data = await request({
blockchain,
address: '0x61fFE014bA17989E743c5F6cB21bF9697530B21e',
method: 'quoteExactInput',
api,
params: {
path: path,
amountIn: '100000000000000000000'
}
})

expect(data.amountOut.toString()).toEqual('16708429622155241142')
expect(data.sqrtPriceX96AfterList[0].toString()).toEqual('193785372217458754790946913570')
expect(data.initializedTicksCrossedList[0]).toEqual(0)
expect(data.gasEstimate.toString()).toEqual('81243')
})
})
})
})
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -906,10 +906,10 @@
resolved "https://registry.yarnpkg.com/@depay/web3-blockchains/-/web3-blockchains-8.1.1.tgz#1de5dff71ead45bde57a1d8e0374d24aadde6533"
integrity sha512-wrtCFNH726Y+xX1+EtzhILMfhLcPEzuaBV3oCef97eJsht0PYjEWpzqB48LwHpY9adwzjarG1dSJIR2VAWpmUA==

"@depay/web3-mock@^14.12.1":
version "14.12.1"
resolved "https://registry.yarnpkg.com/@depay/web3-mock/-/web3-mock-14.12.1.tgz#55380f08c0bb303a2d4dd6e25ee608a5f2830fe9"
integrity sha512-25w7+yFBd2qlzndXbRofBKEv60gjb3GmcHMO7mzduO7egW1Qp0+84r+FJm33Gt987DASsCynHgrm7nrfpIEIRA==
"@depay/web3-mock@^14.13.0":
version "14.13.0"
resolved "https://registry.yarnpkg.com/@depay/web3-mock/-/web3-mock-14.13.0.tgz#37c7364a4a26dde4de6051495229053c5920a17a"
integrity sha512-/ZYe2PYMJI7nYIOlRCwX/IslH+tYyd+RJQ7kn161fA7zShjELTbI33rZ/Xl0Ic/RGvYa1GHMS4KV1H2FehRTgQ==
dependencies:
"@depay/solana-web3.js" "^1.24.0"
"@depay/web3-blockchains" "^8.1.1"
Expand Down

0 comments on commit 46a13b3

Please sign in to comment.