Skip to content

Commit

Permalink
Add vault native functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang committed Sep 26, 2024
1 parent 0c31a33 commit 0871665
Show file tree
Hide file tree
Showing 4 changed files with 2,121 additions and 341 deletions.
2,358 changes: 2,025 additions & 333 deletions v4-client-js/__native__/__ios__/v4-native-client.js

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions v4-client-js/examples/native_examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
withdraw,
withdrawToIBC,
wrappedError,
getMegavaultOwnerShares,
depositToMegavault,
withdrawFromMegavault,
} from '../src/clients/native';
import { DYDX_TEST_ADDRESS, DYDX_TEST_MNEMONIC } from './constants';

Expand Down Expand Up @@ -47,6 +50,19 @@ async function test(): Promise<void> {
const userStats = await getUserStats(payload);
console.log(userStats);

const balances1 = await getAccountBalances();
console.log(balances1);

const vaultOwnerShares = await getMegavaultOwnerShares(payload);
console.log(vaultOwnerShares);

// const depositResult = await depositToMegavault(0, 2);
// console.log(depositResult);

const withdrawResult = await withdrawFromMegavault(0, 1, 0);
console.log(withdrawResult);


const sendTokenPayload = {
subaccountNumber: 0,
amount: '10', // Dydx Token
Expand Down
21 changes: 21 additions & 0 deletions v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ export const MAINNET_CHAIN_ID = 'dydx-mainnet-1';
// ------------ API URLs ------------
export enum IndexerApiHost {
TESTNET = 'https://indexer.v4testnet.dydx.exchange/',
STAGING = 'https://indexer.v4staging.dydx.exchange/',
LOCAL = 'http://localhost:3002',
// For the deployment by DYDX token holders
MAINNET = 'https://indexer.dydx.trade',
}

export enum IndexerWSHost {
TESTNET = 'wss://dydx-testnet.imperator.co/v4/ws',
STAGING = 'wss://indexer.v4staging.dydx.exchange/v4/ws',
LOCAL = 'ws://localhost:3003',
// For the deployment by DYDX token holders
MAINNET = 'wss://indexer.dydx.trade/v4/ws',
Expand All @@ -41,6 +43,7 @@ export enum FaucetApiHost {

export enum ValidatorApiHost {
TESTNET = 'https://test-dydx.kingnodes.com',
STAGING = 'https://validator.v4staging.dydx.exchange',
LOCAL = 'http://localhost:26657',
// For the deployment by DYDX token holders
MAINNET = 'https://dydx-ops-rpc.kingnodes.com:443',
Expand Down Expand Up @@ -262,6 +265,24 @@ export class Network {
return new Network('testnet', indexerConfig, validatorConfig);
}

static staging(): Network {
const indexerConfig = new IndexerConfig(IndexerApiHost.STAGING, IndexerWSHost.STAGING);
const validatorConfig = new ValidatorConfig(
ValidatorApiHost.STAGING,
TESTNET_CHAIN_ID,
{
CHAINTOKEN_DENOM: 'adv4tnt',
USDC_DENOM: 'ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5',
USDC_GAS_DENOM: 'uusdc',
USDC_DECIMALS: 6,
CHAINTOKEN_DECIMALS: 18,
},
undefined,
'Client Example',
);
return new Network('staging', indexerConfig, validatorConfig);
}

static local(): Network {
const indexerConfig = new IndexerConfig(IndexerApiHost.LOCAL, IndexerWSHost.LOCAL);
const validatorConfig = new ValidatorConfig(
Expand Down
67 changes: 59 additions & 8 deletions v4-client-js/src/clients/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1372,21 +1372,72 @@ export async function getMegavaultOwnerShares(payload: string): Promise<string>
}
}

export async function getMegavaultWithdrawalInfo(payload: string): Promise<string> {
export async function getMegavaultWithdrawalInfo(
sharesToWithdraw: bigint
): Promise<string> {
try {
const client = globalThis.client;
if (client === undefined) {
throw new UserError('client is not connected. Call connectClient() first');
}
const json = JSON.parse(payload);
const address = json.address;
if (address === undefined) {
throw new UserError('address is not set');
}
const response =
await globalThis.client?.validatorClient.get.getMegavaultWithdrawalInfo(address);
await globalThis.client?.validatorClient.get.getMegavaultWithdrawalInfo(sharesToWithdraw);
return encodeJson(response);
} catch (e) {
return wrappedError(e);
}
}
}

export async function depositToMegavault(
subaccountNumber: number,
amountUsdc: number
): Promise<string> {
try {
const client = globalThis.client;
if (client === undefined) {
throw new UserError('client is not connected. Call connectNetwork() first');
}
const wallet = globalThis.wallet;
if (wallet === undefined) {
throw new UserError('wallet is not set. Call connectWallet() first');
}
const subaccount = new SubaccountInfo(wallet, subaccountNumber);
const tx = await client.depositToMegavault(
subaccount,
amountUsdc,
Method.BroadcastTxCommit,
);
return encodeJson(tx);
} catch (error) {
return wrappedError(error);
}
}

export async function withdrawFromMegavault(
subaccountNumber: number,
shares: number,
minAmount: number,
): Promise<string> {
try {
const client = globalThis.client;
if (client === undefined) {
throw new UserError('client is not connected. Call connectNetwork() first');
}
const wallet = globalThis.wallet;
if (wallet === undefined) {
throw new UserError('wallet is not set. Call connectWallet() first');
}
const subaccount = new SubaccountInfo(wallet, subaccountNumber);
const tx = await client.withdrawFromMegavault(
subaccount,
shares,
minAmount,
Method.BroadcastTxCommit,
);
return encodeJson(tx);
} catch (error) {
return wrappedError(error);
}
}


0 comments on commit 0871665

Please sign in to comment.