Skip to content

Commit

Permalink
trying add shop smart contract
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisZerbib committed Jun 4, 2024
1 parent 675b3ac commit 2db3d09
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import PriceConverter from "./components/PriceConverter";
import CurrencySwitcher from "./components/CurrencySwitcher";
import { useCurrency } from "./providers/useCurrency";
// import WalletBalanceTon from "./components/WalletBalanceTon";
// import WalletTxList from "./components/WalletTxList";
import WalletTxList from "./components/WalletTxList";

function App() {
const { network } = useTonConnect();
Expand Down Expand Up @@ -150,6 +150,12 @@ function App() {
</div>
{/* {network && wallet && <WalletBalanceTon walletAddress={wallet} />} */}
{/* {network && wallet && <WalletTxList walletAddress={wallet} />} */}
<Button onClick={() => toggleDrawer(true)}>Orders</Button>
<OrdersDrawer
orders={mockOrders}
open={drawerOpen}
onClose={toggleDrawer}
/>
</div>
</div>
</FlexBoxRowSpaceBetween>
Expand Down
9 changes: 6 additions & 3 deletions src/components/OrderDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "@fortawesome/free-solid-svg-icons";
import { fas } from "@fortawesome/free-solid-svg-icons";
import { useTonConnect } from "../hooks/useTonConnect";

import WalletTxList from "./WalletTxList";
interface OrdersDrawerProps {
orders: OrderProps[];
open: boolean;
Expand Down Expand Up @@ -62,7 +62,9 @@ const OrdersDrawer: React.FC<OrdersDrawerProps> = ({
<Typography variant="h6" gutterBottom sx={{ mb: 2 }}>
Your Orders
</Typography>
<List sx={{ pt: 0 }}>
{/* <List sx={{ pt: 0 }}>
{orders.map((order, index) => (
<ListItem key={index} button sx={{ py: 1, px: 2 }}>
<FontAwesomeIcon
Expand All @@ -71,7 +73,8 @@ const OrdersDrawer: React.FC<OrdersDrawerProps> = ({
<ListItemText primary={`Order ${index + 1}: ${order.status}`} />
</ListItem>
))}
</List>
</List> */}
<WalletTxList walletAddress={wallet!} />
</div>
);

Expand Down
26 changes: 21 additions & 5 deletions src/components/PayWithCrypto.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, toNano } from "ton";
import { Address, Cell, toNano } from "ton";
import { useTonConnect } from "../hooks/useTonConnect";
import { CenterDiv, ButtonBuyTonStyled } from "./styled/styled";
import { useCart } from "../providers/CartProvider";
Expand Down Expand Up @@ -30,6 +30,14 @@ export function BuyWithCrypto({
const tonLogoUrl = "ton.svg";
const usdtLogoUrl = "usdt.svg";

const cart = cartItems.map((item) => {
return {
id: item.id,
price: item.price,
quantity: item.quantity,
};
});

return (
<CenterDiv>
<ButtonBuyTonStyled
Expand All @@ -54,17 +62,25 @@ export function BuyWithCrypto({
onClick();
if (currency === "TON") {
/// TODO If ok then empty the cart and send the payment snackbar
sender.send({
to: Address.parse(tonRecipient),
value: toNano(amount),
});
try {
const amountNano = toNano(amount);
const message = await sender.send({
to: Address.parse(tonRecipient),
value: amountNano,
});
console.log("Transaction sent", message);
} catch (error) {
console.log("Transaction failed", error);
}

// show snackbar

// empty the cart
} else if (currency === "USDT") {
// Handle USDT payment
// Add your logic for USDT payment here
// show snackbar
// empty the cart
}
}}
>
Expand Down
42 changes: 42 additions & 0 deletions src/contracts/Shop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
Contract,
ContractProvider,
Sender,
Address,
Cell,
contractAddress,
beginCell,
toNano,
} from "ton-core";

interface Order {
id: number;
details: string;
status: boolean;
}

export default class ShopContract implements Contract {
async sendMessage(
provider: ContractProvider,
via: Sender,
message: Cell
) {
await provider.internal(via, {
value: toNano("0.05"),
body: message,
});
}

async getWalletAddress(provider: ContractProvider, forAddress: Address) {
const { stack } = await provider.get("get_wallet_address", [
{ type: "slice", cell: beginCell().storeAddress(forAddress).endCell() },
]);

return stack.readAddress().toString();
}

constructor(
readonly address: Address,
readonly init?: { code: Cell; data: Cell }
) { }
}
42 changes: 42 additions & 0 deletions src/hooks/useShopCBDContract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useState } from "react";
import Counter from "../contracts/counter";
import { useTonClient } from "./useTonClient";
import { useAsyncInitialize } from "./useAsyncInitialize";
import { useTonConnect } from "./useTonConnect";
import { Address, OpenedContract } from "ton-core";
import { useQuery } from "@tanstack/react-query";
import { CHAIN } from "@tonconnect/protocol";

export function useCBDShopContract() {
const { client } = useTonClient();
const { sender, network } = useTonConnect();

const counterContract = useAsyncInitialize(async () => {
if (!client) return;
const contract = new Counter(
Address.parse(
network === CHAIN.MAINNET
? "EQBPEDbGdwaLv1DKntg9r6SjFIVplSaSJoJ-TVLe_2rqBOmH"
: "EQBYLTm4nsvoqJRvs_L-IGNKwWs5RKe19HBK_lFadf19FUfb"
) // replace with your address from tutorial 2 step 8
);
return client.open(contract) as OpenedContract<Counter>;
}, [client]);

const { data, isFetching } = useQuery(
["counter"],
async () => {
if (!counterContract) return null;
return (await counterContract!.getCounter()).toString();
},
{ refetchInterval: 3000 }
);

return {
value: isFetching ? null : data,
address: counterContract?.address.toString(),
sendIncrement: () => {
return counterContract?.sendIncrement(sender);
},
};
}
2 changes: 1 addition & 1 deletion src/pages/CheckoutPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function CheckoutPage({ open, onClose }: any) {
).toFixed(2);

useEffect(() => {
setFees(totalPriceFixed * 0.02);
setFees(totalPriceFixed * 0.002);
}, [totalPriceFixed]);

return (
Expand Down

0 comments on commit 2db3d09

Please sign in to comment.