# TypeScript Client

[![GitHub](https://img.shields.io/badge/GitHub-dolomite--exchange%2Fdolomite--margin-lightgrey)](https://github.com/dolomite-exchange/dolomite-margin)

## Install <a href="#install" id="install"></a>

```shell
npm i -s @dolomite-exchange/dolomite
```

or with `yarn`

```sh
yarn add @dolomite-exchange/dolomite
```

## Initialize <a href="#initialize" id="initialize"></a>

You will need to initialize DolomiteMargin using a [Web3 provider](https://web3js.readthedocs.io/en/v1.2.1/web3.html#providers) / Web3 node endpoint and Network.

```typescript
import { DolomiteMargin, Networks } from '@dolomite-exchange/dolomite-margin';

// --- Initialize with Web3 provider ---
const dolomiteMargin = new DolomiteMargin(
  provider,  // Valid web3 provider
  Networks.ARBITRtUM,
  {
    defaultAccount: '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1', // Optional
    accounts: [
      {
        address: '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1', // Optional
        privateKey: '0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d',
      },
    ], // Optional: loading in an account for signing transactions
  }, // Optional
);

// --- OR Initialize with Web3 Provider node endpoint ---
const dolomiteMargin = new DolomiteMargin(
  'https://arbitrum-mainnet.infura.io/v3/YOUR-PROJECT-ID',
  Networks.ARBITRUM,
  {
    defaultAccount: '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1', // Optional - but needed if using Infura
    accounts: [
      {
        address: '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1', // Optional
        privateKey: '0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d',
      },
    ], // Optional: loading in an account for signing transactions
  }, // Optional
);
```

## Standard Actions <a href="#standard-actions" id="standard-actions"></a>

DolomiteMargin exposes a number of "standard" actions for interacting with the protocol. These are a subset of what is possible with [Operations](https://docs.dolomite.io/#/typescript?id=operations), but are simpler to use.

### **Deposit**

Deposit funds to DolomiteMargin

```typescript
import { MarketId, BigNumber } from '@dolomite-exchange/dolomite-margin';

// Deposits a certain amount of tokens for some asset.
// By default resolves when transaction is received by the node - not when mined
const result = await dolomiteMargin.standardActions.deposit({
  accountOwner: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5', // Your address
  marketId: MarketId.ETH,

  // Base units of the token, so 1e18 = 1 ETH
  // NOTE: USDC has 6 decimal places, so 1e6 = 1 USDC
  amount: new BigNumber('1e18'),
});
```

* `MarketId.ETH` will send ETH whereas `MarketId.WETH` will send WETH. Both are the same market on the protocol
* For all markets except `MarketId.ETH`, you will first need to set allowance on that token. See [Tokens](https://docs.dolomite.io/#/typescript?id=tokens)

### **Withdraw**

Withdraw funds from DolomiteMargin

```typescript
import { MarketId, BigNumber } from '@dolomite-exchange/dolomite-margin';

// Withdraws a certain amount of tokens for some asset.
// By default resolves when transaction is received by the node - not when mined
const result = await dolomiteMargin.standardActions.withdraw({
  accountOwner: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5', // Your address
  marketId: MarketId.ETH,

  // Base units of the token, so 1e18 = 1 ETH
  // NOTE: USDC has 6 decimal places, so 1e6 = 1 USDC
  amount: new BigNumber('1e18'),
});

// Withdraws all of your tokens for some asset.
// By default resolves when transaction is received by the node - not when mined
const result = await dolomiteMargin.standardActions.withdrawToZero({
  accountOwner: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5', // Your address
  marketId: MarketId.ETH,
});
```

* `MarketId.ETH` will withdraw as ETH whereas `MarketId.WETH` will withdraw as WETH. Both are the same market on the protocol

## Operations <a href="#operations" id="operations"></a>

The main way to interact with DolomiteMargin is through Operations. See [Operations](/developer-documentation/dolomite-margin-glossary.md#operations).

### **Initialize**

To initialize an Operation:

```typescript
const operation = dolomiteMargin.operation.initiate();
```

DolomiteMargin also provides a [Payable Proxy](https://github.com/dolomite-exchange/dolomite-margin/blob/master/contracts/external/proxies/PayableProxy.sol) contract that will automatically wrap and unwrap ETH <-> WETH, so that users can interact with DolomiteMargin using only ETH. You can use it by:

```typescript
const operation = dolomiteMargin.operation.initiate({ proxy: ProxyType.Payable });
```

### **Add Actions**

Once an operation is initialized, Actions can be added to it. Action functions modify the `operation` itself, and also always return the `operation`.

In this example 1 ETH is being withdrawn from an account, and then 200 DAI are being deposited into it:

```typescript
import { MarketId } from '@dolomite-exchange/dolomite-margin';

operation.withdraw({
  primaryAccountOwner: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5',
  primaryAccountId: new BigNumber('123456'),
  marketId: MarketId.WETH,
  amount: {
    value: new BigNumber('-1e18'),
    reference: AmountReference.Delta,
    denomination: AmountDenomination.Actual,
  },
  to: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5'
})ty
  .deposit({
    primaryAccountOwner: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5',
    primaryAccountId: new BigNumber('123456'),
    marketId: MarketId.DAI,
    amount: {
      value: new BigNumber('200e18'),
      reference: AmountReference.Delta,
      denomination: AmountDenomination.Actual,
    },
    from: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5',
  });
```

See [AccountOperation](https://github.com/dolomite-exchange/dolomite-margin/blob/master/src/modules/operate/AccountOperation.ts) for the full list of Actions available to add to an Operation.

### **Commit**

After Actions have been added to the `operation`, it can be committed. This is what sends the transaction to the blockchain to execute the Operation on the protocol.

```typescript
const response = await operation.commit({
  from: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5',
  gasPrice: '1000000000',
  confirmationType: ConfirmationType.Confirmed,
});
```

## Getters <a href="#getters" id="getters"></a>

DolomiteMargin provides a number of read-only getter functions which read information off the smart contracts on the blockchain. You can find them [here](https://github.com/dolomite-exchange/dolomite-margin/blob/master/src/modules/Getters.ts).

Example of getting the balances of an Account:

```typescript
const balances = await dolomiteMargin.getters.getAccountBalances(
  '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359', // Account Owner
  new BigNumber('11'), // Account Number
);
```

## Logs <a href="#logs" id="logs"></a>

DolomiteMargin provides a helper to parse DolomiteMargin-specific logs from a transaction.

```typescript
const dolomiteMarginLogs = dolomiteMargin.logs.parseLogs(transactionReceipt);
```

## Tokens <a href="#tokens" id="tokens"></a>

DolomiteMargin provides helper functions to help with interacting with ERC20 tokens. You can find them all [here](https://github.com/dolomite-exchange/dolomite-margin/blob/master/src/modules/Token.ts).

Example of setting DAI token allowance on DolomiteMargin:

```typescript
await dolomiteMargin.token.setMaximumDolomiteMarginAllowance(
  '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359', // DAI Contract Address
  '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5', // My Address
  { from: '0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5' }, // My Address
);
```

## Types <a href="#types" id="types"></a>

You can import types from DolomiteMargin as:

```typescript
import {
  AmountDenomination,
  AmountReference,
  ConfirmationType,
} from '@dolomite-exchange/dolomite-margin';
```

## Web3 <a href="#web3" id="web3"></a>

DolomiteMargin uses [Web3 1.2.X](https://web3js.readthedocs.io/) under the hood. You can access it through `dolomiteMargin.web3`

## BigNumber <a href="#bignumber" id="bignumber"></a>

DolomiteMargin uses [BigNumber 8.X](http://mikemcl.github.io/bignumber.js/). You can import this from DolomiteMargin as:

```javascript
import { BigNumber } from '@dolomite-exchange/dolomite-margin';
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.dolomite.io/developer-documentation/typescript-client.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
