From c389dc57a4539b28dce2bc4372c9cbfef0f85705 Mon Sep 17 00:00:00 2001 From: Spongebob-MrDapps Date: Fri, 27 Sep 2024 20:01:53 +0530 Subject: [PATCH] rns functions --- lib/js/packages/reef-mobile-js/dist/index.js | 54 ++++++++++++++- .../src/jsApi/abi/ReefNameService.ts | 7 ++ .../reef-mobile-js/src/jsApi/accountApi.ts | 67 +++++++++++++------ lib/model/account/AccountCtrl.dart | 10 +++ 4 files changed, 118 insertions(+), 20 deletions(-) create mode 100644 lib/js/packages/reef-mobile-js/src/jsApi/abi/ReefNameService.ts diff --git a/lib/js/packages/reef-mobile-js/dist/index.js b/lib/js/packages/reef-mobile-js/dist/index.js index 5175935e..d8d9a2cf 100644 --- a/lib/js/packages/reef-mobile-js/dist/index.js +++ b/lib/js/packages/reef-mobile-js/dist/index.js @@ -172993,6 +172993,30 @@ ${bz(e, r10)}`); } catch (error) { } + // src/jsApi/abi/ReefNameService.ts + var RnsAddress = "0x2B679Ea6B05da053D698b0d2c6daF1014Db7281e"; + var ReefNameService = [ + { "type": "constructor", "inputs": [], "stateMutability": "nonpayable" }, + { "name": "RnsCreated", "type": "event", "inputs": [{ "name": "user", "type": "address", "indexed": true, "internalType": "address" }, { "name": "rns", "type": "string", "indexed": false, "internalType": "string" }], "anonymous": false }, + { "name": "allUsers", "type": "function", "inputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, + { "name": "blacklist", "type": "function", "inputs": [{ "name": "", "type": "string", "internalType": "string" }], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" }, + { "name": "blacklistAddress", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }, { "name": "_action", "type": "bool", "internalType": "bool" }], "outputs": [], "stateMutability": "nonpayable" }, + { "name": "buy", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [], "stateMutability": "payable" }, + { "name": "create", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [], "stateMutability": "payable" }, + { "name": "getPrice", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, + { "name": "migrate", "type": "function", "inputs": [{ "name": "_user", "type": "address", "internalType": "address" }, { "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [], "stateMutability": "nonpayable" }, + { "name": "owner", "type": "function", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, + { "name": "rnsPrices", "type": "function", "inputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, + { "name": "rnsToSell", "type": "function", "inputs": [{ "name": "", "type": "string", "internalType": "string" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, + { "name": "rnsToUser", "type": "function", "inputs": [{ "name": "", "type": "string", "internalType": "string" }], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, + { "name": "sell", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }, { "name": "_price", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" }, + { "name": "sendReef", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [], "stateMutability": "payable" }, + { "name": "setRnsPrices", "type": "function", "inputs": [{ "name": "_rnsPrices", "type": "uint256[]", "internalType": "uint256[]" }], "outputs": [], "stateMutability": "nonpayable" }, + { "name": "userToRns", "type": "function", "inputs": [{ "name": "", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "string", "internalType": "string" }], "stateMutability": "view" }, + { "name": "usersCount", "type": "function", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, + { "name": "withdraw", "type": "function", "inputs": [], "outputs": [], "stateMutability": "payable" } + ]; + // src/jsApi/accountApi.ts var { REEF_EXTENSION_IDENT } = V7e; var buildAccountWithMeta = async (name6, address) => { @@ -173053,6 +173077,34 @@ ${bz(e, r10)}`); take(1) )); }, + fetchRns: async (evmAddress) => { + const provider = await firstValueFrom(z7e.selectedProvider$); + const rnsContract = new Contract(RnsAddress, ReefNameService, provider); + try { + const rns = await rnsContract.userToRns(evmAddress); + return rns; + } catch (error) { + console.log({ + name: "fetchRns", + error: error.message + }); + return ""; + } + }, + fetchEvmAddressFromRns: async (reefName) => { + const provider = await firstValueFrom(z7e.selectedProvider$); + const rnsContract = new Contract(RnsAddress, ReefNameService, provider); + try { + const addr = await rnsContract.rnsToUser(reefName); + return addr; + } catch (error) { + console.log({ + name: "fetchEvmAddressFromRns", + error: error.message + }); + return ""; + } + }, toReefEVMAddressWithNotification: (evmAddress) => { return q7e.addReefSpecificStringFromAddress(evmAddress); }, @@ -177717,7 +177769,7 @@ query FETCH_TX_INFO { return { query: ` query TokenQuery { - tokens(where: {id_eq: "${tokenAddr}"}) { + tokens(where: {id_containsInsensitive: "${tokenAddr}"}, limit: 1) { decimals iconUrl id diff --git a/lib/js/packages/reef-mobile-js/src/jsApi/abi/ReefNameService.ts b/lib/js/packages/reef-mobile-js/src/jsApi/abi/ReefNameService.ts new file mode 100644 index 00000000..dddc3605 --- /dev/null +++ b/lib/js/packages/reef-mobile-js/src/jsApi/abi/ReefNameService.ts @@ -0,0 +1,7 @@ +import { ContractInterface } from 'ethers'; + +export const RnsAddress = "0x2B679Ea6B05da053D698b0d2c6daF1014Db7281e" + +export const ReefNameService: ContractInterface = [ + { "type": "constructor", "inputs": [], "stateMutability": "nonpayable" }, { "name": "RnsCreated", "type": "event", "inputs": [{ "name": "user", "type": "address", "indexed": true, "internalType": "address" }, { "name": "rns", "type": "string", "indexed": false, "internalType": "string" }], "anonymous": false }, { "name": "allUsers", "type": "function", "inputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, { "name": "blacklist", "type": "function", "inputs": [{ "name": "", "type": "string", "internalType": "string" }], "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], "stateMutability": "view" }, { "name": "blacklistAddress", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }, { "name": "_action", "type": "bool", "internalType": "bool" }], "outputs": [], "stateMutability": "nonpayable" }, { "name": "buy", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [], "stateMutability": "payable" }, { "name": "create", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [], "stateMutability": "payable" }, { "name": "getPrice", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "name": "migrate", "type": "function", "inputs": [{ "name": "_user", "type": "address", "internalType": "address" }, { "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [], "stateMutability": "nonpayable" }, { "name": "owner", "type": "function", "inputs": [], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, { "name": "rnsPrices", "type": "function", "inputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "name": "rnsToSell", "type": "function", "inputs": [{ "name": "", "type": "string", "internalType": "string" }], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "name": "rnsToUser", "type": "function", "inputs": [{ "name": "", "type": "string", "internalType": "string" }], "outputs": [{ "name": "", "type": "address", "internalType": "address" }], "stateMutability": "view" }, { "name": "sell", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }, { "name": "_price", "type": "uint256", "internalType": "uint256" }], "outputs": [], "stateMutability": "nonpayable" }, { "name": "sendReef", "type": "function", "inputs": [{ "name": "_rns", "type": "string", "internalType": "string" }], "outputs": [], "stateMutability": "payable" }, { "name": "setRnsPrices", "type": "function", "inputs": [{ "name": "_rnsPrices", "type": "uint256[]", "internalType": "uint256[]" }], "outputs": [], "stateMutability": "nonpayable" }, { "name": "userToRns", "type": "function", "inputs": [{ "name": "", "type": "address", "internalType": "address" }], "outputs": [{ "name": "", "type": "string", "internalType": "string" }], "stateMutability": "view" }, { "name": "usersCount", "type": "function", "inputs": [], "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], "stateMutability": "view" }, { "name": "withdraw", "type": "function", "inputs": [], "outputs": [], "stateMutability": "payable" } +] \ No newline at end of file diff --git a/lib/js/packages/reef-mobile-js/src/jsApi/accountApi.ts b/lib/js/packages/reef-mobile-js/src/jsApi/accountApi.ts index a6dea789..14463667 100644 --- a/lib/js/packages/reef-mobile-js/src/jsApi/accountApi.ts +++ b/lib/js/packages/reef-mobile-js/src/jsApi/accountApi.ts @@ -1,14 +1,15 @@ -import {AddressName, getAccountSigner, ReefAccount, reefState, addressUtils, network} from '@reef-chain/util-lib'; -import {combineLatest, map, switchMap, take} from "rxjs/operators"; -import {firstValueFrom} from 'rxjs'; +import { AddressName, getAccountSigner, ReefAccount, reefState, addressUtils, network } from '@reef-chain/util-lib'; +import { combineLatest, map, switchMap, take } from "rxjs/operators"; +import { firstValueFrom } from 'rxjs'; import { extension } from '@reef-chain/util-lib'; import { resolveEvmAddress as utilsResolveEvmAddr, resolveAddress as utilsResolveToNativeAddress, isSubstrateAddress } from "@reef-chain/evm-provider/utils"; -import {Provider} from "@reef-chain/evm-provider"; +import { Provider } from "@reef-chain/evm-provider"; import Signer from "./background/Signer"; -import {ethers} from 'ethers'; +import { Contract, ethers } from 'ethers'; import type { InjectedAccountWithMeta } from '@reef-chain/util-lib/dist/dts/extension'; +import { ReefNameService, RnsAddress } from './abi/ReefNameService'; -const {REEF_EXTENSION_IDENT} = extension; +const { REEF_EXTENSION_IDENT } = extension; export const buildAccountWithMeta = async (name: string, address: string): Promise => { const acountWithMeta: InjectedAccountWithMeta = { @@ -49,15 +50,15 @@ export const innitApi = (signingKey: Signer) => { accounts.map(async (account: AddressName) => { return await buildAccountWithMeta(account.name, account.address); } - )); - console.log("updateAccounts=",accountsWithMeta); + )); + console.log("updateAccounts=", accountsWithMeta); reefState.setAccounts(accountsWithMeta); }, claimEvmAccount: async (nativeAddress: string) => { return firstValueFrom(reefState.accounts$.pipe( take(1), map((accounts: ReefAccount[]) => { - return accounts.find((s)=> s.address === nativeAddress); + return accounts.find((s) => s.address === nativeAddress); }), combineLatest([reefState.selectedProvider$]), switchMap(async ([signer, provider]: [ReefAccount | undefined, Provider]) => { @@ -79,7 +80,7 @@ export const innitApi = (signingKey: Signer) => { address, type: reefState.UpdateDataType.ACCOUNT_NATIVE_BALANCE, } as reefState.UpdateAction); - reefState.onTxUpdateResetSigners({isInBlock: true, txTypeEvm: false}, updateActions); + reefState.onTxUpdateResetSigners({ isInBlock: true, txTypeEvm: false }, updateActions); return true; } catch (e) { console.log('account.claimEvmAccount() - ', e.message); @@ -89,32 +90,60 @@ export const innitApi = (signingKey: Signer) => { take(1) )); }, - toReefEVMAddressWithNotification: (evmAddress: string)=>{ + fetchRns: async (evmAddress: string) => { + const provider = await firstValueFrom(reefState.selectedProvider$); + const rnsContract = new Contract(RnsAddress, ReefNameService, provider); + try { + const rns = await rnsContract.userToRns(evmAddress); + return rns; + } catch (error) { + console.log({ + name: "fetchRns", + error: error.message + }); + return "" + } + }, + fetchEvmAddressFromRns: async (reefName: string) => { + const provider = await firstValueFrom(reefState.selectedProvider$); + const rnsContract = new Contract(RnsAddress, ReefNameService, provider); + try { + const addr = await rnsContract.rnsToUser(reefName); + return addr; + } catch (error) { + console.log({ + name: "fetchEvmAddressFromRns", + error: error.message + }); + return "" + } + }, + toReefEVMAddressWithNotification: (evmAddress: string) => { return addressUtils.addReefSpecificStringFromAddress(evmAddress); }, - toReefEVMAddressNoNotification: (evmAddressMsg: string)=>{ + toReefEVMAddressNoNotification: (evmAddressMsg: string) => { return addressUtils.removeReefSpecificStringFromAddress(evmAddressMsg); }, - resolveEvmAddress:async(nativeAddress:string)=>{ + resolveEvmAddress: async (nativeAddress: string) => { const provider = await firstValueFrom(reefState.selectedProvider$); - return utilsResolveEvmAddr(provider,nativeAddress); + return utilsResolveEvmAddr(provider, nativeAddress); }, - resolveFromEvmAddress:async(evmAddress:string)=>{ + resolveFromEvmAddress: async (evmAddress: string) => { const provider = await firstValueFrom(reefState.selectedProvider$); - const nativeAddress=await utilsResolveToNativeAddress(provider,evmAddress); - return nativeAddress||null; + const nativeAddress = await utilsResolveToNativeAddress(provider, evmAddress); + return nativeAddress || null; }, - isValidEvmAddress: (address: string) => ethers.utils.isAddress(address), + isValidEvmAddress: (address: string) => ethers.utils.isAddress(address), isValidSubstrateAddress: (address: string) => isSubstrateAddress(address), listenBindActivity: (address: string) => { return network.getLatestBlockAccountUpdates$([address]) - } + } }; } diff --git a/lib/model/account/AccountCtrl.dart b/lib/model/account/AccountCtrl.dart index 41d5a381..88bcca13 100644 --- a/lib/model/account/AccountCtrl.dart +++ b/lib/model/account/AccountCtrl.dart @@ -159,6 +159,16 @@ Future listenBindActivity(String address) async { .jsPromise('window.account.resolveFromEvmAddress("$evmAddress")'); } + Future resolveToRns(String evmAddress) async { + return await _jsApi + .jsPromise('window.account.fetchRns("$evmAddress")'); + } + + Future resolveEvmFromRns(String rns) async { + return await _jsApi + .jsPromise('window.account.fetchEvmAddressFromRns("$rns")'); + } + Future sanitizeEvmAddress(String evmAddress) async{ return await _jsApi.jsPromise('window.utils.sanitizeInput("$evmAddress")'); }