# Terra DApps Integration

## To detect MapNode Extension with Terra

To detect whether your browser is running MapNode Extension, please use:

```javascript
if (window.MapNode) {
  console.log('MapNode Extension is installed!');
}
```

## Notice: MapNode Extension Testnet is under development and not available now.

## To connect MapNode Extension Wallet

To connect MapNode Extension means to access the user's \[blockchain - like Cosmos] account(s).

```javascript
// Connect only
const connection = window.MapNode.terra('<chain: String>');
// Connect & get accounts
connection.request({ method: 'terra_accounts' });
// Check whether dapp connected or not
connection.isConnected();
```

Currently chains support: \['terra','cosmos']

***

## To disconnect MapNode Extension Wallet

To disconnect MapNode Extension, please use:

```javascript
connection.disconnect();
```

## To experience functions

Once your account is connected, let's start experiencing more functions.‌

### Get Current Account

return `Promise<Array[String]>`

* If wallet can not be found, return `[]` instead of `throw Error`

```javascript
connection.request({ method: 'terra_accounts' }).then((accounts) => {
  if (accounts[0]) {
    // Do something with accounts
  } else {
    // Wallet not found
  }
});
```

### Check wallet whether exists or not

return `Promise<{data: Boolean}>`

```javascript
const hasWallet = connection.request({ method: 'has_wallet', params: ['<chain>'] });
```

### Execute contract function

return: `Promise<Hash>`

```javascript
const signature = connection.request({
  method: 'terra_execute',
  params: ['<contractAddres: string>', '<executeMsg: string>'],
});

// Example Execute Contract
let cw20Contract =
  'juno10rktvmllvgctcmhl5vv8kl3mdksukyqf2tdveh8drpn0sppugwwqjzz30z';
let execMsg =
  '{"transfer":{"amount":"1","recipient":"juno1cx4nq77x3unvl2xsa9fmm9drxkexzkjnzwt2y7"}}';

const response = await connection.request({
  method: 'terra_execute',
  params: [cw20Contract, execMsg],
});
```

***

## To handle events

### List of events

Currently we only support some action event from wallet extension

```javascript
connection.on('event_name', callback);
​//Example
connection.on('close', () => window.location.reload());
connection.on('accountsChanged', () => window.location.reload());
```

<table><thead><tr><th width="184.0317835309541">Events</th><th>Trigger</th></tr></thead><tbody><tr><td>accountsChanged</td><td>Receive when active account changed in Extension</td></tr><tr><td>networkChanged</td><td>Receive when active network changed in Extension</td></tr><tr><td>chainChanged</td><td>Receive when active chain changed in Extension</td></tr><tr><td>disconnect</td><td>Receive when disconnect from Extension</td></tr><tr><td>close</td><td>Alias for disconnect event</td></tr></tbody></table>

<table><thead><tr><th width="359.9860751564186">Method</th><th>Description</th></tr></thead><tbody><tr><td>on(event, callback)</td><td>Add event listener</td></tr><tr><td>off(event, callback)</td><td>Remove event listener</td></tr></tbody></table>
