Skip to content

Commit

Permalink
fix(frontend): update contract modal validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bytegen-dev committed Mar 3, 2025
1 parent accd5e3 commit 08d2f02
Show file tree
Hide file tree
Showing 26 changed files with 607 additions and 3,690 deletions.
407 changes: 352 additions & 55 deletions frontend/openapi-docs.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"@hey-api/client-axios": "^0.6.1",
"@hey-api/client-fetch": "^0.8.1",
"@hey-api/openapi-ts": "^0.64.6",
"@meshsdk/core": "^1.9.0-beta.4",
"@radix-ui/react-dialog": "^1.0.4",
Expand Down
77 changes: 49 additions & 28 deletions frontend/src/components/dashboard/CreateContractModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import { Button } from "@/components/ui/button";
import { useState } from "react";
import { useAppContext } from "@/lib/contexts/AppContext";
import { X } from "lucide-react";
import { createPaymentSource } from "@/lib/api/create-payment-source";
import { getPaymentSources } from "@/lib/api/payment-source";
import { postPaymentSource, getPaymentSource } from "@/lib/api/generated";

type CreateContractModalProps = {
onClose: () => void;
}

type FormData = {
network: 'MAINNET' | 'PREPROD';
paymentType: string;
network: 'Mainnet' | 'Preprod';
paymentType: 'Web3CardanoV1';
blockfrostApiKey: string;
adminWallets: { walletAddress: string }[];
feeReceiverWallet: { walletAddress: string };
Expand All @@ -32,8 +31,8 @@ type FormData = {
}

const initialFormData: FormData = {
network: 'PREPROD',
paymentType: 'WEB3_CARDANO_V1',
network: 'Preprod',
paymentType: 'Web3CardanoV1',
blockfrostApiKey: '',
adminWallets: [{ walletAddress: '' }],
feeReceiverWallet: { walletAddress: '' },
Expand All @@ -44,15 +43,13 @@ const initialFormData: FormData = {
};

export function CreateContractModal({ onClose }: CreateContractModalProps) {
const { state } = useAppContext();

const [formData, setFormData] = useState<FormData>({
...initialFormData,
});

const [error, setError] = useState<string>('');
const [isLoading, setIsLoading] = useState(false);
const { dispatch } = useAppContext();
const { apiClient } = useAppContext();

const handleAdd = async () => {
setError('');
Expand All @@ -79,27 +76,52 @@ export function CreateContractModal({ onClose }: CreateContractModalProps) {
return;
}

const payload = {
network: formData.network,
paymentType: formData.paymentType,
blockfrostApiKey: formData.blockfrostApiKey,
AdminWallets: formData.adminWallets.filter(w => w.walletAddress.trim()),
FeeReceiverNetworkWallet: formData.feeReceiverWallet,
FeePermille: formData.feePermille,
CollectionWallet: formData.collectionWallet,
PurchasingWallets: formData.purchasingWallets.filter(w => w.walletMnemonic.trim()),
SellingWallets: formData.sellingWallets.filter(w => w.walletMnemonic.trim())
};

await createPaymentSource(payload, state.apiKey!);

if (formData.adminWallets.length != 3) {
setError('At least 3 admin wallets are required');
return;
}

await postPaymentSource({
client: apiClient,
body: {
AdminWallets: formData.adminWallets.filter(w => w.walletAddress.trim()).slice(0, 3) as [
{ walletAddress: string },
{ walletAddress: string },
{ walletAddress: string }
],
network: formData.network,
paymentType: formData.paymentType,
feeRatePermille: formData.feePermille,
FeeReceiverNetworkWallet: formData.feeReceiverWallet,
PurchasingWallets: formData.purchasingWallets.filter(w => w.walletMnemonic.trim()).map(w => ({
walletMnemonic: w.walletMnemonic,
collectionAddress: formData.collectionWallet.walletAddress.trim(),
note: w.note || ''
})),
SellingWallets: formData.sellingWallets.filter(w => w.walletMnemonic.trim()).map(w => ({
walletMnemonic: w.walletMnemonic,
collectionAddress: formData.collectionWallet.walletAddress.trim(),
note: w.note || ''
})),
PaymentSourceConfig: {
rpcProviderApiKey: formData.blockfrostApiKey,
rpcProvider: 'Blockfrost'
},
}
});

const sourcesData = await getPaymentSources(state.apiKey!);
//TODO: refetch all
const sourcesData = await getPaymentSource({
client: apiClient,
query: {
cursorId: undefined,
take: 100,
}
});

dispatch({
type: 'SET_PAYMENT_SOURCES',
payload: sourcesData?.data?.paymentSources || []
payload: sourcesData?.data?.data?.paymentSources || []
});

onClose();
Expand Down Expand Up @@ -157,7 +179,7 @@ export function CreateContractModal({ onClose }: CreateContractModalProps) {
<select
className="w-full p-2 rounded-md bg-background border"
value={formData.network}
onChange={(e) => setFormData({ ...formData, network: e.target.value as 'MAINNET' | 'PREPROD' })}
onChange={(e) => setFormData({ ...formData, network: e.target.value as 'Mainnet' | 'Preprod' })}
>
<option value="PREPROD">Preprod</option>
<option value="MAINNET">Mainnet</option>
Expand All @@ -173,8 +195,7 @@ export function CreateContractModal({ onClose }: CreateContractModalProps) {
className="w-full p-2 rounded-md bg-background border"
value={formData.blockfrostApiKey}
onChange={(e) => setFormData({ ...formData, blockfrostApiKey: e.target.value })}
placeholder={`${formData.network} Blockfrost API key`}
required
placeholder="Using default Blockfrost API key"
/>
</div>

Expand Down
18 changes: 9 additions & 9 deletions frontend/src/components/dashboard/RegisterAgentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { useAppContext } from '@/lib/contexts/AppContext';
import { registerAgent } from '@/lib/api/register-agent';
import { toast } from 'react-toastify';
import { shortenAddress } from '@/lib/utils';
import { postRegistry, PostRegistryData } from '@/lib/api/generated';

interface RegisterAgentModalProps {
onClose: () => void;
Expand Down Expand Up @@ -36,7 +36,7 @@ const AGENT_PLACEHOLDERS = {
};

export function RegisterAgentModal({ onClose, onSuccess, paymentContractAddress, sellingWallets, network }: RegisterAgentModalProps) {
const { state } = useAppContext();
const { apiClient } = useAppContext();
const [isLoading, setIsLoading] = useState(false);
const [formData, setFormData] = useState({
name: '',
Expand Down Expand Up @@ -72,12 +72,12 @@ export function RegisterAgentModal({ onClose, onSuccess, paymentContractAddress,
}

try {
const details = {
network,
paymentContractAddress,
const details: PostRegistryData['body'] = {
network: network === 'PREPROD' ? 'Preprod' : 'Mainnet',
smartContractAddress: paymentContractAddress,
tags: formData.tags,
name: formData.name,
api_url: formData.api_url,
apiUrl: formData.api_url,
description: formData.description,
author: {
name: formData.authorName,
Expand All @@ -88,16 +88,16 @@ export function RegisterAgentModal({ onClose, onSuccess, paymentContractAddress,
name: formData.capabilityName,
version: formData.capabilityVersion
},
requests_per_hour: formData.requests_per_hour,
requestsPerHour: formData.requests_per_hour,
pricing: [{
unit: formData.pricingUnit,
quantity: formData.pricingQuantity
}],
legal: {},
sellingWalletVkey: selectedWallet
}
};
console.log(details);
await registerAgent(details, state.apiKey!);
await postRegistry({ client: apiClient, body: details });

onSuccess();
} catch (error) {
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/components/dashboard/RegisteredAgents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Button } from "@/components/ui/button";
import { Pagination } from "../ui/pagination";
import BlinkingUnderscore from '../BlinkingUnderscore';
import { RegisterAgentModal } from './RegisterAgentModal';
import { getRegisteredAgents } from '@/lib/api/get-registered-agents';
import { getRegistry } from '@/lib/api/generated';
import { toast } from 'react-toastify';

interface RegisteredAgentsProps {
Expand All @@ -25,7 +25,7 @@ export function RegisteredAgents({ paymentContractAddress, network, sellingWalle
const [hasMore, setHasMore] = useState(false);
const [cursor, setCursor] = useState<string | undefined>();
const [error, setError] = useState<string | null>(null);
const { state } = useAppContext();
const { state, apiClient } = useAppContext();
const [showRegisterModal, setShowRegisterModal] = useState(false);

const fetchAgents = useCallback(async (nextCursor?: string) => {
Expand Down Expand Up @@ -53,16 +53,16 @@ export function RegisteredAgents({ paymentContractAddress, network, sellingWalle
throw new Error('Selling wallet vkey is required');
}

const response = await getRegisteredAgents(state.apiKey, 10, nextCursor, walletVKey, network, paymentContractAddress);
const response = await getRegistry({ client: apiClient, query: { cursorId: nextCursor, network: network === 'PREPROD' ? 'Preprod' : 'Mainnet', smartContractAddress: paymentContractAddress } });

const agentsFound = response?.data?.assets || [];
const agentsFound = response?.data?.data?.assets || [];
setAgents(prevAgents =>
nextCursor
? [...prevAgents, ...agentsFound]
: agentsFound
);
setHasMore(response?.data?.hasMore || false);
setCursor(response?.data?.nextCursor || undefined);
setHasMore(response?.data?.data?.assets.length === 10);
setCursor(response?.data?.data?.assets[response?.data?.data?.assets.length - 1]?.id || undefined);
} catch (error) {
console.error('Failed to fetch agents:', error);
console.log(walletVKey, network, paymentContractAddress);
Expand Down
12 changes: 4 additions & 8 deletions frontend/src/components/wallet/SwapDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Button } from "@/components/ui/button";
import { getWalletBalance } from "@/lib/api/balance/get";
import swappableTokens from "@/assets/swappableTokens.json";
import { FaExchangeAlt } from "react-icons/fa";
import { getWallet } from "@/lib/api/wallet";
import { getWallet } from "@/lib/api/generated";
import { useAppContext } from "@/lib/contexts/AppContext";
import { toast } from "react-toastify";
import BlinkingUnderscore from "../BlinkingUnderscore";
Expand All @@ -31,7 +31,7 @@ interface SwapDialogProps {
}

export function SwapDialog({ isOpen, onClose, walletAddress, network, blockfrostApiKey, walletType, walletId }: SwapDialogProps) {
const { state } = useAppContext();
const { state, apiClient } = useAppContext();
const [adaBalance, setAdaBalance] = useState<number>(0);
const [usdmBalance, setUsdmBalance] = useState<number>(0);
const [nmkrBalance, setNmkrBalance] = useState<number>(0);
Expand Down Expand Up @@ -141,13 +141,9 @@ export function SwapDialog({ isOpen, onClose, walletAddress, network, blockfrost

const type = walletType?.toLowerCase() === "purchasing" ? "Purchasing" : "Selling";

const response = await getWallet(state?.apiKey || "", {
walletType: type,
id: walletId,
includeSecret: true,
});
const response = await getWallet({ client: apiClient, query: { walletType: type, id: walletId, includeSecret: "true" } });

const fetchedMnemonic = response.data?.Secret?.mnemonic || null;
const fetchedMnemonic = response.data?.data?.Secret?.mnemonic || null;
setMnemonic(fetchedMnemonic);
}
} catch (error: any) {
Expand Down
70 changes: 0 additions & 70 deletions frontend/src/lib/api/api-keys.ts

This file was deleted.

Loading

0 comments on commit 08d2f02

Please sign in to comment.