Created
September 4, 2021 13:42
-
-
Save 0x62/39150fd2f92ee44860f5e0f754e9e1bd to your computer and use it in GitHub Desktop.
Hacky workaround
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ethers } from 'ethers' | |
class RpcApi { | |
constructor({ endpoint, address, abi }) { | |
this._endpoint = endpoint | |
this._address = null | |
this._abi = null | |
this._id = 0 | |
if (abi && address) this.setContract({ address, abi }) | |
} | |
get _nextId() { | |
return ++this._id | |
} | |
setContract({ address, abi }) { | |
this._address = address | |
this._abi = abi | |
this._iface = new ethers.utils.Interface(abi) | |
} | |
async balanceOf(address) { | |
const balance = await this._call('balanceOf', [address]) | |
return parseInt(balance) | |
} | |
async _call(fn, params) { | |
return this._fetch('eth_call', [{ | |
to: this._address, | |
data: this._iface.encodeFunctionData(fn, params) | |
}]) | |
} | |
async _fetch(method, params = []) { | |
const res = await fetch(this._endpoint, { | |
method: 'POST', | |
body: JSON.stringify({ | |
jsonrpc: '2.0', | |
id: this._nextId, | |
method, | |
params, | |
}) | |
}) | |
const { result, error } = await res.json() | |
if (error) { | |
throw new Error(`RPC error: ${error}`) | |
} | |
return result | |
} | |
} | |
export default RpcApi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const api = new RpcApi({ | |
endpoint: `https://eth-rinkeby.alchemyapi.io/v2/${ALCHEMY_KEY}`, | |
address: contractAddress, | |
abi, | |
}) | |
const balance = await api.balanceOf(address) |
Just incase anyone else comes here trying to do it with BSC I switched to use https://moralis.io/ for now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @0x62 yeah I was using the BSC open data seed which requires cors. Which from what I can see you can't apply with cloudflare workers 😢