Skip to content

Commit

Permalink
wip: frontend with viem
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKheops committed Oct 27, 2023
1 parent 59839df commit 97d7fae
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 302 deletions.
30 changes: 25 additions & 5 deletions apps/extension/src/core/domains/ethereum/handler.tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { CustomErc20Token } from "@core/domains/tokens/types"
import i18next from "@core/i18nConfig"
import {
ETH_ERROR_EIP1474_INTERNAL_ERROR,
ETH_ERROR_EIP1474_INVALID_INPUT,
ETH_ERROR_EIP1474_INVALID_PARAMS,
ETH_ERROR_EIP1474_RESOURCE_UNAVAILABLE,
Expand All @@ -35,7 +36,15 @@ import { isEthereumAddress } from "@polkadot/util-crypto"
import { convertAddress } from "@talisman/util/convertAddress"
import { githubUnknownTokenLogoUrl } from "@talismn/chaindata-provider"
import { throwAfter } from "@talismn/util"
import { PublicClient, createClient, getAddress, http, recoverMessageAddress, toHex } from "viem"
import {
PublicClient,
RpcError,
createClient,
getAddress,
http,
recoverMessageAddress,
toHex,
} from "viem"
import { hexToNumber } from "viem/utils"

import { getErc20TokenInfo } from "../../util/getErc20TokenInfo"
Expand Down Expand Up @@ -811,7 +820,7 @@ export class EthTabsHandler extends TabsHandler {
}
}

handle<TMessageType extends keyof RequestSignatures>(
async handle<TMessageType extends keyof RequestSignatures>(
id: string,
type: TMessageType,
request: RequestTypes[TMessageType],
Expand All @@ -822,9 +831,20 @@ export class EthTabsHandler extends TabsHandler {
case "pub(eth.subscribe)":
return this.ethSubscribe(id, url, port)

case "pub(eth.request)":
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.ethRequest(id, url, request as AnyEthRequest, port) as any
case "pub(eth.request)": {
try {
return await this.ethRequest(id, url, request as AnyEthRequest, port)
} catch (err) {
if (err instanceof EthProviderRpcError) throw err
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { code, shortMessage, message } = err as RpcError
throw new EthProviderRpcError(
shortMessage ?? message ?? "Internal error",
code ?? ETH_ERROR_EIP1474_INTERNAL_ERROR,
shortMessage ? message : undefined
)
}
}

default:
throw new Error(`Unable to handle message of type ${type}`)
Expand Down
230 changes: 10 additions & 220 deletions apps/extension/src/core/util/abi/abiErc20.ts
Original file line number Diff line number Diff line change
@@ -1,222 +1,12 @@
export const abiErc20 = [
{
type: "event",
name: "Approval",
inputs: [
{
indexed: true,
name: "owner",
type: "address",
},
{
indexed: true,
name: "spender",
type: "address",
},
{
indexed: false,
name: "value",
type: "uint256",
},
],
},
{
type: "event",
name: "Transfer",
inputs: [
{
indexed: true,
name: "from",
type: "address",
},
{
indexed: true,
name: "to",
type: "address",
},
{
indexed: false,
name: "value",
type: "uint256",
},
],
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "owner",
type: "address",
},
{
indexed: true,
internalType: "address",
name: "operator",
type: "address",
},
{
indexed: false,
internalType: "bool",
name: "approved",
type: "bool",
},
],
name: "ApprovalForAll",
type: "event",
},
{
type: "function",
name: "allowance",
stateMutability: "view",
inputs: [
{
name: "owner",
type: "address",
},
{
name: "spender",
type: "address",
},
],
outputs: [
{
name: "",
type: "uint256",
},
],
},
{
type: "function",
name: "approve",
stateMutability: "nonpayable",
inputs: [
{
name: "spender",
type: "address",
},
{
name: "amount",
type: "uint256",
},
],
outputs: [
{
name: "",
type: "bool",
},
],
},
{
type: "function",
name: "balanceOf",
stateMutability: "view",
inputs: [
{
name: "account",
type: "address",
},
],
outputs: [
{
name: "",
type: "uint256",
},
],
},
{
type: "function",
name: "decimals",
stateMutability: "view",
inputs: [],
outputs: [
{
name: "",
type: "uint8",
},
],
},
{
type: "function",
name: "name",
stateMutability: "view",
inputs: [],
outputs: [
{
name: "",
type: "string",
},
],
},
{
type: "function",
name: "symbol",
stateMutability: "view",
inputs: [],
outputs: [
{
name: "",
type: "string",
},
],
},
{
type: "function",
name: "totalSupply",
stateMutability: "view",
inputs: [],
outputs: [
{
name: "",
type: "uint256",
},
],
},
{
type: "function",
name: "transfer",
stateMutability: "nonpayable",
inputs: [
{
name: "recipient",
type: "address",
},
{
name: "amount",
type: "uint256",
},
],
outputs: [
{
name: "",
type: "bool",
},
],
},
{
type: "function",
name: "transferFrom",
stateMutability: "nonpayable",
inputs: [
{
name: "sender",
type: "address",
},
{
name: "recipient",
type: "address",
},
{
name: "amount",
type: "uint256",
},
],
outputs: [
{
name: "",
type: "bool",
},
],
},
"function balanceOf(address owner) view returns (uint256)",
"function decimals() view returns (uint8)",
"function symbol() view returns (string)",
"function name() view returns (string)",

"function totalSupply() view returns (uint256)",
"function transfer(address to, uint256 amount) returns (bool)",
"function allowance(address owner, address spender) view returns (uint256)",
"function approve(address spender, uint256 amount) returns (bool)",
"function transferFrom(address from, address to, uint256 amount) returns (bool)",
] as const
4 changes: 4 additions & 0 deletions apps/extension/src/core/util/abi/abiErc721.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const abiErc721 = [
"function symbol() view returns (string)",
"function name() view returns (string)",
"function tokenURI(uint256 tokenId) view returns (string)",

"function supportsInterface(bytes4 interfaceId) external view returns (bool)",
"function balanceOf(address owner) external view returns (uint256)",
"function ownerOf(uint256 tokenId) external view returns (address)",
Expand Down
10 changes: 5 additions & 5 deletions apps/extension/src/core/util/getErc20ContractData.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ethers } from "ethers"
import { Client, getContract } from "viem"

import { abiErc20 } from "./abi"
import { Client, getContract, parseAbi } from "viem"

const ABI_ERC20 = [
"function symbol() view returns (string)",
"function decimals() view returns (uint8)",
]
] as const

