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

fix: user get balance #50

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions .yarn/versions/9d88b67e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
releases:
"@near-eth/nep141-erc20": patch
rainbow-bridge-client-monorepo: patch
15 changes: 15 additions & 0 deletions packages/nep141-erc20/src/natural-erc20/getBalance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Web3 from 'web3'
import { getEthProvider } from '@near-eth/client/dist/utils'

export default async function getBalance (address, user) {
if (!user) return null

const web3 = new Web3(getEthProvider())

const erc20Contract = new web3.eth.Contract(
JSON.parse(process.env.ethErc20AbiText),
address
)

return await erc20Contract.methods.balanceOf(user).call()
}
34 changes: 15 additions & 19 deletions packages/nep141-erc20/src/natural-erc20/getMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import Web3 from 'web3'
import getName from './getName'
import { getEthProvider } from '@near-eth/client/dist/utils'

async function getBalance (address, user) {
if (!user) return null
const erc20Symbols = {}
export async function getSymbol (address) {
if (erc20Symbols[address]) return erc20Symbols[address]

const web3 = new Web3(getEthProvider())

const erc20Contract = new web3.eth.Contract(
const contract = new web3.eth.Contract(
JSON.parse(process.env.ethErc20AbiText),
address
)

return await erc20Contract.methods.balanceOf(user).call()
erc20Symbols[address] = await contract.methods.symbol().call()
.catch(() => address.slice(0, 5) + '…')

return erc20Symbols[address]
}

const erc20Decimals = {}
Expand Down Expand Up @@ -56,29 +60,21 @@ async function getIcon (address) {

/**
* Fetch name, icon, and decimals (precision) of ERC20 token with given `address`.
*
* Can provide an Ethereum wallet address as second argument, in which case that
* wallet's balance will also be returned. If omitted, `balance` is returned as `null`.
*
* Values other than `balance` are cached.
*
* @param address ERC20 token contract address
* @param user (optional) Ethereum wallet address that may hold tokens with given `address`
*
* @returns {Promise<{ address: string, balance: number|null, decimals: number, icon: string|null, name: string }>}
* @returns {Promise<{ address: string, decimals: number, icon: string|null, name: string }>}
*/
export default async function getErc20Data (address, user) {
const [balance, decimals, icon, name] = await Promise.all([
getBalance(address, user),
export default async function getMetadata (address) {
const [decimals, icon, name, symbol] = await Promise.all([
getDecimals(address),
getIcon(address),
getName(address)
getName(address),
getSymbol(address)
])
return {
address,
balance,
decimals,
icon,
name
name,
symbol
}
}
2 changes: 1 addition & 1 deletion packages/nep141-erc20/src/natural-erc20/getName.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function getName (address) {
address
)

erc20Names[address] = await contract.methods.symbol().call()
erc20Names[address] = await contract.methods.name().call()
.catch(() => address.slice(0, 5) + '…')

return erc20Names[address]
Expand Down
1 change: 1 addition & 0 deletions packages/nep141-erc20/src/natural-erc20/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { getBalance } from './getBalance'
export { default as getMetadata } from './getMetadata'
export { default as getAllowance } from './getAllowance'
export { default as getName } from './getName'
Expand Down
2 changes: 1 addition & 1 deletion packages/nep141-erc20/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../../tsconfig.json",
"include": ["src/*"],
"include": ["src/*", "src/natural-erc20/getBalance.js"],
0x3bfc marked this conversation as resolved.
Show resolved Hide resolved
"compilerOptions": {
"allowJs": true, /* Allow javascript files to be compiled. */
"outDir": "./dist/"
Expand Down