# SDK Integration

Amulet SDK provides functionalities for other Dapp to integrate with Amulet protocol.

## Github

{% embed url="<https://github.com/Amulet-Protocol/amulet-sdk>" %}

## SDK Library

Amulet SDK can be installed from `npm` package manager

```typescript
npm install @amulet.org/sdk
```

It currently supports the following functions:

* Get premium
* Buy Cover
* Stake and underwrite SOL token
* Unstake AUWT token and receive a ticket
* Withdraw AMTSOL token with ticket

The full sample code for buying cover using JS code can be found [here](https://github.com/Amulet-Protocol/amulet-sdk/tree/main/example)

### Prerequisites

Before using the SDK, you are required to register an API key with Amulet protocol. The API key is required by the SDK to call the `getPremium` API from our endpoint. The API key can be requested by submitting the form [here](https://forms.office.com/Pages/ResponsePage.aspx?id=sgaXQG1kiUCFqL-DRuRopJ7l7SqtVTFJhhmIe-vp8gRUME5INUNMM0FKNjBJSFJWOVhJRjIxR1ZJViQlQCN0PWcu).&#x20;

After receiving your API key, add it on `Amulet` class `apiSecret` key parameter, as shown below

```typescript
new Amulet({
    ...
    apiSecret: process.env.REACT_APP_API_SECRET
})
```

### Get premium

Get premium function calculates the total premium amount that needs to be paid for a product with respect to the cover duration and cover amount.

Below is a step-by-step illustration of the JS code:

```typescript
import { clusterApiUrl, Connection } from '@solana/web3.js';
import { Amulet, Mode, BN, ProductId } from '@amulet/sdk';
import type { GetPremiumParam } from '@amulet/sdk';

const amulet = new Amulet({
    mode: Mode.Devnet,
    connection: new Connection(clusterApiUrl('devnet')),
    apiSecret: process.env.REACT_APP_API_SECRET
});

const param: GetPremiumParam = {
    productId: ProductId.Raydium,
    coverAmount: new BN(1e9),
    days: 30,
};

// Calculate cover premium
const { premium } = await amulet.getPremium(param);
console.log(premium); // BN(2000)
```

### Buy Cover&#x20;

Buy cover function enables user to purchase a product cover with the input of cover duration and cover amount.

Below is a step-by-step illustration of the JS code:

```typescript
import { clusterApiUrl, Connection, Keypair } from '@solana/web3.js';
import { Amulet, Mode, BN, ProductId } from '@amulet/sdk';
import type { BuyCoverParam } from '@amulet/sdk';

const amulet = new Amulet({
    mode: Mode.Devnet,
    connection: new Connection(clusterApiUrl('devnet')),
    apiSecret: process.env.REACT_APP_API_SECRET
});

const keypair = Keypair.generate(); // Replace this with the user wallet address 

const param: BuyCoverParam = {
    owner: keypair.publicKey,
    referrer: keypair.publicKey,
    productId: ProductId.Raydium,
    coverAmount: new BN(1e9),
    days: 30,
};

// Buy cover
const { transaction } = await amulet.buyCover(param);
console.log(transaction.signature); 
// 5wzdPU5zd57o1hJvtg9U4a8xciFazPn8MD5WqyUhGN5AdrrceM1rU9CtaastW7g5e6AbsbLxLHnDpRP8gPNnNKnQ
```

### Stake and underwrite SOL token

This function allows SOL token to be staked on Amulet protocol with earning yield from staking and underwriting pool. AUWT token is minted and transferred to user token account as the representative token.

Below is a step-by-step illustration of the JS code:

```typescript
import { clusterApiUrl, Connection, Keypair } from '@solana/web3.js';
import { Amulet, Mode, BN } from '@amulet/sdk';
import type { StakeSolForAuwtParam } from '@amulet/sdk';

const amulet = new Amulet({
    mode: Mode.Devnet,
    connection: new Connection(clusterApiUrl('devnet')),
    apiSecret: process.env.REACT_APP_API_SECRET
});

const keypair = Keypair.generate(); // Replace this with the user wallet address 

const param: StakeSolForAuwtParam = {
    staker: keypair.publicKey,
    stakeAmount: new BN(1e9),
};

// Stake SOL token and get AUWT token
const { transaction } = await amulet.stakeSolForAuwt(param);
console.log(transaction.signature); 
// 5wzdPU5zd57o1hJvtg9U4a8xciFazPn8MD5WqyUhGN5AdrrceM1rU9CtaastW7g5e6AbsbLxLHnDpRP8gPNnNKnQ
```

### Unstake AUWT token and receive a ticket

This function allows user to swap AUWT token for amtSOL token without paying unstaking fee by waiting. A ticket receipt account will be created recording the amount to be withdrawn and waiting period. The ticket can only be used to withdraw amtSOL after the stated waiting period.

Below is a step-by-step illustration of the JS code:

```typescript
import { clusterApiUrl, Connection, Keypair } from '@solana/web3.js';
import { Amulet, Mode, BN } from '@amulet/sdk';
import type { RedeemAuwtDelayedParam } from '@amulet/sdk';

const amulet = new Amulet({
    mode: Mode.Devnet,
    connection: new Connection(clusterApiUrl('devnet')),
    apiSecret: process.env.REACT_APP_API_SECRET
});

const keypair = Keypair.generate(); // Replace this with the user wallet address 

const param: RedeemAuwtDelayedParam = {
    staker: keypair.publicKey,
    redeemAmount: new BN(1e9),
};

// Unstake AUWT token for AMTSOL token with receiving a ticket
const { transaction, ticketAccount } = await amulet.redeemAuwtForAmtsolDelayed(param);
console.log(transaction.signature);
// 5wzdPU5zd57o1hJvtg9U4a8xciFazPn8MD5WqyUhGN5AdrrceM1rU9CtaastW7g5e6AbsbLxLHnDpRP8gPNnNKnQ
console.log(ticketAccount);
// DkVm7cLFRYMe98dhQY334PUXQG3ZBEHPs4ph9iiGogcm
```

### Withdraw amtSOL token with a ticket.

This function allows user to withdraw amtSOL with the ticket receipt generated from the AUWT token unstaking function.

Below is a step-by-step illustration of the JS code:

```typescript
import { clusterApiUrl, Connection, Keypair, PublicKey } from '@solana/web3.js';
import { Amulet, Mode } from '@amulet/sdk';
import type { WithdrawTicketAccountParam } from '@amulet/sdk';

const amulet = new Amulet({
    mode: Mode.Devnet,
    connection: new Connection(clusterApiUrl('devnet')),
    apiSecret: process.env.REACT_APP_API_SECRET
});

const keypair = Keypair.generate(); // Replace this with the user wallet address 
// Replace this with the ticket account address from AUWT unstaking event
const ticketAccount = new PublicKey("DkVm7cLFRYMe98dhQY334PUXQG3ZBEHPs4ph9iiGogcm") 

const param: WithdrawTicketAccountParam = {
    staker: keypair.publicKey,
    ticketAccount: ticketAccount,
};

// Withdraw AMTSOL token with the ticket
const { transaction } = await amulet.withdrawAmtsolTicketAccount(param);
console.log(transaction.signature);
// 5wzdPU5zd57o1hJvtg9U4a8xciFazPn8MD5WqyUhGN5AdrrceM1rU9CtaastW7g5e6AbsbLxLHnDpRP8gPNnNKnQ
```
