Skip to content

Commit

Permalink
feat: add check balance before claim
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonatan-Chaverri committed Jan 22, 2025
1 parent 846e5f3 commit 64bf7ed
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 45 deletions.
4 changes: 2 additions & 2 deletions apps/snfoundry/deployments/sepolia_latest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"CofiCollection": {
"classHash": "0x7a108607024be2054d1558cbfd5de3fe264afe9e025bc7653b1bc5fc76d1af0",
"address": "0x2beb2106100c5238830e2d0e23193f581fd69e53b9c5d0f7eabf11c7d85db14",
"address": "0x1b0409734a9924cf829943a4d65925957d34f041af627a1d8a6d5be69ab532d",
"contract": "cofi_collection.cairo"
},
"Marketplace": {
"classHash": "0x44eb6074fa74a9dbddb65c52394cd1bab274e3bc22e1fc36c26c37686d1e440",
"address": "0x3b73e235a852f76cb01e014246c59db4623d7ecf5c68675f07df0e209cd0303",
"address": "0xdda31f5e1f8316230739226265d62c2d869ad989ad0e51be592dce30776918",
"contract": "marketplace.cairo"
}
}
9 changes: 7 additions & 2 deletions apps/snfoundry/scripts-ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
executeDeployCalls,
exportDeployments,
} from "./deploy-contract";
import { green } from "./helpers/colorize-log";
import { green, yellow } from "./helpers/colorize-log";

