Skip to content

Commit

Permalink
remove delegate signer
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarnadas committed Feb 27, 2024
1 parent f8ae23e commit 2a3ebfa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 422 deletions.
78 changes: 10 additions & 68 deletions sdk/hooks/src/Account.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
import { useAccount } from '@orderly.network/hooks';
import { AccountStatusEnum } from '@orderly.network/types';
import { Button, Callout, Card, Container, Flex, Heading, Text } from '@radix-ui/themes';
import { Button, Card, Container, Flex, Heading, Text } from '@radix-ui/themes';
import { JsonRpcSigner } from 'ethers';
import { FC, useEffect, useState } from 'react';
import { FC, useEffect } from 'react';

import {
DelegateSignerResponse,
announceDelegateSigner,
delegateAddOrderlyKey,
registerDelegateSigner
} from './helpers/delegateSigner';
import { testnetChainIdHex } from './network';

export const Account: FC<{
signer?: JsonRpcSigner;
delegateSignerEnabled: boolean;
delegateSigner?: DelegateSignerResponse;
setDelegateSigner: React.Dispatch<React.SetStateAction<DelegateSignerResponse | undefined>>;
setDelegateOrderlyKey: React.Dispatch<React.SetStateAction<string | undefined>>;
}> = ({
signer,
delegateSignerEnabled,
delegateSigner,
setDelegateSigner,
setDelegateOrderlyKey
}) => {
const [txHash, setTxHash] = useState<string | undefined>();

}> = ({ signer }) => {
const { account, state } = useAccount();

useEffect(() => {
Expand Down Expand Up @@ -80,65 +62,25 @@ export const Account: FC<{
)}
</Card>

{delegateSignerEnabled && (
<Callout.Root>
<Callout.Text>
Delegate Signer information is not persisted on successive page visits
</Callout.Text>
</Callout.Root>
)}

{delegateSignerEnabled && (
<>
<Button
disabled={!signer || !account.address}
onClick={async () => {
if (!signer || !account.address) return;
const hash = await registerDelegateSigner(signer, account.address);
setTxHash(hash);
}}
>
Register Delegate Signer
</Button>
</>
)}

<Button
disabled={
delegateSignerEnabled
? !account.wallet || !account.address || !txHash
: state.status !== AccountStatusEnum.NotSignedIn
}
disabled={state.status !== AccountStatusEnum.NotSignedIn}
onClick={async () => {
if (delegateSignerEnabled) {
if (!account.wallet || !account.address || !txHash) return;
const res = await announceDelegateSigner(account, txHash);
setDelegateSigner(res);
} else {
await account.createAccount();
}
await account.createAccount();
}}
>
{delegateSignerEnabled ? 'Announce Delegate Signer' : 'Create Account'}
Create Account
</Button>

<Button
disabled={
delegateSignerEnabled
? delegateSigner == null
: state.status > AccountStatusEnum.DisabledTrading ||
state.status === AccountStatusEnum.NotConnected
state.status > AccountStatusEnum.DisabledTrading ||
state.status === AccountStatusEnum.NotConnected
}
onClick={async () => {
if (delegateSignerEnabled) {
const key = await delegateAddOrderlyKey(account);
setDelegateOrderlyKey(key);
} else {
await account.createOrderlyKey(30);
}
await account.createOrderlyKey(30);
}}
>
{delegateSignerEnabled ? 'Create Delegate Orderly Key' : 'Create Orderly Key'}
Create Orderly Key
</Button>
</Flex>
);
Expand Down
30 changes: 3 additions & 27 deletions sdk/hooks/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, Container, Flex, Switch, Tabs } from '@radix-ui/themes';
import { Button, Container, Flex, Tabs } from '@radix-ui/themes';
import { BrowserProvider, JsonRpcSigner } from 'ethers';
import { useEffect, useState } from 'react';

Expand All @@ -9,17 +9,11 @@ import { Orderbook } from './Orderbook';
import { Orders } from './Orders';
import { Positions } from './Positions';
import { Trades } from './Trades';
import { DelegateSignerResponse } from './helpers/delegateSigner';
import { checkValidNetwork } from './network';

