Skip to content

Commit 67ae1d4

Browse files
committed
eoa withdrawal
1 parent 55e4096 commit 67ae1d4

File tree

3 files changed

+121
-42
lines changed

3 files changed

+121
-42
lines changed

src/App.tsx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121

2222
function App() {
2323
const [brokerId, setBrokerId] = useState<string>('');
24-
const [address, setAddress] = useState<string>('');
24+
const [contractAddress, setContractAddress] = useState<string>('');
2525
const [accountId, setAccountId] = useState<string>();
2626
const [delegateSigner, setDelegateSigner] = useState<DelegateSignerResponse>();
2727
const [orderlyKey, setOrderlyKey] = useState<Uint8Array>();
@@ -41,13 +41,11 @@ function App() {
4141
setBrokerId(loadBrokerId(connectedChain.id));
4242
const address = loadContractAddress(connectedChain.id);
4343
if (!address && isTestnet(connectedChain.id)) {
44-
setAddress(exampleDelegateContract);
45-
} else {
46-
setAddress(address);
44+
setContractAddress(exampleDelegateContract);
4745
}
4846
} else {
4947
setBrokerId('');
50-
setAddress('');
48+
setContractAddress('');
5149
}
5250
}, [connectedChain]);
5351

@@ -120,9 +118,9 @@ function App() {
120118
<label>
121119
Delegate Signer Address
122120
<TextField.Root
123-
value={address}
121+
value={contractAddress}
124122
onChange={(event) => {
125-
setAddress(event.target.value);
123+
setContractAddress(event.target.value);
126124
setAccountId(undefined);
127125
}}
128126
/>
@@ -131,16 +129,16 @@ function App() {
131129
<Button
132130
disabled={
133131
!brokerId ||
134-
!address ||
132+
!contractAddress ||
135133
!connectedChain ||
136-
address.toLowerCase() === wallet?.accounts[0].address.toLowerCase() ||
134+
contractAddress.toLowerCase() === wallet?.accounts[0].address.toLowerCase() ||
137135
!isChainSupported
138136
}
139137
onClick={async () => {
140-
if (!brokerId || !address || !connectedChain) return;
141-
setAccountId(getAccountId(address, brokerId));
138+
if (!brokerId || !contractAddress || !connectedChain) return;
139+
setAccountId(getAccountId(contractAddress, brokerId));
142140
saveBrokerId(connectedChain.id, brokerId);
143-
saveContractAddress(connectedChain.id, address);
141+
saveContractAddress(connectedChain.id, contractAddress);
144142
setShowEOA(false);
145143
setActiveTab('delegate-signer');
146144
}}
@@ -179,7 +177,7 @@ function App() {
179177
<DelegateSigner
180178
brokerId={brokerId}
181179
accountId={accountId}
182-
contractAddress={address}
180+
contractAddress={contractAddress}
183181
delegateSigner={delegateSigner}
184182
setDelegateSigner={setDelegateSigner}
185183
orderlyKey={orderlyKey}
@@ -190,7 +188,7 @@ function App() {
190188
<Assets
191189
brokerId={brokerId}
192190
accountId={accountId}
193-
contractAddress={address}
191+
contractAddress={contractAddress}
194192
showEOA={showEOA}
195193
orderlyKey={orderlyKey}
196194
/>

src/Assets.tsx

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
delegateSettlePnL,
1414
usdFormatter,
1515
settlePnL,
16-
getUnsettledPnL
16+
getUnsettledPnL,
17+
withdraw
1718
} from './helpers';
1819

1920
export const Assets: FC<{
@@ -69,6 +70,9 @@ export const Assets: FC<{
6970
return;
7071
}
7172
const address = wallet.accounts[0].address;
73+
console.log('address', address);
74+
console.log('usdcContract', usdcContract);
75+
console.log('getUSDCAddress(connectedChain.id)', getUSDCAddress(connectedChain.id));
7276
usdcContract.balanceOf(address).then(setBalance);
7377
usdcContract.allowance(address, getVaultAddress(connectedChain.id)).then(setAllowance);
7478
};
@@ -82,6 +86,7 @@ export const Assets: FC<{
8286
setContractBalance(undefined);
8387
return;
8488
}
89+
console.log('contractAddress', contractAddress);
8590
const fetchContractBalance = async () => {
8691
if (!usdcContract || !contractAddress) {
8792
setContractBalance(undefined);
@@ -161,32 +166,19 @@ export const Assets: FC<{
161166
</Table.Body>
162167
</Table.Root>
163168

164-
{showEOA && (
165-
<Flex direction="column" gap="4">
166-
<Button
167-
disabled={!wallet || !connectedChain || !brokerId || !orderlyKey}
168-
onClick={async () => {
169-
if (!wallet || !connectedChain || !brokerId || !orderlyKey) return;
170-
await settlePnL(wallet, connectedChain.id, brokerId, accountId, orderlyKey);
171-
}}
172-
>
173-
Settle PnL
174-
</Button>
175-
</Flex>
176-
)}
169+
<Flex direction="column" gap="4">
170+
<TextField.Root
171+
style={{ gridArea: 'input' }}
172+
type="number"
173+
step="0.01"
174+
min="0"
175+
placeholder="USDC amount"
176+
onChange={(event) => {
177+
setAmount(event.target.value);
178+
}}
179+
/>
177180

178-
{!showEOA && (
179-
<Flex direction="column" gap="4">
180-
<TextField.Root
181-
style={{ gridArea: 'input' }}
182-
type="number"
183-
step="0.01"
184-
min="0"
185-
placeholder="USDC amount"
186-
onChange={(event) => {
187-
setAmount(event.target.value);
188-
}}
189-
/>
181+
{!showEOA && (
190182
<Button
191183
disabled={
192184
!wallet ||
@@ -233,7 +225,29 @@ export const Assets: FC<{
233225
>
234226
{needsApproval ? 'Approve' : 'Deposit to Contract'}
235227
</Button>
228+
)}
236229

230+
{showEOA ? (
231+
<Button
232+
disabled={!wallet || !connectedChain || !brokerId || !orderlyKey}
233+
onClick={async () => {
234+
if (!wallet || !connectedChain || !orderlyKey || !amount) return;
235+
const amountBN = parseUnits(amount, 6);
236+
if (parseUnits(String(vaultBalance), 6) < amountBN) return;
237+
await withdraw(
238+
wallet,
239+
connectedChain.id,
240+
brokerId,
241+
accountId,
242+
orderlyKey,
243+
amountBN.toString(),
244+
wallet.accounts[0].address
245+
);
246+
}}
247+
>
248+
Withdraw
249+
</Button>
250+
) : (
237251
<Button
238252
disabled={
239253
!wallet ||
@@ -262,7 +276,19 @@ export const Assets: FC<{
262276
>
263277
Withdraw from Contract
264278
</Button>
279+
)}
265280

281+
{showEOA ? (
282+
<Button
283+
disabled={!wallet || !connectedChain || !brokerId || !orderlyKey}
284+
onClick={async () => {
285+
if (!wallet || !connectedChain || !brokerId || !orderlyKey) return;
286+
await settlePnL(wallet, connectedChain.id, brokerId, accountId, orderlyKey);
287+
}}
288+
>
289+
Settle PnL
290+
</Button>
291+
) : (
266292
<Button
267293
disabled={!wallet || !connectedChain || !brokerId || !orderlyKey}
268294
onClick={async () => {
@@ -279,8 +305,8 @@ export const Assets: FC<{
279305
>
280306
Settle Delegate PnL
281307
</Button>
282-
</Flex>
283-
)}
308+
)}
309+
</Flex>
284310
</Flex>
285311
);
286312
};

src/helpers/index.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,61 @@ export async function delegateDeposit(
345345
await contract.depositTo(delegateContract, depositInput, { value: depositFee });
346346
}
347347

348+
export async function withdraw(
349+
wallet: WalletState,
350+
chainId: string,
351+
brokerId: string,
352+
accountId: string,
353+
orderlyKey: Uint8Array,
354+
amount: string,
355+
receiver: string
356+
): Promise<void> {
357+
const nonceRes = await signAndSendRequest(
358+
accountId,
359+
orderlyKey,
360+
`${getBaseUrl(chainId)}/v1/withdraw_nonce`
361+
);
362+
const nonceJson = await nonceRes.json();
363+
const withdrawNonce = nonceJson.data.withdraw_nonce as string;
364+
365+
const withdrawMessage = {
366+
brokerId,
367+
chainId: Number(chainId),
368+
receiver,
369+
token: 'USDC',
370+
amount: Number(amount),
371+
timestamp: Date.now(),
372+
withdrawNonce
373+
};
374+
375+
const provider = new BrowserProvider(wallet.provider);
376+
const signer = await provider.getSigner();
377+
const signature = await signer.signTypedData(
378+
getOnChainDomain(chainId),
379+
{ Withdraw: MESSAGE_TYPES.Withdraw },
380+
withdrawMessage
381+
);
382+
383+
const res = await signAndSendRequest(
384+
accountId,
385+
orderlyKey,
386+
`${getBaseUrl(chainId)}/v1/withdraw_request`,
387+
{
388+
method: 'POST',
389+
body: JSON.stringify({
390+
message: withdrawMessage,
391+
signature,
392+
userAddress: wallet.accounts[0].address,
393+
verifyingContract: getVerifyingAddress(chainId)
394+
})
395+
}
396+
);
397+
const withdrawJson = await res.json();
398+
if (!withdrawJson.success) {
399+
throw new Error(withdrawJson.message);
400+
}
401+
}
402+
348403
export async function delegateWithdraw(
349404
wallet: WalletState,
350405
chainId: string,

0 commit comments

Comments
 (0)