Skip to content

Profiles Demo: refactor data validation and adjust USDC amount #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion smart-wallet/profiles-demo/src/app/api/data-validation/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// app/api/data-validation/route.ts
import { encodeFunctionData, erc20Abi, numberToHex, parseUnits } from "viem";

export async function POST(request: Request) {
// Get the data from the request body
const requestData = await request.json();
Expand All @@ -23,11 +25,17 @@ export async function POST(request: Request) {
(physicalAddress.postalCode.length < 5 ||
physicalAddress.postalCode.length > 10)
) {
if (!errors.physicalAddress) {
errors.physicalAddress = {};
}
errors.physicalAddress.postalCode = "Invalid postal code format";
}

// Check country validation - for example, only allow certain countries
if (physicalAddress.countryCode && physicalAddress.countryCode === "XY") {
if (!errors.physicalAddress) {
errors.physicalAddress = {};
}
errors.physicalAddress.countryCode = "We don't ship to this country";
}

Expand All @@ -36,6 +44,9 @@ export async function POST(request: Request) {
physicalAddress.city &&
physicalAddress.city.toLowerCase() === "restricted"
) {
if (!errors.physicalAddress) {
errors.physicalAddress = {};
}
errors.physicalAddress.city = "We don't ship to this city";
}
}
Expand All @@ -45,10 +56,25 @@ export async function POST(request: Request) {
return Response.json({ errors });
}

// change the calls to the actual calls
const calls = [
{
to: "0x036CbD53842c5426634e7929541eC2318f3dCF7e", // USDC contract address on Base Sepolia
data: encodeFunctionData({
abi: erc20Abi,
functionName: "transfer",
args: [
"0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
parseUnits("0.02", 6),
],
}),
}
]

// If all validations pass, return success
return Response.json({
request: {
calls: requestData.calls,
calls: calls,
chainId: requestData.chainId,
capabilities: requestData.capabilities
}
Expand Down
22 changes: 19 additions & 3 deletions smart-wallet/profiles-demo/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useEffect, useState } from "react";
import { encodeFunctionData, erc20Abi, numberToHex, parseUnits } from "viem";
import { useConnect, useSendCalls } from "wagmi";
import { useConnect, useSendCalls, useCallsStatus } from "wagmi";

interface DataRequest {
email: boolean;
Expand All @@ -25,6 +25,19 @@ export default function Home() {

const { sendCalls, data, error, isPending } = useSendCalls();
const { connect, connectors } = useConnect()
const [id, setId] = useState<string>("");
const statusResult = useCallsStatus({
id,
query: {
enabled: id !== "",
refetchInterval: (data) => {
if (data?.state.data?.statusCode === 200) {
return false;
}
return 500;
},
},
});


// Function to get callback URL - replace in production
Expand All @@ -43,7 +56,7 @@ export default function Home() {

// Extract address if provided
if (callbackData.physicalAddress) {
const addr = callbackData.physicalAddress.physicalAddress;
const addr = callbackData.physicalAddress;
newResult.address = [
addr.address1,
addr.address2,
Expand All @@ -55,6 +68,7 @@ export default function Home() {
}

setResult(newResult);
setId(data.callsId);
} else if (data && !data.capabilities?.dataCallback) {
setResult({ success: false, error: "Invalid response - no data callback" });
}
Expand Down Expand Up @@ -96,7 +110,7 @@ export default function Home() {
abi: erc20Abi,
functionName: "transfer",
args: [
"0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
"0x036CbD53842c5426634e7929541eC2318f3dCF7e",
parseUnits("0.01", 6),
],
}),
Expand All @@ -118,6 +132,8 @@ export default function Home() {
}
}

console.log("statusResult", statusResult);

return (
<div style={{ maxWidth: "600px", margin: "0 auto", padding: "20px" }}>
<h1>Profiles Demo</h1>
Expand Down
Loading