Skip to content

Commit

Permalink
Select SC which depends on network
Browse files Browse the repository at this point in the history
  • Loading branch information
modship committed Sep 5, 2024
1 parent b7231a9 commit a4f8a22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# buildnet config:
VITE_SC_ADDRESS=AS14oUcMujECzburUs8iMZZGsJQQQ61MxPVUKyV8WUNrw3mHmSqF
MAINNET_SC_ADDRESS = AS1q5hUfxLXNXLKsYQVXZLK7MPUZcWaNZZsK7e9QzqhGdAgLpUGT;
BUILDNET_SC_ADDRESS = AS12qKAVjU1nr66JSkQ6N4Lqu4iwuVc6rAbRTrxFoynPrPdP1sj3G;
VITE_OP_FEES=10000000 # value in nMAS
4 changes: 3 additions & 1 deletion frontend/src/const/sc.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
22 changes: 21 additions & 1 deletion frontend/src/utils/write-mns-sc.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Args,
CHAIN_ID,
Client,
EOperationStatus,
ICallData,
Expand All @@ -13,10 +14,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 { stringToBytes } from 'viem';
import { useAccountStore } from '../lib/connectMassaWallets/store';

interface ToasterMessage {
pending: string;
Expand Down Expand Up @@ -70,17 +72,32 @@ 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);
const [isError, setIsError] = useState(false);
const [opId, setOpId] = useState<string | undefined>(undefined);
const [list, setList] = useState<DnsUserEntryListResult[]>([]);
const [listSpinning, setListSpinning] = useState(false);
const { chainId } = useAccountStore();

async function getAllocCost(
params: DnsAllocParams,
): Promise<DnsGetAllocCostResponse> {
const SC_ADDRESS = getScAddress(chainId);

let price = 0n;
try {
let args = new Args();
Expand Down Expand Up @@ -184,6 +201,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,
Expand Down Expand Up @@ -316,6 +335,7 @@ export function useWriteMNS(client?: Client) {
params: DnsUserEntryListParams,
): Promise<DnsUserEntryListResult[]> {
setListSpinning(true);
const SC_ADDRESS = getScAddress(chainId);
let resultBalance = await client?.smartContracts().readSmartContract({
targetAddress: SC_ADDRESS,
targetFunction: 'balanceOf',
Expand Down

0 comments on commit a4f8a22

Please sign in to comment.