Skip to content

Commit

Permalink
Fixed a few bugs in minting function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-astrand committed Sep 23, 2024
1 parent 321d8c6 commit 2ab8847
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
65 changes: 33 additions & 32 deletions frontend/components/common/Minting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,40 @@ export const Minting = ({ chainName }: { chainName: string }) => {
// Add other properties as needed
}

const [config, setConfig] = useState<Config | null>(null);
const [amount, setAmount] = useState<number>(1);
interface Balance {
denom: string;
amount: number;
}

const [balances, setBalances] = useState<Balance[]>([]);
const [price, setPrice] = useState("0");
const [signingClient, setSigningClient] =
useState<SigningCosmWasmClient | null>(null);
const [minting, setMinting] = useState(false);
interface Project {
projectId: number;
name: string;
max_wattpeak: number;
minted_wattpeak_count: number;
}

const { status, address, getSigningCosmWasmClient } =
useChain(chainName);
const [config, setConfig] = useState<Config | null>(null);
const [amount, setAmount] = useState<number>(1);
const [balances, setBalances] = useState<Balance[]>([]);
const [price, setPrice] = useState("0");
const [signingClient, setSigningClient] =
useState<SigningCosmWasmClient | null>(null);
const [minting, setMinting] = useState(false);
const [projects, setProjects] = useState<Project[]>([]);
// State for selected project ID
const [selectedProjectId, setSelectedProjectId] = useState<number | null>(
null
);

const [loading, setLoading] = useState(true);
const [cryptoAmount, setCryptoAmount] = useState<number>(0);
const [junoBalance, setJunoBalance] = useState(0);
const [wattpeakBalance, setWattpeakBalance] = useState(0);
const hasRunQuery = useRef(false);
const [error, setError] = useState<Error | null>(null);
const backgroundColor = useColorModeValue(
"rgba(0, 0, 0, 0.04)",
"rgba(52, 52, 52, 1)"
);

const { connect, status, address, getSigningCosmWasmClient, wallet } =
useChain(chainName);

)
const isMobile = useMediaQuery({ query: "(max-width: 768px)" });

const handleMintClick = async () => {
Expand Down Expand Up @@ -154,6 +149,7 @@ export const Minting = ({ chainName }: { chainName: string }) => {

const handleMaxClick = () => {
const numericAmount = Number(amount);

if (!isNaN(numericAmount)) {
setAmount(Number(numericAmount.toFixed(6)));
} else {
Expand All @@ -164,6 +160,7 @@ export const Minting = ({ chainName }: { chainName: string }) => {
(junoBalance / (config?.minting_price?.amount || 0)).toFixed(6)
)
); // Set as number
setCryptoAmount(junoBalance);
};

const handleBlurAmount = () => {
Expand Down Expand Up @@ -222,34 +219,35 @@ export const Minting = ({ chainName }: { chainName: string }) => {
const fetchData = async () => {
try {
setLoading(true);

// Initiate all fetch operations
const [projectsResult, configResult] = await Promise.all([
queryProjects(),
queryNftConfig(),
]);

// Process projects
const projectsWithId = projectsResult.map((project: any, index: number) => ({
...project,
projectId: index + 1,
}));
const projectsWithId = projectsResult.map(
(project: any, index: number) => ({
...project,
projectId: index + 1,
})
);
setProjects(projectsWithId);

// Set config
setConfig(configResult);

// Set crypto amount based on config
setCryptoAmount(parseFloat(configResult.minting_price.amount));

} catch (err) {
setError(err as Error);
console.error("Error fetching projects or config:", err);
} finally {
setLoading(false);
}
};

fetchData();
}, []);

Expand All @@ -258,17 +256,16 @@ export const Minting = ({ chainName }: { chainName: string }) => {
if (status === "Connected" && address) {
try {
setLoading(true);

// Fetch signing client and balances in parallel
const [client, balancesResult] = await Promise.all([
getSigningCosmWasmClient(),
getBalances(address),
]);

setSigningClient(client as unknown as SigningCosmWasmClient);
setBalances(balancesResult as unknown as Balance[]);
setCorrectBalances(balancesResult);

} catch (err) {
setError(err as Error);
console.error("Error fetching balances:", err);
Expand All @@ -284,11 +281,9 @@ export const Minting = ({ chainName }: { chainName: string }) => {
setLoading(false);
}
};

fetchBalances();
}, [status, address, getSigningCosmWasmClient]);



useEffect(() => {
let payable_amount = ((amount + amount * 0.05) * 5 * 1000000).toString();
Expand Down Expand Up @@ -351,7 +346,11 @@ export const Minting = ({ chainName }: { chainName: string }) => {
? "projectButtonSelected"
: "projectButton"
}
color={inputColor}
color={
selectedProjectId === project.projectId
? "black"
: inputColor
}
borderColor={borderColor}
onClick={() => setSelectedProjectId(project.projectId)}
>
Expand Down Expand Up @@ -382,6 +381,7 @@ export const Minting = ({ chainName }: { chainName: string }) => {
onBlur={handleBlurCryptoAmount}
placeholder="Juno"
min="1"
max={junoBalance}
color={inputColor}
/>
</Box>
Expand All @@ -398,6 +398,7 @@ export const Minting = ({ chainName }: { chainName: string }) => {
onChange={handleAmountChange}
onBlur={handleBlurAmount}
min="1"
max={junoBalance / config?.minting_price.amount}
placeholder="Wattpeak"
color={inputColor}
/>
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/common/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ export const Swap = ({ chainName }: { chainName: string }) => {
width={150}
height={150}
/>
<p>Name: {nft.name}</p>
<p>{nft.name}</p>
</ListItem>
))}
</UnorderedList>
Expand Down Expand Up @@ -381,7 +381,7 @@ export const Swap = ({ chainName }: { chainName: string }) => {
width={150}
height={150}
/>
<p>Name: {nft.name}</p>
<p>{nft.name}</p>
</ListItem>
))}
</UnorderedList>
Expand Down
2 changes: 2 additions & 0 deletions frontend/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ a {
border-radius: 23px;
font-size: 15px;
cursor: pointer;
color: black;
}


.project-details button:hover {
background: linear-gradient(180deg, #FFD602 0%, #FFA231 100%);
color: black;
Expand Down

0 comments on commit 2ab8847

Please sign in to comment.