Skip to content

Commit

Permalink
feat: Add vault functions for native/mobile (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang authored Sep 26, 2024
1 parent dcdd233 commit e62c959
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 6 deletions.
94 changes: 91 additions & 3 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
4 changes: 2 additions & 2 deletions v4-client-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion v4-client-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dydxprotocol/v4-client-js",
"version": "1.3.6",
"version": "1.3.7",
"description": "General client library for the new dYdX system (v4 decentralized)",
"main": "build/src/index.js",
"scripts": {
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 @@ -265,6 +268,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
89 changes: 89 additions & 0 deletions v4-client-js/src/clients/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1352,3 +1352,92 @@ export async function setSelectedGasDenom(gasDenom: string): Promise<string> {
return wrappedError(error);
}
}

export async function getMegavaultOwnerShares(payload: string): 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.getMegavaultOwnerShares(address);
return encodeJson(response);
} catch (e) {
return wrappedError(e);
}
}

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 response =
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 e62c959

Please sign in to comment.