function App() {
const [provider, setProvider] = useState<BrowserProvider | undefined>();
const [signer, setSigner] = useState<JsonRpcSigner | undefined>();
const [delegateSignerEnabled, setDelegateSignerEnabled] = useState<boolean>(false);
const [delegateSigner, setDelegateSigner] = useState<DelegateSignerResponse>();
const [_delegateOrderlyKey, setDelegateOrderlyKey] = useState<string>();

console.log('delegateSigner', delegateSigner);

useEffect(() => {
async function run() {
Expand Down Expand Up @@ -59,14 +53,6 @@ function App() {
? `${signer.address.substring(0, 6)}...${signer.address.substr(-4)}`
: 'Connect wallet'}
</Button>
<Flex gap="1">
<Switch
onCheckedChange={(checked) => {
setDelegateSignerEnabled(checked);
}}
/>
Delegate Signer mode
</Flex>
</Flex>

<Tabs.Root defaultValue="account" style={{ marginTop: '1rem' }}>
Expand All @@ -81,20 +67,10 @@ function App() {
</Tabs.List>

<Tabs.Content value="account">
<Account
signer={signer}
delegateSignerEnabled={delegateSignerEnabled}
delegateSigner={delegateSigner}
setDelegateSigner={setDelegateSigner}
setDelegateOrderlyKey={setDelegateOrderlyKey}
/>
<Account signer={signer} />
</Tabs.Content>
<Tabs.Content value="assets">
<Assets
signer={signer}
delegateSignerEnabled={delegateSignerEnabled}
delegateSigner={delegateSigner}
/>
<Assets signer={signer} />
</Tabs.Content>
<Tabs.Content value="orderbook">
<Orderbook />
Expand Down
67 changes: 16 additions & 51 deletions sdk/hooks/src/Assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,11 @@ import { Button, Flex, Grid, Heading, Table, TextField } from '@radix-ui/themes'
import { JsonRpcSigner } from 'ethers';
import { FC, useMemo, useState } from 'react';

import {
DelegateSignerResponse,
delegateContract,
delegateDeposit,
delegateSettlePnL,
delegateWithdraw
} from './helpers/delegateSigner';
import { testnetChainId } from './network';

export const Assets: FC<{
signer?: JsonRpcSigner;
delegateSignerEnabled: boolean;
delegateSigner?: DelegateSignerResponse;
}> = ({ signer, delegateSignerEnabled, delegateSigner }) => {
}> = ({ signer }) => {
const { account } = useAccount();
const collateral = useCollateral();
const [chains] = useChains('testnet', {
Expand Down Expand Up @@ -66,12 +57,10 @@ export const Assets: FC<{
</Table.Root>
<Grid
columns="2"
rows={delegateSignerEnabled ? '5' : '4'}
rows="4"
gap="1"
style={{
gridTemplateAreas: delegateSignerEnabled
? `'input input' 'deposit deposit' 'withdraw withdraw' 'mint mint' 'settlepnl settlepnl'`
: `'input input' 'deposit withdraw' 'mint mint' 'settlepnl settlepnl'`
gridTemplateAreas: `'input input' 'deposit withdraw' 'mint mint' 'settlepnl settlepnl'`
}}
>
<TextField.Root style={{ gridArea: 'input' }}>
Expand All @@ -93,54 +82,36 @@ export const Assets: FC<{
if (amount == null) return;
if (Number(deposit.allowance) < Number(amount)) {
await deposit.approve(amount.toString());
} else if (delegateSignerEnabled) {
if (!signer || !delegateSigner) return;
await delegateDeposit(signer, amount, delegateSigner.account_id);
} else {
deposit.setQuantity(amount);
await deposit.deposit();
}
}}
>
{Number(deposit.allowance) < Number(amount)
? 'Approve'
: delegateSignerEnabled
? 'Deposit to Contract'
: 'Deposit'}
{Number(deposit.allowance) < Number(amount) ? 'Approve' : 'Deposit'}
</Button>

<Button
style={{ gridArea: 'withdraw' }}
disabled={
delegateSignerEnabled ? delegateSigner == null || amount == null : amount == null
}
disabled={amount == null}
onClick={async () => {
if (amount == null) return;
if (delegateSignerEnabled) {
await delegateWithdraw(account, amount);
} else {
await withdraw({
chainId: Number(testnetChainId),
amount: Number(amount),
token: 'USDC',
allowCrossChainWithdraw: false
});
}
await withdraw({
chainId: Number(testnetChainId),
amount: Number(amount),
token: 'USDC',
allowCrossChainWithdraw: false
});
}}
>
{delegateSignerEnabled ? 'Withdraw to Contract' : 'Withdraw'}
Withdraw
</Button>

<Button
style={{ gridArea: 'mint' }}
disabled={signer == null}
onClick={async () => {
let user_address;
if (delegateSignerEnabled) {
user_address = delegateContract;
} else {
user_address = signer?.address;
}
const user_address = signer?.address;
if (!user_address) return;
await fetch('https://testnet-operator-evm.orderly.org/v1/faucet/usdc', {
method: 'POST',
Expand All @@ -160,18 +131,12 @@ export const Assets: FC<{

<Button
style={{ gridArea: 'settlepnl' }}
disabled={
delegateSignerEnabled ? delegateSigner == null : signer == null && unsettledPnL > 0
}
disabled={signer == null && unsettledPnL > 0}
onClick={async () => {
if (delegateSignerEnabled) {
await delegateSettlePnL(account);
} else {
await account.settle();
}
await account.settle();
}}
>
{delegateSignerEnabled ? 'Settle Delegate PnL' : 'Settle PnL'}
Settle PnL
</Button>
</Grid>
</Flex>
Expand Down
Loading

0 comments on commit 2a3ebfa

Please sign in to comment.