Skip to content

Commit 836415b

Browse files
committed
fix
1 parent f278bc7 commit 836415b

File tree

6 files changed

+113
-34
lines changed

6 files changed

+113
-34
lines changed

examples/testapp/src/pages/pay-playground/components/CodeEditor.module.css

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,52 @@
7272
user-select: none;
7373
}
7474

75+
.inputContainer {
76+
padding: 1rem 1.5rem;
77+
border-bottom: 1px solid #e2e8f0;
78+
background: #f8fafc;
79+
}
80+
81+
.inputLabel {
82+
display: flex;
83+
flex-direction: column;
84+
gap: 0.5rem;
85+
font-size: 0.875rem;
86+
color: #475569;
87+
}
88+
89+
.inputLabelText {
90+
font-weight: 500;
91+
}
92+
93+
.textInput {
94+
width: 100%;
95+
padding: 0.625rem 0.875rem;
96+
font-size: 0.875rem;
97+
font-family: "SF Mono", Monaco, "Cascadia Code", "Roboto Mono", Consolas, "Courier New", monospace;
98+
color: #0f172a;
99+
background: white;
100+
border: 1px solid #cbd5e1;
101+
border-radius: 6px;
102+
outline: none;
103+
transition: all 0.2s;
104+
}
105+
106+
.textInput:focus {
107+
border-color: #0052ff;
108+
box-shadow: 0 0 0 3px rgba(0, 82, 255, 0.1);
109+
}
110+
111+
.textInput:disabled {
112+
opacity: 0.5;
113+
cursor: not-allowed;
114+
background: #f1f5f9;
115+
}
116+
117+
.textInput::placeholder {
118+
color: #94a3b8;
119+
}
120+
75121
.editorWrapper {
76122
position: relative;
77123
height: 390px;

examples/testapp/src/pages/pay-playground/components/CodeEditor.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ interface CodeEditorProps {
99
includePayerInfo: boolean;
1010
onPayerInfoToggle: (checked: boolean) => void;
1111
showPayerInfoToggle?: boolean;
12+
paymasterUrl?: string;
13+
onPaymasterUrlChange?: (url: string) => void;
14+
showPaymasterUrlInput?: boolean;
1215
}
1316

1417
export const CodeEditor = ({
@@ -20,6 +23,9 @@ export const CodeEditor = ({
2023
includePayerInfo,
2124
onPayerInfoToggle,
2225
showPayerInfoToggle = true,
26+
paymasterUrl = '',
27+
onPaymasterUrlChange,
28+
showPaymasterUrlInput = false,
2329
}: CodeEditorProps) => {
2430
return (
2531
<div className={styles.editorPanel}>
@@ -70,6 +76,22 @@ export const CodeEditor = ({
7076
</div>
7177
)}
7278

79+
{showPaymasterUrlInput && (
80+
<div className={styles.inputContainer}>
81+
<label className={styles.inputLabel}>
82+
<span className={styles.inputLabelText}>Paymaster URL</span>
83+
<input
84+
type="text"
85+
value={paymasterUrl}
86+
onChange={(e) => onPaymasterUrlChange?.(e.target.value)}
87+
disabled={isLoading}
88+
className={styles.textInput}
89+
placeholder="https://your-paymaster.com/api"
90+
/>
91+
</label>
92+
</div>
93+
)}
94+
7395
<div className={styles.editorWrapper}>
7496
<textarea
7597
value={code}

examples/testapp/src/pages/pay-playground/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export {
22
DEFAULT_GET_PAYMENT_STATUS_CODE,
33
DEFAULT_PAY_CODE,
44
DEFAULT_PAY_WITH_TOKEN_CODE,
5+
generatePayWithTokenCode,
56
GET_PAYMENT_STATUS_QUICK_TIPS,
67
PAY_CODE_WITH_PAYER_INFO,
78
PAY_QUICK_TIPS,

examples/testapp/src/pages/pay-playground/constants/playground.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,21 @@ export const GET_PAYMENT_STATUS_QUICK_TIPS = [
6868
'Make sure to use the same testnet setting as the original payment',
6969
];
7070

71-
export const DEFAULT_PAY_WITH_TOKEN_CODE = `import { base } from '@base-org/account'
72-
73-
try {
74-
const result = await base.payWithToken({
75-
amount: '1000000', // Amount in base units (e.g., 1 USDC = 1000000)
76-
to: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
77-
token: 'USDC', // Token symbol or contract address
78-
testnet: true,
79-
paymaster: {
71+
export const generatePayWithTokenCode = (
72+
paymasterUrl: string,
73+
includePayerInfo: boolean
74+
): string => {
75+
const paymasterConfig = paymasterUrl
76+
? `paymaster: {
77+
url: '${paymasterUrl}'
78+
}`
79+
: `paymaster: {
8080
// Paymaster configuration for gas sponsorship
8181
// url: 'https://your-paymaster.com/api'
82-
}
83-
})
84-
85-
return result;
86-
} catch (error) {
87-
console.error('Token payment failed:', error.message);
88-
throw error;
89-
}`;
90-
91-
export const PAY_WITH_TOKEN_CODE_WITH_PAYER_INFO = `import { base } from '@base-org/account'
82+
}`;
9283

93-
try {
94-
const result = await base.payWithToken({
95-
amount: '1000000', // Amount in base units (e.g., 1 USDC = 1000000)
96-
to: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
97-
token: 'USDC', // Token symbol or contract address
98-
testnet: true,
99-
paymaster: {
100-
// Paymaster configuration for gas sponsorship
101-
// url: 'https://your-paymaster.com/api'
102-
},
84+
const payerInfoConfig = includePayerInfo
85+
? `,
10386
payerInfo: {
10487
requests: [
10588
{ type: 'name'},
@@ -108,14 +91,30 @@ try {
10891
{ type: 'physicalAddress', optional: true },
10992
{ type: 'onchainAddress' }
11093
]
111-
}
94+
}`
95+
: '';
96+
97+
return `import { base } from '@base-org/account'
98+
99+
try {
100+
const result = await base.payWithToken({
101+
amount: '1000000', // Amount in base units (e.g., 1 USDC = 1000000)
102+
to: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
103+
token: 'USDC', // Token symbol or contract address
104+
testnet: true,
105+
${paymasterConfig}${payerInfoConfig}
112106
})
113107
114108
return result;
115109
} catch (error) {
116110
console.error('Token payment failed:', error.message);
117111
throw error;
118112
}`;
113+
};
114+
115+
export const DEFAULT_PAY_WITH_TOKEN_CODE = generatePayWithTokenCode('', false);
116+
117+
export const PAY_WITH_TOKEN_CODE_WITH_PAYER_INFO = generatePayWithTokenCode('', true);
119118

120119
export const PAY_WITH_TOKEN_QUICK_TIPS = [
121120
'Get testnet ETH at <a href="https://faucet.circle.com/" target="_blank" rel="noopener noreferrer">https://faucet.circle.com/</a> - select "Base Sepolia" as the network',

examples/testapp/src/pages/pay-playground/index.page.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
DEFAULT_GET_PAYMENT_STATUS_CODE,
55
DEFAULT_PAY_CODE,
66
DEFAULT_PAY_WITH_TOKEN_CODE,
7+
generatePayWithTokenCode,
78
GET_PAYMENT_STATUS_QUICK_TIPS,
89
PAY_CODE_WITH_PAYER_INFO,
910
PAY_QUICK_TIPS,
10-
PAY_WITH_TOKEN_CODE_WITH_PAYER_INFO,
1111
PAY_WITH_TOKEN_QUICK_TIPS,
1212
} from './constants';
1313
import { useCodeExecution } from './hooks';
@@ -16,6 +16,7 @@ import styles from './styles/Home.module.css';
1616
function PayPlayground() {
1717
const [includePayerInfo, setIncludePayerInfo] = useState(false);
1818
const [includePayWithTokenPayerInfo, setIncludePayWithTokenPayerInfo] = useState(false);
19+
const [payWithTokenPaymasterUrl, setPayWithTokenPaymasterUrl] = useState('');
1920
const [payCode, setPayCode] = useState(DEFAULT_PAY_CODE);
2021
const [payWithTokenCode, setPayWithTokenCode] = useState(DEFAULT_PAY_WITH_TOKEN_CODE);
2122
const [getPaymentStatusCode, setGetPaymentStatusCode] = useState(DEFAULT_GET_PAYMENT_STATUS_CODE);
@@ -47,17 +48,24 @@ function PayPlayground() {
4748

4849
const handlePayWithTokenReset = () => {
4950
setIncludePayWithTokenPayerInfo(false);
51+
setPayWithTokenPaymasterUrl('');
5052
setPayWithTokenCode(DEFAULT_PAY_WITH_TOKEN_CODE);
5153
payWithTokenExecution.reset();
5254
};
5355

5456
const handlePayWithTokenPayerInfoToggle = (checked: boolean) => {
5557
setIncludePayWithTokenPayerInfo(checked);
56-
const newCode = checked ? PAY_WITH_TOKEN_CODE_WITH_PAYER_INFO : DEFAULT_PAY_WITH_TOKEN_CODE;
58+
const newCode = generatePayWithTokenCode(payWithTokenPaymasterUrl, checked);
5759
setPayWithTokenCode(newCode);
5860
payWithTokenExecution.reset();
5961
};
6062

63+
const handlePayWithTokenPaymasterUrlChange = (url: string) => {
64+
setPayWithTokenPaymasterUrl(url);
65+
const newCode = generatePayWithTokenCode(url, includePayWithTokenPayerInfo);
66+
setPayWithTokenCode(newCode);
67+
};
68+
6169
const handleGetPaymentStatusExecute = () => {
6270
getPaymentStatusExecution.executeCode(getPaymentStatusCode);
6371
};
@@ -144,6 +152,9 @@ try {
144152
includePayerInfo={includePayWithTokenPayerInfo}
145153
onPayerInfoToggle={handlePayWithTokenPayerInfoToggle}
146154
showPayerInfoToggle={true}
155+
paymasterUrl={payWithTokenPaymasterUrl}
156+
onPaymasterUrlChange={handlePayWithTokenPaymasterUrlChange}
157+
showPaymasterUrlInput={true}
147158
/>
148159
<QuickTips tips={PAY_WITH_TOKEN_QUICK_TIPS} />
149160
</div>

examples/testapp/src/pages/pay-playground/utils/codeSanitizer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import * as acorn from 'acorn';
33
// Define the whitelist of allowed operations
44
export const WHITELIST = {
55
// Allowed SDK functions
6-
allowedFunctions: ['pay', 'getPaymentStatus'],
6+
allowedFunctions: ['pay', 'getPaymentStatus', 'payWithToken'],
77

88
// Allowed object properties and methods
99
allowedObjects: {
10-
base: ['pay', 'getPaymentStatus'],
10+
base: ['pay', 'getPaymentStatus', 'payWithToken'],
1111
console: ['log', 'error', 'warn', 'info'],
1212
Promise: ['resolve', 'reject', 'all', 'race'],
1313
Object: ['keys', 'values', 'entries', 'assign'],

0 commit comments

Comments
 (0)