# SameChain

#### If reading docs isn't your thing, just check out our example code for integration

> We are currently updating the repository with more examples, so please hold on.

{% embed url="<https://github.com/kanalabs/aggregator-example>" %}

###

### Same Chain Swap

{% hint style="info" %}
If you wish to execute the transaction using your own logic that fits your use case, please refer to our [`aggregator-example`](https://github.com/kanalabs/aggregator-example) repository on Github
{% endhint %}

### **1. SwapQuotes function**

```typescript
 swapQuotes = async (params: SameChainSwapParams)
```

| **SameChainSwapParams** | **Description**                               |
| ----------------------- | --------------------------------------------- |
| inputToken              | The input token for the swap operation.       |
| outputToken             | The output token to receive after the swap.   |
| amountIn                | The amount of input tokens to be swapped.     |
| slippage                | The allowed slippage percentage for the swap. |
| network                 | The network ID for the swap operation.        |
| options                 | (Optional) Additional options for the swap.   |

### **2. ExecuteSwapInstruction**

```typescript
executeSwapInstruction = async (params: SameChainInstructionParams)
```

| **executeSwapInstruction** | **Description**                                                  |
| -------------------------- | ---------------------------------------------------------------- |
| quote                      | A `CommonRouteInterface` representing the quote response format. |
| address                    | The address related to the instruction.                          |
| options                    | (Optional) An object containing additional options.              |
| options.provider           | (Optional) The connection provider to use.                       |
| options.integrator         | (Optional) A string identifying the integrator.                  |
| options.recipient          | (Optional) Address of the recipient.                             |

#### 1 . APTOS :

**1.1 .Initialising The SDK**

```typescript
import { Account, AccountAddress, Aptos, AptosConfig, Ed25519PrivateKey, Network } from "@aptos-labs/ts-sdk";
import { SwapAggregator, Environment, NetworkId } from '@kanalabs/aggregator';

const aptosConfig = new AptosConfig({ network: Network.MAINNET });
const aptosProvider = new Aptos(aptosConfig)

const aptosSigner = Account.fromPrivateKey({
  privateKey: new Ed25519PrivateKey(process.env.APTOS_PRIVATEKEY || ''),
  address:  AccountAddress.from(process.env.APTOS_ADDRESS || ''),
  legacy: true,
});

const swap = new SwapAggregator(Environment.production, {
  providers: {
    aptos: aptosProvider,
  },
  signers: {
    aptos: aptosSigner,
  },
});
```

> NOTE : SIGNER CAN BE A APTOS ACCOUNT INSTANCE DERIVED FROM PRIVATE KEY OR A SIGN AND SUBMIT FUNCTION FROM APTOS WALLET ADAPTER

**1.2 .Getting Swap Quotes**

```typescript
const quote = await swap.swapQuotes({
  apiKey: APIKEY,
  inputToken: '0x1::aptos_coin::AptosCoin',
  outputToken: '0x6f986d146e4a90b828d8c12c14b6f4e003fdff11a8eecceceb63744363eaac01::mod_coin::MOD',
  amountIn: '100000',
  slippage: 0.5,
  network: NetworkId.aptos,
  options:{
      integratorAddress :'0x000...........', // if ur an integrator and want to collect fee then u can pass ur address here
      is_fee_coin_in: true , // u can pass this one as true if u want collect fee from input Token , but by Default it will be false (Output Token)
      isFeeReferrer:true, // this one should be true , if integrator wants to collect fee 
    }
});

const optimalQuote = quote.data[0];
```

{% hint style="info" %}
Please note that the feature of switching fee from output token to input token is only available in aptos chain only
{% endhint %}

**1.3 .Executing Swap Quotes**

```typescript
const executeSwap = await swap.executeSwapInstruction({
  apiKey: APIKEY,
  quote: optimalQuote,
  address: swap.signer?.aptos?.accountAddress.toString(),
});
```

#### 2 . SUI :

**2.1 .Initialising The SDK**\`\`\`typescript

```typescript
import { Ed25519Keypair, JsonRpcProvider, RawSigner, Connection as SuiConnection } from '@mysten/sui.js';
import { SwapAggregator, Environment, NetworkId } from '@kanalabs/aggregator';

const suiProvider = new JsonRpcProvider(new SuiConnection({ fullnode: 'SUI RPC NODE ' }));

const suiSigner = new RawSigner(Ed25519Keypair.deriveKeypair(process.env.SUIMNEMONICS || ''), suiProvider);

const swap = new SwapAggregator(Environment.production, {
  providers: {
    sui: suiProvider,
  },
  signers: {
    sui: suiSigner,
  },
});
```

**2.2 .Getting Swap Quotes**

```typescript
const quote = await swap.swapQuotes({
  apiKey: APIKEY,
  inputToken: '0x2::sui::SUI',
  outputToken: '0xf0fe2210b4f0c4e3aff7ed147f14980cf14f1114c6ad8fd531ab748ccf33373b::bswt::BSWT',
  amountIn: '10000',
  slippage: 1,
  network: NetworkId.sui,
});
const optimalQuote = quote.data[0];
```

**2.3 .Executing Swap Quotes**

```typescript
const executeSwap = await swap.executeSwapInstruction({
  apiKey: APIKEY,
  quote: optimalQuote,
  address: await swap.signer?.sui?.getAddress(),
});
```

#### 3 . SOLANA :

**3.1 .Initialising The SDK**\`\`\`typescript

```typescript
import { Connection, Keypair, clusterApiUrl } from '@solana/web3.js';
import { SwapAggregator, Environment, NetworkId } from '@kanalabs/aggregator';

const solanaProvider = new Connection(clusterApiUrl('mainnet-beta'));
const solanaSigner = Keypair.fromSecretKey(bs58.decode(process.env.SOLANAPAYER || ''));

const swap = new SwapAggregator(Environment.production, {
  providers: {
    solana: solanaProvider,
  },
  signers: {
    solana: solanaSigner,
  },
});
```

**3.2 .Getting Swap Quotes**

```typescript
const quote = await swap.swapQuotes({
  apiKey: APIKEY,
  inputToken: 'So11111111111111111111111111111111111111112',
  outputToken: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
  amountIn: '1000',
  slippage: 1,
  network: NetworkId.solana,
});
const optimalQuote = quote.data[0];
```

**3.3 .Executing Swap Quotes**

```typescript
const executeSwap = await swap.executeSwapInstruction({
  apiKey: APIKEY,
  quote: optimalQuote,
  address: swap.signer?.solana?.publicKey.toString(),
});
```

#### 4 . EVM CHAINS :

**4.1 .Initialising The SDK**

```typescript
import { ethers, utils } from 'ethers';
import { SwapAggregator, Environment, NetworkId } from '@kanalabs/aggregator';

const polygonRpc = process.env.ETH_NODE_URI_POLYGON as string;
const polygonProvider = ethers.getDefaultProvider(polygonRpc);
const polygonSigner = new ethers.Wallet(privateKey, polygonProvider);

const swap = new SwapAggregator(Environment.production, {
  providers: {
    polygon: polygonProvider,
  },
  signers: {
    polygon: polygonSigner,
  },
});
```

**4.2 .Getting Swap Quotes**

```typescript
const quote = await swap.swapQuotes({
  apiKey: APIKEY,
  inputToken: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
  outputToken: '0xc2132D05D31c914a87C6611C10748AEb04B58e8F',
  amountIn: utils.parseEther('0.05').toString(),
  slippage: 1,
  network: NetworkId.polygon,
});
const optimalQuote = quote.data[0];
```

**4.3 .Executing Swap Quotes**

```typescript
const executeSwap = await swap.executeSwapInstruction({
  apiKey: APIKEY,
  quote: optimalQuote,
  address: await swap.signer?.polygon?.getAddress(),
});
```