const PARSED_ABI_ERC20 = parseAbi(ABI_ERC20)

export type Erc20ContractData = {
symbol: string
Expand All @@ -31,7 +31,7 @@ export const getErc20ContractData = async (
): Promise<Erc20ContractData> => {
const contract = getContract({
address: contractAddress,
abi: abiErc20,
abi: PARSED_ABI_ERC20,
publicClient: client,
})
const [symbol, decimals] = await Promise.all([contract.read.symbol(), contract.read.decimals()])
Expand Down
28 changes: 2 additions & 26 deletions apps/extension/src/core/util/getErc20TokenInfo.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
import { EvmAddress, EvmNetworkId } from "@core/domains/ethereum/types"
import { CustomErc20TokenCreate } from "@core/domains/tokens/types"
import { ethers } from "ethers"
import { Client } from "viem"

import { getCoinGeckoErc20Coin } from "./coingecko/getCoinGeckoErc20Coin"
import { getErc20ContractData, getErc20ContractDataOld } from "./getErc20ContractData"

/**
* @deprecated use viem
*/
export const getErc20TokenInfoOld = async (
provider: ethers.providers.JsonRpcProvider,
evmNetworkId: EvmNetworkId,
contractAddress: EvmAddress
): Promise<CustomErc20TokenCreate> => {
const [{ decimals, symbol }, coinGeckoData] = await Promise.all([
getErc20ContractDataOld(provider, contractAddress),
getCoinGeckoErc20Coin(evmNetworkId, contractAddress),
])

return {
evmNetworkId,
contractAddress,
decimals,
symbol,
image: coinGeckoData?.image.small,
coingeckoId: coinGeckoData?.id,
}
}
import { getErc20ContractData } from "./getErc20ContractData"

export const getErc20TokenInfo = async (
client: Client,
evmNetworkId: EvmNetworkId,
contractAddress: `0x${string}`
contractAddress: EvmAddress
): Promise<CustomErc20TokenCreate> => {
const [{ decimals, symbol }, coinGeckoData] = await Promise.all([
getErc20ContractData(client, contractAddress),
Expand Down
Loading

0 comments on commit 97d7fae

Please sign in to comment.