# DEX Class

### Constructor

Instances of <mark style="color:orange;">`DEX`</mark> cannot be constructed manually. They are only accessible as a return value of <mark style="color:yellow;">`SDK.init()`</mark>. Each instance of the DEX class represents a connection to a particular instance of the Portal DEX network for a particular user.

The DEX instance returned by <mark style="color:yellow;">`SDK.init()`</mark> contains no assets. Before any trades are executed on that instance, assets must be transferred into it using <mark style="color:orange;">`DEX.xfer()`</mark>.

### Instance Properties

```javascript
Array = dex.assets
```

Returns the assets and their quantities currently available. The elements of the Array are Objects with format: `{ asset: String, qty: Number }`.

### Instance Methods

```javascript
Array = dex.xfer( source, assets )
```

Transfers one or more assets into the receiver from the specified source. Instances of DEX initially contain no assets and this method must be called before any trades are executed.

<table><thead><tr><th width="131">Argument</th><th>Type and Semantics</th></tr></thead><tbody><tr><td><mark style="color:blue;"><code>source</code></mark></td><td>Object. Specifies the source of the assets and the parameters necessary for transferring them. Format depends on the source.</td></tr><tr><td><mark style="color:blue;"><code>assets</code></mark></td><td>Array. Specifies quantities of assets to transfer. The elements of the Array are Objects with format: <code>{ asset: String, qty: Number }</code></td></tr></tbody></table>

<mark style="color:orange;">`DEX.xfer`</mark>`()` returns an Array whose elements are Objects with format:

```json
{
   status: String,
   asset: String,
   qty: Number      // quantity actually in wallet
}
```

```javascript
trade = dex.trade( order_id, order_type, goals,
                   sell_asset, sell_qty,
                   buy_asset,  buy_qty )
```

Performs a swap of the specified assets under the specified conditions. Returns an instance of the Trade class. Will throw an exception if there are any errors in the arguments - thus it is advisable to call this function in a try/catch block.

<table><thead><tr><th width="220">Argument</th><th>Type and semantics</th></tr></thead><tbody><tr><td><code>order_id</code></td><td>String (case sensitive). A user-defined identifier of this trade. Stored in the instance and returned on every method call to that instance.</td></tr><tr><td><code>order_type</code></td><td>String (case insensitive). One of: "limit" or "market".</td></tr><tr><td>goals</td><td>Object.</td></tr><tr><td><code>sell_asset, buy_asset</code></td><td>String (case insensitive). One of: "BTC" or "ETH".</td></tr><tr><td><code>sell_qty, buy_qty</code></td><td>Number</td></tr><tr><td><code>sell_ctx, buy_ctx</code></td><td>Object. Format is dependent on the values of <code>order_type</code>, <code>sell_asset</code>, and <code>buy_asset</code>.</td></tr></tbody></table>

```javascript
Boolean = dex.close()
```

Close the connection to receiver. Returns `true` if successful else `false`.

```javascript
trade = dex.find( order_id )
```

Returns the instance of Trade with the specified `order_id` or `undefined` if no trade with that `order_id` was executed on the receiver.

```javascript
Array = dex.history()
```


---

# 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://sdk.portaldefi.com/dex-class.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.
