From 211d2efb879c1d86c00245239f62fa8d3d5b433b Mon Sep 17 00:00:00 2001 From: modship Date: Thu, 5 Sep 2024 11:13:53 +0200 Subject: [PATCH 1/6] Fix sc address select depends on network --- frontend/.env.example | 4 ++-- frontend/src/const/sc.ts | 4 +++- frontend/src/utils/write-mns-sc.tsx | 22 +++++++++++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/frontend/.env.example b/frontend/.env.example index af0b2ca..f5f1e05 100644 --- a/frontend/.env.example +++ b/frontend/.env.example @@ -1,3 +1,3 @@ -# buildnet config: -VITE_SC_ADDRESS=AS14oUcMujECzburUs8iMZZGsJQQQ61MxPVUKyV8WUNrw3mHmSqF +MAINNET_SC_ADDRESS= +BUILDNET_SC_ADDRESS= VITE_OP_FEES=10000000 # value in nMAS \ No newline at end of file diff --git a/frontend/src/const/sc.ts b/frontend/src/const/sc.ts index cfc1d11..94b5498 100644 --- a/frontend/src/const/sc.ts +++ b/frontend/src/const/sc.ts @@ -1,6 +1,8 @@ import { fromMAS } from '@massalabs/massa-web3'; -export const SC_ADDRESS = import.meta.env.VITE_SC_ADDRESS; +export const MAINNET_SC_ADDRESS = import.meta.env.VITE_MAINNET_SC_ADDRESS; +export const BUILDNET_SC_ADDRESS = import.meta.env.VITE_BUILDNET_SC_ADDRESS; + export const VESTING_SESSION_STORAGE_COST = fromMAS(2); let defaultOpFees: bigint; try { diff --git a/frontend/src/utils/write-mns-sc.tsx b/frontend/src/utils/write-mns-sc.tsx index f81d216..0315186 100644 --- a/frontend/src/utils/write-mns-sc.tsx +++ b/frontend/src/utils/write-mns-sc.tsx @@ -1,5 +1,6 @@ import { Args, + CHAIN_ID, Client, EOperationStatus, ICallData, @@ -10,10 +11,11 @@ import { } from '@massalabs/massa-web3'; import { ToastContent, toast } from '@massalabs/react-ui-kit'; import { useState } from 'react'; -import { DEFAULT_OP_FEES, SC_ADDRESS } from '../const/sc'; +import { DEFAULT_OP_FEES, MAINNET_SC_ADDRESS, BUILDNET_SC_ADDRESS } from '../const/sc'; import { OperationToast } from '../lib/connectMassaWallets/components/OperationToast'; import { logSmartContractEvents } from '../lib/connectMassaWallets/utils'; import { ExplorerApiClient } from '../ExplorerApiClient'; +import { useAccountStore } from '../lib/connectMassaWallets/store'; interface ToasterMessage { pending: string; @@ -67,6 +69,18 @@ type callSmartContractOptions = { function minBigInt(a: bigint, b: bigint) { return a < b ? a : b; } + +function getScAddress(chainId: bigint | undefined) { + switch (chainId) { + case CHAIN_ID.BuildNet: + return BUILDNET_SC_ADDRESS; + case CHAIN_ID.MainNet: + return MAINNET_SC_ADDRESS; + default: + throw new Error('Environnement SC_ADDRESS not found'); + } +} + export function useWriteMNS(client?: Client) { const [isPending, setIsPending] = useState(false); const [isSuccess, setIsSuccess] = useState(false); @@ -75,10 +89,13 @@ export function useWriteMNS(client?: Client) { const [list, setList] = useState([]); const [listSpinning, setListSpinning] = useState(false); const explorerApi = new ExplorerApiClient(); + const { chainId } = useAccountStore(); async function getAllocCost( params: DnsAllocParams, ): Promise { + const SC_ADDRESS = getScAddress(chainId); + let price = 0n; try { let args = new Args(); @@ -182,6 +199,8 @@ export function useWriteMNS(client?: Client) { let operationId: string | undefined; let toastId: string | undefined; + const SC_ADDRESS = getScAddress(chainId); + const callData = { targetAddress: SC_ADDRESS, targetFunction, @@ -314,6 +333,7 @@ export function useWriteMNS(client?: Client) { params: DnsUserEntryListParams, ): Promise { setListSpinning(true); + const SC_ADDRESS = getScAddress(chainId); let resultBalance = await client?.smartContracts().readSmartContract({ targetAddress: SC_ADDRESS, targetFunction: 'balanceOf', From b3a2c375b595ed26740810014007f3c8a4ca255a Mon Sep 17 00:00:00 2001 From: modship Date: Thu, 5 Sep 2024 11:15:26 +0200 Subject: [PATCH 2/6] prettier --- frontend/src/utils/write-mns-sc.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frontend/src/utils/write-mns-sc.tsx b/frontend/src/utils/write-mns-sc.tsx index 0315186..ceda185 100644 --- a/frontend/src/utils/write-mns-sc.tsx +++ b/frontend/src/utils/write-mns-sc.tsx @@ -11,7 +11,11 @@ import { } from '@massalabs/massa-web3'; import { ToastContent, toast } from '@massalabs/react-ui-kit'; import { useState } from 'react'; -import { DEFAULT_OP_FEES, MAINNET_SC_ADDRESS, BUILDNET_SC_ADDRESS } from '../const/sc'; +import { + DEFAULT_OP_FEES, + MAINNET_SC_ADDRESS, + BUILDNET_SC_ADDRESS, +} from '../const/sc'; import { OperationToast } from '../lib/connectMassaWallets/components/OperationToast'; import { logSmartContractEvents } from '../lib/connectMassaWallets/utils'; import { ExplorerApiClient } from '../ExplorerApiClient'; From 31dca406878f4c8398f5bc10d109ecf5c1573954 Mon Sep 17 00:00:00 2001 From: modship Date: Thu, 5 Sep 2024 11:35:55 +0200 Subject: [PATCH 3/6] Fix undefined chainId --- frontend/src/components/MNSManagement.tsx | 7 ++++--- frontend/src/utils/write-mns-sc.tsx | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/MNSManagement.tsx b/frontend/src/components/MNSManagement.tsx index 9039e75..9cae970 100644 --- a/frontend/src/components/MNSManagement.tsx +++ b/frontend/src/components/MNSManagement.tsx @@ -10,7 +10,8 @@ interface MNSManagementProps { export function MNSManagement(props: MNSManagementProps) { const { customClass } = props; - const { massaClient, connectedAccount, currentProvider } = useAccountStore(); + const { massaClient, connectedAccount, currentProvider, chainId } = + useAccountStore(); const { list, listSpinning, @@ -25,9 +26,9 @@ export function MNSManagement(props: MNSManagementProps) { const connected = !!connectedAccount && !!currentProvider; useEffect(() => { - if (!connectedAccount || !massaClient || listSpinning) return; + if (!connectedAccount || !massaClient || listSpinning || !chainId) return; getUserEntryList({ address: connectedAccount.address() }); - }, [connectedAccount, massaClient]); + }, [connectedAccount, massaClient, chainId]); return (
{!connected ? ( diff --git a/frontend/src/utils/write-mns-sc.tsx b/frontend/src/utils/write-mns-sc.tsx index ceda185..4479a8e 100644 --- a/frontend/src/utils/write-mns-sc.tsx +++ b/frontend/src/utils/write-mns-sc.tsx @@ -81,7 +81,7 @@ function getScAddress(chainId: bigint | undefined) { case CHAIN_ID.MainNet: return MAINNET_SC_ADDRESS; default: - throw new Error('Environnement SC_ADDRESS not found'); + throw new Error('SC_ADDRESS not found for chainId : ' + chainId); } } @@ -338,6 +338,8 @@ export function useWriteMNS(client?: Client) { ): Promise { setListSpinning(true); const SC_ADDRESS = getScAddress(chainId); + console.log('SC_ADDRESS', SC_ADDRESS); + let resultBalance = await client?.smartContracts().readSmartContract({ targetAddress: SC_ADDRESS, targetFunction: 'balanceOf', From 58dab55f1bb55538e9e78df15c80fd504ef11db6 Mon Sep 17 00:00:00 2001 From: modship Date: Thu, 5 Sep 2024 11:38:16 +0200 Subject: [PATCH 4/6] remove log --- frontend/src/utils/write-mns-sc.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/src/utils/write-mns-sc.tsx b/frontend/src/utils/write-mns-sc.tsx index 4479a8e..ec69e58 100644 --- a/frontend/src/utils/write-mns-sc.tsx +++ b/frontend/src/utils/write-mns-sc.tsx @@ -338,7 +338,6 @@ export function useWriteMNS(client?: Client) { ): Promise { setListSpinning(true); const SC_ADDRESS = getScAddress(chainId); - console.log('SC_ADDRESS', SC_ADDRESS); let resultBalance = await client?.smartContracts().readSmartContract({ targetAddress: SC_ADDRESS, From 7f91870193dfc76da635f57fa66c9e25c8f5ad1b Mon Sep 17 00:00:00 2001 From: modship Date: Thu, 5 Sep 2024 12:08:08 +0200 Subject: [PATCH 5/6] fix env --- frontend/.env.example | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/.env.example b/frontend/.env.example index f5f1e05..89c1c25 100644 --- a/frontend/.env.example +++ b/frontend/.env.example @@ -1,3 +1,3 @@ -MAINNET_SC_ADDRESS= -BUILDNET_SC_ADDRESS= +VITE_MAINNET_SC_ADDRESS= +VITE_BUILDNET_SC_ADDRESS= VITE_OP_FEES=10000000 # value in nMAS \ No newline at end of file From 4e798d6effae443067e10e3148be5e92ca37ccac Mon Sep 17 00:00:00 2001 From: modship Date: Thu, 5 Sep 2024 14:22:34 +0200 Subject: [PATCH 6/6] add sc address to env example --- frontend/.env.example | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/.env.example b/frontend/.env.example index 89c1c25..26b1fce 100644 --- a/frontend/.env.example +++ b/frontend/.env.example @@ -1,3 +1,3 @@ -VITE_MAINNET_SC_ADDRESS= -VITE_BUILDNET_SC_ADDRESS= +VITE_MAINNET_SC_ADDRESS=AS1q5hUfxLXNXLKsYQVXZLK7MPUZcWaNZZsK7e9QzqhGdAgLpUGT +VITE_BUILDNET_SC_ADDRESS=AS12qKAVjU1nr66JSkQ6N4Lqu4iwuVc6rAbRTrxFoynPrPdP1sj3G VITE_OP_FEES=10000000 # value in nMAS \ No newline at end of file