Skip to content

Commit

Permalink
Simplified process environment handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Oct 9, 2023
1 parent 77d3bab commit b77a3ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
8 changes: 7 additions & 1 deletion examples/wCCD/src/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Network, WithWalletConnector, MAINNET, TESTNET } from '@concordium/reac
import { version } from '../package.json';

import WCCD from './wCCD';
import { WCCD_CONTRACT_INDEX_MAINNET, WCCD_CONTRACT_INDEX_TESTNET } from './constants';

/**
* Connect to wallet, setup application state context, and render children when the wallet API is ready for use.
Expand All @@ -12,19 +13,24 @@ export default function Root() {
const mainnet = 'mainnet';

let NETWORK: Network;
let wCCDContractIndex: bigint;

if (process.env.NETWORK === mainnet) {
NETWORK = MAINNET;
wCCDContractIndex = WCCD_CONTRACT_INDEX_MAINNET;
} else if (process.env.NETWORK === testnet) {
NETWORK = TESTNET;
wCCDContractIndex = WCCD_CONTRACT_INDEX_TESTNET;
} else {
throw Error('Environmental variable NETWORK needs to be defined and set to either "mainnet" or "testnet"');
}

return (
<div>
<main className="wccd">
<WithWalletConnector network={NETWORK}>{(props) => <WCCD {...props} />}</WithWalletConnector>
<WithWalletConnector network={NETWORK}>
{(props) => <WCCD walletConnectionProps={props} wCCDContractIndex={wCCDContractIndex} />}
</WithWalletConnector>
<div>
Version: {version} |{' '}
<a
Expand Down
55 changes: 20 additions & 35 deletions examples/wCCD/src/wCCD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ import {
import * as leb from '@thi.ng/leb128';
import { multiply, round } from 'mathjs';

import {
useGrpcClient,
WalletConnectionProps,
useConnect,
useConnection,
TESTNET,
MAINNET,
} from '@concordium/react-components';
import { useGrpcClient, WalletConnectionProps, useConnect, useConnection } from '@concordium/react-components';
import { wrap, unwrap } from './utils';
import {
CONTRACT_SUB_INDEX,
Expand All @@ -28,8 +21,6 @@ import {
BALANCEOF_FUNCTION_RAW_SCHEMA,
BROWSER_WALLET,
WALLET_CONNECT,
WCCD_CONTRACT_INDEX_TESTNET,
WCCD_CONTRACT_INDEX_MAINNET,
} from './constants';

import ArrowIcon from './assets/Arrow.svg';
Expand Down Expand Up @@ -132,36 +123,30 @@ async function updateWCCDBalanceAccount(rpcClient: ConcordiumGRPCClient, account
return BigInt(leb.decodeULEB128(toBuffer(res.returnValue.slice(4), 'hex'))[0]);
}

export default function wCCD(props: WalletConnectionProps) {
interface ConnectionProps {
walletConnectionProps: WalletConnectionProps;
wCCDContractIndex: bigint;
}

export default function wCCD(props: ConnectionProps) {
const { walletConnectionProps, wCCDContractIndex } = props;

const { network, activeConnectorType, activeConnector, activeConnectorError, connectedAccounts, genesisHashes } =
props;
walletConnectionProps;

const { connection, setConnection, account, genesisHash } = useConnection(connectedAccounts, genesisHashes);
const { connect, isConnecting, connectError } = useConnect(activeConnector, setConnection);

const [admin, setAdmin] = useState<string>();
const [adminError, setAdminError] = useState('');
const [accountExistsOnNetwork, setAccountExistsOnNetwork] = useState(true);

const testnet = 'testnet';
const mainnet = 'mainnet';
let WCCD_CONTRACT_INDEX: bigint;

let grpcClient: ConcordiumGRPCClient | undefined;

if (process.env.NETWORK === mainnet) {
WCCD_CONTRACT_INDEX = WCCD_CONTRACT_INDEX_MAINNET;
grpcClient = useGrpcClient(MAINNET);
} else if (process.env.NETWORK === testnet) {
WCCD_CONTRACT_INDEX = WCCD_CONTRACT_INDEX_TESTNET;
grpcClient = useGrpcClient(TESTNET);
} else {
throw Error('Environmental variable NETWORK needs to be defined and set to either "mainnet" or "testnet"');
}
const grpcClient = useGrpcClient(network);

useEffect(() => {
// Update admin contract.
if (grpcClient && connection) {
viewAdmin(grpcClient, WCCD_CONTRACT_INDEX)
viewAdmin(grpcClient, wCCDContractIndex)
.then((a) => {
setAdmin(a);
setAdminError('');
Expand Down Expand Up @@ -191,7 +176,7 @@ export default function wCCD(props: WalletConnectionProps) {

useEffect(() => {
if (grpcClient && connection && account) {
updateWCCDBalanceAccount(grpcClient, account, WCCD_CONTRACT_INDEX)
updateWCCDBalanceAccount(grpcClient, account, wCCDContractIndex)
.then((a) => {
setAmountAccount(a);
setAmountAccountError('');
Expand Down Expand Up @@ -265,7 +250,7 @@ export default function wCCD(props: WalletConnectionProps) {
connectorName="Browser Wallet"
setWaitingForUser={setWaitingForUser}
connection={connection}
{...props}
{...walletConnectionProps}
/>
<WalletConnectionTypeButton
buttonStyle={ButtonStyle}
Expand All @@ -274,7 +259,7 @@ export default function wCCD(props: WalletConnectionProps) {
connectorName="Wallet Connect"
setWaitingForUser={setWaitingForUser}
connection={connection}
{...props}
{...walletConnectionProps}
/>
</div>
{(!accountExistsOnNetwork ||
Expand Down Expand Up @@ -314,7 +299,7 @@ export default function wCCD(props: WalletConnectionProps) {
onClick={() => {
window.open(
`https://${
process.env.NETWORK === testnet ? `testnet.` : ``
network.name === 'testnet' ? `testnet.` : ``
}ccdscan.io/?dcount=1&dentity=account&daddress=${account}`,
'_blank',
'noopener,noreferrer'
Expand Down Expand Up @@ -424,7 +409,7 @@ export default function wCCD(props: WalletConnectionProps) {
const tx = (isWrapping ? wrap : unwrap)(
connection,
account,
WCCD_CONTRACT_INDEX,
wCCDContractIndex,
CONTRACT_SUB_INDEX,
amount,
receiver
Expand Down Expand Up @@ -455,7 +440,7 @@ export default function wCCD(props: WalletConnectionProps) {
onClick={() => {
window.open(
`https://${
process.env.NETWORK === testnet ? `testnet.` : ``
network.name === 'testnet' ? `testnet.` : ``
}ccdscan.io/?dcount=1&dentity=transaction&dhash=${hash}`,
'_blank',
'noopener,noreferrer'
Expand All @@ -480,7 +465,7 @@ export default function wCCD(props: WalletConnectionProps) {
onClick={() => {
window.open(
`https://${
process.env.NETWORK === testnet ? `testnet.` : ``
network.name === 'testnet' ? `testnet.` : ``
}ccdscan.io/?dcount=1&dentity=account&daddress=${admin}`,
'_blank',
'noopener,noreferrer'
Expand Down

0 comments on commit b77a3ba

Please sign in to comment.