Skip to content

Commit

Permalink
remove hardcoded delegate contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarnadas committed Feb 8, 2024
1 parent 1f647c4 commit ff8c5e2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
36 changes: 31 additions & 5 deletions src/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ import {
DelegateSignerResponse,
announceDelegateSigner,
delegateAddOrderlyKey,
registerDelegateSigner,
registerExampleDelegateSigner,
isTestnet
} from './helpers';

export const Account: FC<{
brokerId: string;
accountId: string;
contractAddress: string;
delegateSigner?: DelegateSignerResponse;
setDelegateSigner: React.Dispatch<React.SetStateAction<DelegateSignerResponse | undefined>>;
orderlyKey?: Uint8Array;
setOrderlyKey: React.Dispatch<React.SetStateAction<Uint8Array | undefined>>;
}> = ({ brokerId, accountId, delegateSigner, setDelegateSigner, orderlyKey, setOrderlyKey }) => {
}> = ({
brokerId,
accountId,
contractAddress,
delegateSigner,
setDelegateSigner,
orderlyKey,
setOrderlyKey
}) => {
const [txHash, setTxHash] = useState<string>('');
const [publicKey, setPublicKey] = useState<string>();

Expand Down Expand Up @@ -101,7 +110,12 @@ export const Account: FC<{
onClick={async () => {
const address = wallet?.accounts[0]?.address;
if (!wallet || !connectedChain || !address) return;
const hash = await registerDelegateSigner(wallet, brokerId, connectedChain.id, address);
const hash = await registerExampleDelegateSigner(
wallet,
brokerId,
connectedChain.id,
address
);
setTxHash(hash);
}}
>
Expand All @@ -127,7 +141,13 @@ export const Account: FC<{
disabled={!wallet || !connectedChain || !brokerId || !txHash}
onClick={async () => {
if (!wallet || !connectedChain || !brokerId || !txHash) return;
const res = await announceDelegateSigner(wallet, connectedChain.id, brokerId, txHash);
const res = await announceDelegateSigner(
wallet,
connectedChain.id,
brokerId,
contractAddress,
txHash
);
setDelegateSigner(res);
}}
>
Expand All @@ -139,7 +159,13 @@ export const Account: FC<{
disabled={!wallet || !connectedChain || !brokerId}
onClick={async () => {
if (!wallet || !connectedChain || !brokerId) return;
const key = await delegateAddOrderlyKey(wallet, connectedChain.id, brokerId, accountId);
const key = await delegateAddOrderlyKey(
wallet,
connectedChain.id,
brokerId,
contractAddress,
accountId
);
setOrderlyKey(key);
}}
>
Expand Down
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ function App() {
<Account
brokerId={brokerId}
accountId={accountId}
contractAddress={contractAddress}
delegateSigner={delegateSigner}
setDelegateSigner={setDelegateSigner}
orderlyKey={orderlyKey}
Expand Down
10 changes: 7 additions & 3 deletions src/Assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,20 @@ export const Assets: FC<{
<Table.Row>
<Table.RowHeaderCell>Wallet Balance (USDC):</Table.RowHeaderCell>
<Table.Cell>
{balance ? formatter.format(Number(formatUnits(balance, 6))) : '-'}
{balance != null ? formatter.format(Number(formatUnits(balance, 6))) : '-'}
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.RowHeaderCell>Contract Balance (USDC):</Table.RowHeaderCell>
<Table.Cell>
{contractBalance ? formatter.format(Number(formatUnits(contractBalance, 6))) : '-'}
{contractBalance != null
? formatter.format(Number(formatUnits(contractBalance, 6)))
: '-'}
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.RowHeaderCell>Account Balance (USDC):</Table.RowHeaderCell>
<Table.Cell>{vaultBalance ? String(vaultBalance) : '-'}</Table.Cell>
<Table.Cell>{vaultBalance != null ? String(vaultBalance) : '-'}</Table.Cell>
</Table.Row>
</Table.Body>
</Table.Root>
Expand Down Expand Up @@ -185,6 +187,7 @@ export const Assets: FC<{
wallet,
connectedChain.id,
brokerId,
contractAddress,
amountBN.toString(),
contractAddress,
accountId
Expand Down Expand Up @@ -213,6 +216,7 @@ export const Assets: FC<{
wallet,
connectedChain.id,
brokerId,
contractAddress,
accountId,
orderlyKey,
amountBN.toString(),
Expand Down
9 changes: 0 additions & 9 deletions src/SafeInstructions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,6 @@ export const SafeInstructions: FC<{ brokerId: string; chainId: string }> = ({
style={{ maxWidth: '100%' }}
/>
</Flex>

<Flex direction="column">
<Strong>Execute transaction</Strong>
<img
src="./confirm-tx.webp"
alt="Confirm Gnosis Safe batch transaction"
style={{ maxWidth: '100%' }}
/>
</Flex>
</Flex>
</Tabs.Content>
</Container>
Expand Down
17 changes: 11 additions & 6 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export type DelegateSignerResponse = {
valid_signer: string;
};

export async function registerDelegateSigner(
export async function registerExampleDelegateSigner(
wallet: WalletState,
brokerId: string,
chainId: string,
Expand All @@ -99,14 +99,15 @@ export async function announceDelegateSigner(
wallet: WalletState,
chainId: string,
brokerId: string,
delegateContract: string,
txHash: ethers.BytesLike
): Promise<DelegateSignerResponse> {
const nonceRes = await fetch(`${getBaseUrl(chainId)}/v1/registration_nonce`);
const nonceJson = await nonceRes.json();
const registrationNonce = nonceJson.data.registration_nonce as string;

const delegateSignerMessage = {
delegateContract: exampleDelegateContract,
delegateContract,
brokerId,
chainId: Number(chainId),
timestamp: Date.now(),
Expand Down Expand Up @@ -144,13 +145,14 @@ export async function delegateAddOrderlyKey(
wallet: WalletState,
chainId: string,
brokerId: string,
delegateContract: string,
accountId: string
): Promise<Uint8Array> {
const privateKey = utils.randomPrivateKey();
const orderlyKey = `ed25519:${encodeBase58(await getPublicKeyAsync(privateKey))}`;
const timestamp = Date.now();
const addKeyMessage = {
delegateContract: exampleDelegateContract,
delegateContract,
brokerId,
chainId: Number(chainId),
orderlyKey,
Expand Down Expand Up @@ -193,6 +195,7 @@ export async function delegateDeposit(
wallet: WalletState,
chainId: string,
brokerId: string,
delegateContract: string,
amount: string,
contractAddress: string,
accountId: string
Expand All @@ -209,13 +212,14 @@ export async function delegateDeposit(
} satisfies VaultTypes.VaultDepositFEStruct;
const depositFee = await contract.getDepositFee(contractAddress, depositInput);

await contract.depositTo(exampleDelegateContract, depositInput, { value: depositFee });
await contract.depositTo(delegateContract, depositInput, { value: depositFee });
}

export async function delegateWithdraw(
wallet: WalletState,
chainId: string,
brokerId: string,
delegateContract: string,
accountId: string,
orderlyKey: Uint8Array,
amount: string,
Expand All @@ -230,7 +234,7 @@ export async function delegateWithdraw(
const withdrawNonce = nonceJson.data.withdraw_nonce as string;

const delegateWithdrawMessage = {
delegateContract: exampleDelegateContract,
delegateContract,
brokerId,
chainId: Number(chainId),
receiver,
Expand Down Expand Up @@ -272,6 +276,7 @@ export async function delegateSettlePnL(
wallet: WalletState,
chainId: string,
brokerId: string,
delegateContract: string,
accountId: string,
orderlyKey: Uint8Array
): Promise<void> {
Expand All @@ -284,7 +289,7 @@ export async function delegateSettlePnL(
const settleNonce = nonceJson.data.settle_nonce as string;

const delegateSettlePnLMessage = {
delegateContract: exampleDelegateContract,
delegateContract,
brokerId,
chainId: Number(chainId),
timestamp: Date.now(),
Expand Down

0 comments on commit ff8c5e2

Please sign in to comment.