/**
* Deploy a contract using the specified parameters.
Expand Down Expand Up @@ -77,7 +77,7 @@ const deployScript = async (): Promise<void> => {
constructorArgs: {
cofi_collection_address: cofiCollectionAddress,
admin: deployer.address,
market_fee: BigInt(300000),
market_fee: BigInt(250), // 2.5 %
},
});

Expand Down Expand Up @@ -107,6 +107,11 @@ const deployScript = async (): Promise<void> => {
];
const { transaction_hash } = await deployer.execute(transactions);
console.log("🚀 Final transactions hash", transaction_hash);
console.log(
yellow(
"Make sure to update the contracts metadata in web app!! See README.md",
),
);
};

deployScript()
Expand Down
2 changes: 1 addition & 1 deletion apps/snfoundry/scripts-ts/helpers/deploy-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function main() {
// Execute the deploy script without the reset option
try {
execSync(
`cd contracts && scarb build && ts-node ../scripts-ts/deploy.ts --network ${argv.network || "devnet"} --fee ${argv.fee || "eth"} ${!argv.reset && "--no-reset "} && ts-node ../scripts-ts/helpers/parse-deployments.ts && cd·..`,
`cd contracts && scarb build && ts-node ../scripts-ts/deploy.ts --network ${argv.network || "devnet"} --fee ${argv.fee || "eth"} ${!argv.reset && "--no-reset "} && ts-node ../scripts-ts/helpers/parse-deployments.ts && cd ..`,
{ stdio: "inherit" },
);
} catch (error) {
Expand Down
65 changes: 35 additions & 30 deletions apps/web/src/app/user/my-claims/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default function MyClaims() {
const [ClaimedOrders, setClaimedOrders] = useState(mockedOrders);
const [searchTerm, setSearchTerm] = useState("");
const [MoneyToClaim, setMoneyToClaim] = useState(0);
const [checked, setChecked] = useState(false);
const [, setIsFiltersModalOpen] = useState(false);
const { t } = useTranslation();

Expand All @@ -88,42 +89,44 @@ export default function MyClaims() {
);

useEffect(() => {
const unclaimedOrders = mockedOrders
.map((orderGroup) => ({
...orderGroup,
items: orderGroup.items.filter((item) => !item.claimed),
}))
.filter((orderGroup) => orderGroup.items.length > 0);

setOrdersToClaim(unclaimedOrders);

const totalMoneyToClaim = unclaimedOrders.reduce((total, orderGroup) => {
return (
total +
orderGroup.items.reduce(
(groupTotal, item) => groupTotal + item.price,
0,
)
);
}, 0);

setMoneyToClaim(totalMoneyToClaim);

const claimedOrders = mockedOrders
.map((orderGroup) => ({
...orderGroup,
items: orderGroup.items.filter((item) => item.claimed),
}))
.filter((orderGroup) => orderGroup.items.length > 0);

setClaimedOrders(claimedOrders);
const fetchData = () => {
const unclaimedOrders = mockedOrders
.map((orderGroup) => ({
...orderGroup,
items: orderGroup.items.filter((item) => !item.claimed),
}))
.filter((orderGroup) => orderGroup.items.length > 0);

setOrdersToClaim(unclaimedOrders);

const totalMoneyToClaim = 0;

setMoneyToClaim(totalMoneyToClaim);

const claimedOrders = mockedOrders
.map((orderGroup) => ({
...orderGroup,
items: orderGroup.items.filter((item) => item.claimed),
}))
.filter((orderGroup) => orderGroup.items.length > 0);

setClaimedOrders(claimedOrders);
};

fetchData();
}, []);

const openFiltersModal = () => {
setIsFiltersModalOpen(true);
};

const handleClaim = async () => {
if (!checked) {
const total = await contracts.get_claim_balance();
setMoneyToClaim(Number(total));
setChecked(true);
return;
}
console.log("claiming");
const tx = await contracts.claim();
alert(`Claimed success with tx: ${tx}`);
Expand Down Expand Up @@ -181,7 +184,9 @@ export default function MyClaims() {
className="mx-auto mt-5 w-[90%] h-15 px-2"
onClick={() => handleClaim()}
>
{t("recieve")} {MoneyToClaim.toFixed(2)} USD
{!checked
? "Check balance"
: `${t("recieve")} ${MoneyToClaim.toFixed(2)} USD`}
</Button>
</div>
<div className="mb-6">
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/user/register-coffee/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default function RegisterCoffee() {
name: submissionData.variety,
price: submissionData.price,
description: submissionData.description,
image: submissionData.image ?? "",
image: submissionData.image ?? "/images/cafe1.webp",
strength: submissionData.roast,
region: "",
farmName: "",
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/contracts/configExternalContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const configExternalContracts = {
sepolia: {
Marketplace: {
address:
"0x3b73e235a852f76cb01e014246c59db4623d7ecf5c68675f07df0e209cd0303",
"0xdda31f5e1f8316230739226265d62c2d869ad989ad0e51be592dce30776918",
classHash:
"0x44eb6074fa74a9dbddb65c52394cd1bab274e3bc22e1fc36c26c37686d1e440",
abi: [
Expand Down Expand Up @@ -587,7 +587,7 @@ const configExternalContracts = {
},
CofiCollection: {
address:
"0x2beb2106100c5238830e2d0e23193f581fd69e53b9c5d0f7eabf11c7d85db14",
"0x1b0409734a9924cf829943a4d65925957d34f041af627a1d8a6d5be69ab532d",
classHash:
"0x7a108607024be2054d1558cbfd5de3fe264afe9e025bc7653b1bc5fc76d1af0",
abi: [
Expand Down
8 changes: 1 addition & 7 deletions apps/web/src/server/api/routers/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,7 @@ export const productRouter = createTRPCRouter({
name: z.string().min(1),
price: z.number().min(0),
description: z.string().min(1),
image: z
.string()
.optional()
.refine(
(val) => val === "" || z.string().url().safeParse(val).success,
{ message: "Invalid URL" },
),
image: z.string().optional(),
strength: z.string().min(1),
region: z.string().optional(),
farmName: z.string().optional(),
Expand Down
21 changes: 21 additions & 0 deletions apps/web/src/services/contractsInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,26 @@ class ContractsInterface {
"balanceOf",
CallData.compile([address, token_id, "0x0"]),
);

return balance;
}

async get_claim_balance() {
const address = this.get_user_address();
if (!this.marketplaceContract) {
throw new Error("Cofi collection contract is not loaded");
}
const balance_result = await this.marketplaceContract.call(
"claim_balance",
CallData.compile([address]),
);
const balance = BigInt(Number(balance_result));

const stark_price_usd = await this.getStarkPrice();
const total = (Number(balance) / 1000000000000000000) * stark_price_usd;
return total;
}

async register_product(price_usd: number, initial_stock: number) {
// connect user account to contracts
this.connect_account();
Expand Down Expand Up @@ -208,6 +225,10 @@ class ContractsInterface {
"0x0",
]),
);
// wait for allowance to complete to avoid errors
await this.provider.waitForTransaction(tx.transaction_hash, {
retryInterval: 100,
});
return tx.transaction_hash;
}

Expand Down

0 comments on commit 64bf7ed

Please sign in to comment.