From 9264ce52b97ebd69959a7dac3ea9f1c6c4220309 Mon Sep 17 00:00:00 2001 From: Harsh R <53080940+fullstackninja864@users.noreply.github.com> Date: Thu, 12 Oct 2023 11:24:24 +0530 Subject: [PATCH 1/2] feature(core): added rpc calls for playground to calculate evm balance (#4053) * feature(core): added rpc calls for playground to calculate evm balance * chore --- babel.config.js | 1 + mobile-app/App.tsx | 5 +- .../app/api/transaction/transfer_domain.ts | 7 +- mobile-app/app/contexts/EVMProvider.tsx | 31 ++ .../Portfolio/hooks/EvmTokenBalances.ts | 27 +- shared/contracts/DST20V1.json | 298 ++++++++++++++++++ .../app => shared}/contracts/README.md | 0 .../contracts/TransferDomainV1.json | 0 shared/store/evm.ts | 51 ++- tsconfig.json | 2 + 10 files changed, 407 insertions(+), 15 deletions(-) create mode 100644 mobile-app/app/contexts/EVMProvider.tsx create mode 100644 shared/contracts/DST20V1.json rename {mobile-app/app => shared}/contracts/README.md (100%) rename {mobile-app/app => shared}/contracts/TransferDomainV1.json (100%) diff --git a/babel.config.js b/babel.config.js index e00f1a1c37..ce37b6be8b 100644 --- a/babel.config.js +++ b/babel.config.js @@ -14,6 +14,7 @@ module.exports = function (api) { "@shared-api": "./shared/api", "@shared-contexts": "./shared/contexts", "@shared-types": "./shared/types", + "@shared-contracts": "./shared/contracts", "@screens": "./mobile-app/app/screens", "@store": "./shared/store", "@translations": "./shared/translations", diff --git a/mobile-app/App.tsx b/mobile-app/App.tsx index 94fa5fc1da..9a134df64f 100644 --- a/mobile-app/App.tsx +++ b/mobile-app/App.tsx @@ -47,6 +47,7 @@ import { WalletToast } from "@components/WalletToast"; import { ServiceProviderPersistence } from "@api/wallet/service_provider"; import { FavouritePoolpairProvider } from "@contexts/FavouritePoolpairContext"; import BigNumber from "bignumber.js"; +import { EVMProvider } from "@contexts/EVMProvider"; /** * Loads @@ -120,7 +121,9 @@ export default function App(): JSX.Element | null { > -
+ +
+ diff --git a/mobile-app/app/api/transaction/transfer_domain.ts b/mobile-app/app/api/transaction/transfer_domain.ts index 5e75a702d2..96ae667d6a 100644 --- a/mobile-app/app/api/transaction/transfer_domain.ts +++ b/mobile-app/app/api/transaction/transfer_domain.ts @@ -13,7 +13,8 @@ import { NetworkName } from "@defichain/jellyfish-network"; import { ConvertDirection } from "@screens/enum"; import { parseUnits } from "ethers/lib/utils"; import { getEthRpcUrl } from "@store/evm"; -import TransferDomainV1 from "../../contracts/TransferDomainV1.json"; +import { SecuredStoreAPI } from "@api/secured"; +import TransferDomainV1 from "@shared-contracts/TransferDomainV1.json"; const TD_CONTRACT_ADDR = "0xdf00000000000000000000000000000000000001"; @@ -222,8 +223,8 @@ async function createSignedEvmTx({ const transferDST20 = [contractAddress, from, to, parsedAmount, vmAddress]; data = tdFace.encodeFunctionData("transferDST20", transferDST20); } - - const ethRpc = new providers.JsonRpcProvider(await getEthRpcUrl()); + const network = await SecuredStoreAPI.getNetwork(); + const ethRpc = new providers.JsonRpcProvider(getEthRpcUrl(network)); const wallet = new ethers.Wallet(privateKey); /* TODO: Figure out CORS issue when using the ethRpc */ diff --git a/mobile-app/app/contexts/EVMProvider.tsx b/mobile-app/app/contexts/EVMProvider.tsx new file mode 100644 index 0000000000..7ab54bc47d --- /dev/null +++ b/mobile-app/app/contexts/EVMProvider.tsx @@ -0,0 +1,31 @@ +import React, { createContext, useContext, useMemo } from "react"; +import { providers } from "ethers"; +import { useNetworkContext } from "@waveshq/walletkit-ui"; +import { getEthRpcUrl } from "@store/evm"; + +interface EVMProviderContextI { + provider: providers.JsonRpcProvider; +} +const EVMProviderContext = createContext(undefined as any); + +export function useEVMProvider(): EVMProviderContextI { + return useContext(EVMProviderContext); +} + +export function EVMProvider({ + children, +}: React.PropsWithChildren): JSX.Element | null { + const { network } = useNetworkContext(); + const client = useMemo( + () => ({ + provider: new providers.JsonRpcProvider(getEthRpcUrl(network)), + }), + [network], + ); + + return ( + + {children} + + ); +} diff --git a/mobile-app/app/screens/AppNavigator/screens/Portfolio/hooks/EvmTokenBalances.ts b/mobile-app/app/screens/AppNavigator/screens/Portfolio/hooks/EvmTokenBalances.ts index 0255b93b4f..311365e467 100644 --- a/mobile-app/app/screens/AppNavigator/screens/Portfolio/hooks/EvmTokenBalances.ts +++ b/mobile-app/app/screens/AppNavigator/screens/Portfolio/hooks/EvmTokenBalances.ts @@ -9,6 +9,8 @@ import { useLogger } from "@shared-contexts/NativeLoggingProvider"; import { useWalletContext } from "@shared-contexts/WalletContext"; import { useAppDispatch } from "@hooks/useAppDispatch"; import { fetchEvmWalletDetails, fetchEvmTokenBalances } from "@store/evm"; +import { useEVMProvider } from "@contexts/EVMProvider"; +import { useIsFocused } from "@react-navigation/native"; interface AssociatedToken { [key: string]: TokenData; @@ -21,9 +23,18 @@ export function useEvmTokenBalances(): { evmTokens: WalletToken[] } { useState({}); const blockCount = useSelector((state: RootState) => state.block.count); const { network } = useNetworkContext(); + const { provider } = useEVMProvider(); const logger = useLogger(); + const isFocused = useIsFocused(); const { allTokens } = useSelector((state: RootState) => state.wallet); + const tokenIds = Object.keys(allTokens).reduce((current: string[], key) => { + const token = allTokens[key]; + if (token.id !== "0" && token.isDAT && !token.isLPS) { + return [...current, token.id]; + } + return current; + }, []); const { evmWalletDetails, evmTokenBalances } = useSelector( (state: RootState) => state.evm, ); @@ -84,11 +95,15 @@ export function useEvmTokenBalances(): { evmTokens: WalletToken[] } { }; useEffect(() => { - batch(() => { - dispatch(fetchEvmWalletDetails({ network, evmAddress })); - dispatch(fetchEvmTokenBalances({ network, evmAddress })); - }); - }, [network, evmAddress, blockCount]); + if (isFocused) { + batch(() => { + dispatch(fetchEvmWalletDetails({ network, evmAddress, provider })); + dispatch( + fetchEvmTokenBalances({ network, evmAddress, provider, tokenIds }), + ); + }); + } + }, [network, evmAddress, blockCount, isFocused]); useEffect(() => { setAllTokensWithAddress( @@ -107,7 +122,7 @@ export function useEvmTokenBalances(): { evmTokens: WalletToken[] } { return { evmTokens }; } -function getAddressFromDST20TokenId(tokenId: string): string { +export function getAddressFromDST20TokenId(tokenId: string): string { const parsedTokenId = BigInt(tokenId); const numberStr = parsedTokenId.toString(16); // Convert parsedTokenId to hexadecimal const paddedNumberStr = numberStr.padStart(38, "0"); // Pad with zeroes to the left diff --git a/shared/contracts/DST20V1.json b/shared/contracts/DST20V1.json new file mode 100644 index 0000000000..c611c8bc9d --- /dev/null +++ b/shared/contracts/DST20V1.json @@ -0,0 +1,298 @@ +{ + "_format": "hh-sol-artifact-1", + "contractName": "DST20V1", + "sourceName": "contracts/DST20V1.sol", + "abi": [ + { + "inputs": [ + { + "internalType": "string", + "name": "name", + "type": "string" + }, + { + "internalType": "string", + "name": "symbol", + "type": "string" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "internalType": "address", + "name": "spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "account", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "decimals", + "outputs": [ + { + "internalType": "uint8", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "subtractedValue", + "type": "uint256" + } + ], + "name": "decreaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "spender", + "type": "address" + }, + { + "internalType": "uint256", + "name": "addedValue", + "type": "uint256" + } + ], + "name": "increaseAllowance", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "name", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "symbol", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "nonpayable", + "type": "function" + } + ], + "bytecode": "0x60806040523480156200001157600080fd5b50604051620017f0380380620017f08339818101604052810190620000379190620001fa565b818181600390816200004a9190620004ca565b5080600490816200005c9190620004ca565b5050505050620005b1565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b620000d08262000085565b810181811067ffffffffffffffff82111715620000f257620000f162000096565b5b80604052505050565b60006200010762000067565b9050620001158282620000c5565b919050565b600067ffffffffffffffff82111562000138576200013762000096565b5b620001438262000085565b9050602081019050919050565b60005b838110156200017057808201518184015260208101905062000153565b60008484015250505050565b6000620001936200018d846200011a565b620000fb565b905082815260208101848484011115620001b257620001b162000080565b5b620001bf84828562000150565b509392505050565b600082601f830112620001df57620001de6200007b565b5b8151620001f18482602086016200017c565b91505092915050565b6000806040838503121562000214576200021362000071565b5b600083015167ffffffffffffffff81111562000235576200023462000076565b5b6200024385828601620001c7565b925050602083015167ffffffffffffffff81111562000267576200026662000076565b5b6200027585828601620001c7565b9150509250929050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620002d257607f821691505b602082108103620002e857620002e76200028a565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302620003527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000313565b6200035e868362000313565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b6000620003ab620003a56200039f8462000376565b62000380565b62000376565b9050919050565b6000819050919050565b620003c7836200038a565b620003df620003d682620003b2565b84845462000320565b825550505050565b600090565b620003f6620003e7565b62000403818484620003bc565b505050565b5b818110156200042b576200041f600082620003ec565b60018101905062000409565b5050565b601f8211156200047a576200044481620002ee565b6200044f8462000303565b810160208510156200045f578190505b620004776200046e8562000303565b83018262000408565b50505b505050565b600082821c905092915050565b60006200049f600019846008026200047f565b1980831691505092915050565b6000620004ba83836200048c565b9150826002028217905092915050565b620004d5826200027f565b67ffffffffffffffff811115620004f157620004f062000096565b5b620004fd8254620002b9565b6200050a8282856200042f565b600060209050601f8311600181146200054257600084156200052d578287015190505b620005398582620004ac565b865550620005a9565b601f1984166200055286620002ee565b60005b828110156200057c5784890151825560018201915060208501945060208101905062000555565b868310156200059c578489015162000598601f8916826200048c565b8355505b6001600288020188555050505b505050505050565b61122f80620005c16000396000f3fe608060405234801561001057600080fd5b50600436106100a95760003560e01c80633950935111610071578063395093511461016857806370a082311461019857806395d89b41146101c8578063a457c2d7146101e6578063a9059cbb14610216578063dd62ed3e14610246576100a9565b806306fdde03146100ae578063095ea7b3146100cc57806318160ddd146100fc57806323b872dd1461011a578063313ce5671461014a575b600080fd5b6100b6610276565b6040516100c39190610b0c565b60405180910390f35b6100e660048036038101906100e19190610bc7565b610308565b6040516100f39190610c22565b60405180910390f35b61010461032b565b6040516101119190610c4c565b60405180910390f35b610134600480360381019061012f9190610c67565b610335565b6040516101419190610c22565b60405180910390f35b610152610364565b60405161015f9190610cd6565b60405180910390f35b610182600480360381019061017d9190610bc7565b61036d565b60405161018f9190610c22565b60405180910390f35b6101b260048036038101906101ad9190610cf1565b6103a4565b6040516101bf9190610c4c565b60405180910390f35b6101d06103ec565b6040516101dd9190610b0c565b60405180910390f35b61020060048036038101906101fb9190610bc7565b61047e565b60405161020d9190610c22565b60405180910390f35b610230600480360381019061022b9190610bc7565b6104f5565b60405161023d9190610c22565b60405180910390f35b610260600480360381019061025b9190610d1e565b610518565b60405161026d9190610c4c565b60405180910390f35b60606003805461028590610d8d565b80601f01602080910402602001604051908101604052809291908181526020018280546102b190610d8d565b80156102fe5780601f106102d3576101008083540402835291602001916102fe565b820191906000526020600020905b8154815290600101906020018083116102e157829003601f168201915b5050505050905090565b60008061031361059f565b90506103208185856105a7565b600191505092915050565b6000600254905090565b60008061034061059f565b905061034d858285610770565b6103588585856107fc565b60019150509392505050565b60006012905090565b60008061037861059f565b905061039981858561038a8589610518565b6103949190610ded565b6105a7565b600191505092915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b6060600480546103fb90610d8d565b80601f016020809104026020016040519081016040528092919081815260200182805461042790610d8d565b80156104745780601f1061044957610100808354040283529160200191610474565b820191906000526020600020905b81548152906001019060200180831161045757829003601f168201915b5050505050905090565b60008061048961059f565b905060006104978286610518565b9050838110156104dc576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104d390610e93565b60405180910390fd5b6104e982868684036105a7565b60019250505092915050565b60008061050061059f565b905061050d8185856107fc565b600191505092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610616576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161060d90610f25565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610685576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161067c90610fb7565b60405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925836040516107639190610c4c565b60405180910390a3505050565b600061077c8484610518565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81146107f657818110156107e8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107df90611023565b60405180910390fd5b6107f584848484036105a7565b5b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361086b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610862906110b5565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036108da576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016108d190611147565b60405180910390fd5b6108e5838383610a72565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490508181101561096b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610962906111d9565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610a599190610c4c565b60405180910390a3610a6c848484610a77565b50505050565b505050565b505050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610ab6578082015181840152602081019050610a9b565b60008484015250505050565b6000601f19601f8301169050919050565b6000610ade82610a7c565b610ae88185610a87565b9350610af8818560208601610a98565b610b0181610ac2565b840191505092915050565b60006020820190508181036000830152610b268184610ad3565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610b5e82610b33565b9050919050565b610b6e81610b53565b8114610b7957600080fd5b50565b600081359050610b8b81610b65565b92915050565b6000819050919050565b610ba481610b91565b8114610baf57600080fd5b50565b600081359050610bc181610b9b565b92915050565b60008060408385031215610bde57610bdd610b2e565b5b6000610bec85828601610b7c565b9250506020610bfd85828601610bb2565b9150509250929050565b60008115159050919050565b610c1c81610c07565b82525050565b6000602082019050610c376000830184610c13565b92915050565b610c4681610b91565b82525050565b6000602082019050610c616000830184610c3d565b92915050565b600080600060608486031215610c8057610c7f610b2e565b5b6000610c8e86828701610b7c565b9350506020610c9f86828701610b7c565b9250506040610cb086828701610bb2565b9150509250925092565b600060ff82169050919050565b610cd081610cba565b82525050565b6000602082019050610ceb6000830184610cc7565b92915050565b600060208284031215610d0757610d06610b2e565b5b6000610d1584828501610b7c565b91505092915050565b60008060408385031215610d3557610d34610b2e565b5b6000610d4385828601610b7c565b9250506020610d5485828601610b7c565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680610da557607f821691505b602082108103610db857610db7610d5e565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610df882610b91565b9150610e0383610b91565b9250828201905080821115610e1b57610e1a610dbe565b5b92915050565b7f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760008201527f207a65726f000000000000000000000000000000000000000000000000000000602082015250565b6000610e7d602583610a87565b9150610e8882610e21565b604082019050919050565b60006020820190508181036000830152610eac81610e70565b9050919050565b7f45524332303a20617070726f76652066726f6d20746865207a65726f2061646460008201527f7265737300000000000000000000000000000000000000000000000000000000602082015250565b6000610f0f602483610a87565b9150610f1a82610eb3565b604082019050919050565b60006020820190508181036000830152610f3e81610f02565b9050919050565b7f45524332303a20617070726f766520746f20746865207a65726f20616464726560008201527f7373000000000000000000000000000000000000000000000000000000000000602082015250565b6000610fa1602283610a87565b9150610fac82610f45565b604082019050919050565b60006020820190508181036000830152610fd081610f94565b9050919050565b7f45524332303a20696e73756666696369656e7420616c6c6f77616e6365000000600082015250565b600061100d601d83610a87565b915061101882610fd7565b602082019050919050565b6000602082019050818103600083015261103c81611000565b9050919050565b7f45524332303a207472616e736665722066726f6d20746865207a65726f20616460008201527f6472657373000000000000000000000000000000000000000000000000000000602082015250565b600061109f602583610a87565b91506110aa82611043565b604082019050919050565b600060208201905081810360008301526110ce81611092565b9050919050565b7f45524332303a207472616e7366657220746f20746865207a65726f206164647260008201527f6573730000000000000000000000000000000000000000000000000000000000602082015250565b6000611131602383610a87565b915061113c826110d5565b604082019050919050565b6000602082019050818103600083015261116081611124565b9050919050565b7f45524332303a207472616e7366657220616d6f756e742065786365656473206260008201527f616c616e63650000000000000000000000000000000000000000000000000000602082015250565b60006111c3602683610a87565b91506111ce82611167565b604082019050919050565b600060208201905081810360008301526111f2816111b6565b905091905056fea2646970667358221220111835cf5177d466ff04d1477368807f060bf7fab712e923b43a54116fd1f5e764736f6c63430008130033", + "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100a95760003560e01c80633950935111610071578063395093511461016857806370a082311461019857806395d89b41146101c8578063a457c2d7146101e6578063a9059cbb14610216578063dd62ed3e14610246576100a9565b806306fdde03146100ae578063095ea7b3146100cc57806318160ddd146100fc57806323b872dd1461011a578063313ce5671461014a575b600080fd5b6100b6610276565b6040516100c39190610b0c565b60405180910390f35b6100e660048036038101906100e19190610bc7565b610308565b6040516100f39190610c22565b60405180910390f35b61010461032b565b6040516101119190610c4c565b60405180910390f35b610134600480360381019061012f9190610c67565b610335565b6040516101419190610c22565b60405180910390f35b610152610364565b60405161015f9190610cd6565b60405180910390f35b610182600480360381019061017d9190610bc7565b61036d565b60405161018f9190610c22565b60405180910390f35b6101b260048036038101906101ad9190610cf1565b6103a4565b6040516101bf9190610c4c565b60405180910390f35b6101d06103ec565b6040516101dd9190610b0c565b60405180910390f35b61020060048036038101906101fb9190610bc7565b61047e565b60405161020d9190610c22565b60405180910390f35b610230600480360381019061022b9190610bc7565b6104f5565b60405161023d9190610c22565b60405180910390f35b610260600480360381019061025b9190610d1e565b610518565b60405161026d9190610c4c565b60405180910390f35b60606003805461028590610d8d565b80601f01602080910402602001604051908101604052809291908181526020018280546102b190610d8d565b80156102fe5780601f106102d3576101008083540402835291602001916102fe565b820191906000526020600020905b8154815290600101906020018083116102e157829003601f168201915b5050505050905090565b60008061031361059f565b90506103208185856105a7565b600191505092915050565b6000600254905090565b60008061034061059f565b905061034d858285610770565b6103588585856107fc565b60019150509392505050565b60006012905090565b60008061037861059f565b905061039981858561038a8589610518565b6103949190610ded565b6105a7565b600191505092915050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b6060600480546103fb90610d8d565b80601f016020809104026020016040519081016040528092919081815260200182805461042790610d8d565b80156104745780601f1061044957610100808354040283529160200191610474565b820191906000526020600020905b81548152906001019060200180831161045757829003601f168201915b5050505050905090565b60008061048961059f565b905060006104978286610518565b9050838110156104dc576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104d390610e93565b60405180910390fd5b6104e982868684036105a7565b60019250505092915050565b60008061050061059f565b905061050d8185856107fc565b600191505092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610616576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161060d90610f25565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610685576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161067c90610fb7565b60405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925836040516107639190610c4c565b60405180910390a3505050565b600061077c8484610518565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81146107f657818110156107e8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107df90611023565b60405180910390fd5b6107f584848484036105a7565b5b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff160361086b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610862906110b5565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036108da576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016108d190611147565b60405180910390fd5b6108e5838383610a72565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490508181101561096b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610962906111d9565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610a599190610c4c565b60405180910390a3610a6c848484610a77565b50505050565b505050565b505050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610ab6578082015181840152602081019050610a9b565b60008484015250505050565b6000601f19601f8301169050919050565b6000610ade82610a7c565b610ae88185610a87565b9350610af8818560208601610a98565b610b0181610ac2565b840191505092915050565b60006020820190508181036000830152610b268184610ad3565b905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610b5e82610b33565b9050919050565b610b6e81610b53565b8114610b7957600080fd5b50565b600081359050610b8b81610b65565b92915050565b6000819050919050565b610ba481610b91565b8114610baf57600080fd5b50565b600081359050610bc181610b9b565b92915050565b60008060408385031215610bde57610bdd610b2e565b5b6000610bec85828601610b7c565b9250506020610bfd85828601610bb2565b9150509250929050565b60008115159050919050565b610c1c81610c07565b82525050565b6000602082019050610c376000830184610c13565b92915050565b610c4681610b91565b82525050565b6000602082019050610c616000830184610c3d565b92915050565b600080600060608486031215610c8057610c7f610b2e565b5b6000610c8e86828701610b7c565b9350506020610c9f86828701610b7c565b9250506040610cb086828701610bb2565b9150509250925092565b600060ff82169050919050565b610cd081610cba565b82525050565b6000602082019050610ceb6000830184610cc7565b92915050565b600060208284031215610d0757610d06610b2e565b5b6000610d1584828501610b7c565b91505092915050565b60008060408385031215610d3557610d34610b2e565b5b6000610d4385828601610b7c565b9250506020610d5485828601610b7c565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680610da557607f821691505b602082108103610db857610db7610d5e565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610df882610b91565b9150610e0383610b91565b9250828201905080821115610e1b57610e1a610dbe565b5b92915050565b7f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760008201527f207a65726f000000000000000000000000000000000000000000000000000000602082015250565b6000610e7d602583610a87565b9150610e8882610e21565b604082019050919050565b60006020820190508181036000830152610eac81610e70565b9050919050565b7f45524332303a20617070726f76652066726f6d20746865207a65726f2061646460008201527f7265737300000000000000000000000000000000000000000000000000000000602082015250565b6000610f0f602483610a87565b9150610f1a82610eb3565b604082019050919050565b60006020820190508181036000830152610f3e81610f02565b9050919050565b7f45524332303a20617070726f766520746f20746865207a65726f20616464726560008201527f7373000000000000000000000000000000000000000000000000000000000000602082015250565b6000610fa1602283610a87565b9150610fac82610f45565b604082019050919050565b60006020820190508181036000830152610fd081610f94565b9050919050565b7f45524332303a20696e73756666696369656e7420616c6c6f77616e6365000000600082015250565b600061100d601d83610a87565b915061101882610fd7565b602082019050919050565b6000602082019050818103600083015261103c81611000565b9050919050565b7f45524332303a207472616e736665722066726f6d20746865207a65726f20616460008201527f6472657373000000000000000000000000000000000000000000000000000000602082015250565b600061109f602583610a87565b91506110aa82611043565b604082019050919050565b600060208201905081810360008301526110ce81611092565b9050919050565b7f45524332303a207472616e7366657220746f20746865207a65726f206164647260008201527f6573730000000000000000000000000000000000000000000000000000000000602082015250565b6000611131602383610a87565b915061113c826110d5565b604082019050919050565b6000602082019050818103600083015261116081611124565b9050919050565b7f45524332303a207472616e7366657220616d6f756e742065786365656473206260008201527f616c616e63650000000000000000000000000000000000000000000000000000602082015250565b60006111c3602683610a87565b91506111ce82611167565b604082019050919050565b600060208201905081810360008301526111f2816111b6565b905091905056fea2646970667358221220111835cf5177d466ff04d1477368807f060bf7fab712e923b43a54116fd1f5e764736f6c63430008130033", + "linkReferences": {}, + "deployedLinkReferences": {} +} + \ No newline at end of file diff --git a/mobile-app/app/contracts/README.md b/shared/contracts/README.md similarity index 100% rename from mobile-app/app/contracts/README.md rename to shared/contracts/README.md diff --git a/mobile-app/app/contracts/TransferDomainV1.json b/shared/contracts/TransferDomainV1.json similarity index 100% rename from mobile-app/app/contracts/TransferDomainV1.json rename to shared/contracts/TransferDomainV1.json diff --git a/shared/store/evm.ts b/shared/store/evm.ts index e724c53069..6232bb1eaa 100644 --- a/shared/store/evm.ts +++ b/shared/store/evm.ts @@ -1,6 +1,8 @@ -import { SecuredStoreAPI } from "@api"; import { PayloadAction, createAsyncThunk, createSlice } from "@reduxjs/toolkit"; -import { EnvironmentNetwork } from "@waveshq/walletkit-core"; +import { getAddressFromDST20TokenId } from "@screens/AppNavigator/screens/Portfolio/hooks/EvmTokenBalances"; +import { EnvironmentNetwork, isPlayground } from "@waveshq/walletkit-core"; +import { ethers, providers } from "ethers"; +import DST20V1 from "@shared-contracts/DST20V1.json"; interface EvmWalletDetails { hash: string; @@ -42,12 +44,25 @@ export const fetchEvmWalletDetails = createAsyncThunk( async ({ network, evmAddress, + provider, }: { network: EnvironmentNetwork; evmAddress: string; + provider: providers.JsonRpcProvider; }) => { + // If playground then use rpc calls + if (isPlayground(network)) { + const balance = await provider.getBalance(evmAddress); + return { + coin_balance: balance.toString(), + }; + } const url = getBlockscoutUrl(network); const response = await fetch(`${url}/api/v2/addresses/${evmAddress}`); + const data = await response.json(); + if (data.message === "Not found") { + return {}; + } return await response.json(); }, ); @@ -57,15 +72,42 @@ export const fetchEvmTokenBalances = createAsyncThunk( async ({ network, evmAddress, + provider, + tokenIds, }: { network: EnvironmentNetwork; evmAddress: string; + provider: providers.JsonRpcProvider; + tokenIds: string[]; }) => { + // If playground then use rpc calls + if (isPlayground(network)) { + const tokens = tokenIds.map(async (id) => { + const address = getAddressFromDST20TokenId(id); + const contract = new ethers.Contract(address, DST20V1.abi, provider); + const balance = await contract.balanceOf(evmAddress); + const decimals = await contract.decimals(); + return { + token: { + decimals, + address, + }, + value: balance.toString(), + }; + }); + const res = await Promise.all(tokens); + return res.filter((each) => each.value !== "0"); + } + const url = getBlockscoutUrl(network); const response = await fetch( `${url}/api/v2/addresses/${evmAddress}/token-balances`, ); - return await response.json(); + const data = await response.json(); + if (data.message === "Not found") { + return []; + } + return data; }, ); @@ -105,8 +147,7 @@ const getBlockscoutUrl = (network: EnvironmentNetwork) => { } }; -export const getEthRpcUrl = async () => { - const network = await SecuredStoreAPI.getNetwork(); +export const getEthRpcUrl = (network: EnvironmentNetwork) => { // TODO: Add proper ethereum RPC URLs for each network switch (network) { case EnvironmentNetwork.LocalPlayground: diff --git a/tsconfig.json b/tsconfig.json index 959ca614a1..4cbbc4faf1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,6 +25,8 @@ "@shared-api/*": ["../../shared/api/*"], "@shared-contexts": ["../../shared/contexts"], "@shared-contexts/*": ["../../shared/contexts/*"], + "@shared-contracts": ["../../shared/contracts"], + "@shared-contracts/*": ["../../shared/contracts/*"], "@shared-types": ["../../shared/types"], "@shared-types/*": ["../../shared/types/*"], "@screens/*": ["screens/*"], From 0aa72cf5e61db69ae33d92f2bb0c46515f38eb44 Mon Sep 17 00:00:00 2001 From: Pierre Gee Date: Thu, 12 Oct 2023 13:57:34 +0800 Subject: [PATCH 2/2] fix(ui-ux): utxo-evm bug (#4051) * fix(ui-ux): utxo-evm bug * throw error on sending lp to evm address --- .../screens/ConvertConfirmationScreen.tsx | 2 +- .../screens/Portfolio/screens/SendScreen.tsx | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/mobile-app/app/screens/AppNavigator/screens/Portfolio/screens/ConvertConfirmationScreen.tsx b/mobile-app/app/screens/AppNavigator/screens/Portfolio/screens/ConvertConfirmationScreen.tsx index 6b9d7ed544..f1a168083e 100644 --- a/mobile-app/app/screens/AppNavigator/screens/Portfolio/screens/ConvertConfirmationScreen.tsx +++ b/mobile-app/app/screens/AppNavigator/screens/Portfolio/screens/ConvertConfirmationScreen.tsx @@ -239,7 +239,7 @@ export function ConvertConfirmationScreen({ route }: Props): JSX.Element { fee, }), suffix: ` ${sourceToken.displayTextSymbol}${ - convertDirection === ConvertDirection.dvmToEvm ? "" : "-EVM" + convertDirection !== ConvertDirection.evmToDvm ? "" : "-EVM" }`, testID: "resulting_tokens_value", themedProps: { diff --git a/mobile-app/app/screens/AppNavigator/screens/Portfolio/screens/SendScreen.tsx b/mobile-app/app/screens/AppNavigator/screens/Portfolio/screens/SendScreen.tsx index d78d3b90b4..704ff20105 100644 --- a/mobile-app/app/screens/AppNavigator/screens/Portfolio/screens/SendScreen.tsx +++ b/mobile-app/app/screens/AppNavigator/screens/Portfolio/screens/SendScreen.tsx @@ -44,6 +44,10 @@ import { useSafeAreaInsets } from "react-native-safe-area-context"; import { AddressRow } from "@screens/AppNavigator/screens/Portfolio/components/AddressRow"; import { useDomainContext } from "@contexts/DomainContext"; import { ConvertDirection } from "@screens/enum"; +import { + AddressType as AddressCategory, + getAddressType as getAddressCategory, +} from "@waveshq/walletkit-core"; import { useTokenPrice } from "../hooks/TokenPrice"; import { ActiveUSDValueV2 } from "../../Loans/VaultDetail/components/ActiveUSDValueV2"; import { PortfolioParamList } from "../PortfolioNavigator"; @@ -139,6 +143,9 @@ export function SendScreen({ route, navigation }: Props): JSX.Element { let infoText; let themedProps; let status = TransactionCardStatus.Default; + const isEvmAddress = + getAddressCategory(getValues("address"), networkName) === + AddressCategory.ETH; if (new BigNumber(amountToSend).isGreaterThan(token?.amount ?? 0)) { infoText = "Insufficient balance"; @@ -147,6 +154,17 @@ export function SendScreen({ route, navigation }: Props): JSX.Element { light: tailwind("text-red-v2"), }; status = TransactionCardStatus.Error; + } else if ( + isEvmAddress && + (token?.isDAT === false || token?.isLPS === true) + ) { + infoText = + "Transferring non-DAT tokens or LP tokens to an EVM address is not supported"; + themedProps = { + dark: tailwind("text-red-v2"), + light: tailwind("text-red-v2"), + }; + status = TransactionCardStatus.Error; } else if ( token?.isLPS === true && new BigNumber(amountToSend).isGreaterThan(0) @@ -179,7 +197,7 @@ export function SendScreen({ route, navigation }: Props): JSX.Element { style: tailwind("text-xs mt-2 ml-5 font-normal-v2"), }, }; - }, [token, isReservedUtxoUsed, amountToSend]); + }, [token, isReservedUtxoUsed, amountToSend, address]); useEffect(() => { setToken(route.params.token);