0.7.9 • Published 5 days ago

@kanalabs/aggregator v0.7.9

Weekly downloads
-
License
ISC
Repository
-
Last release
5 days ago

Installation

Using NPM: click to redirect

 npm i @kanalabs/aggregator

Using Yarn: click to redirect

 yarn add @kanalabs/aggregator

TypeDoc:

kanalabs aggregator SDK type doc - click to redirect

Supported Chains

  1. Aptos
  2. Arbitrum
  3. Avalanche
  4. BSC (Binance Smart Chain)
  5. Ethereum
  6. Polygon
  7. Solana
  8. Sui
  9. zkSync Era

Supported Bridges

  1. Wormhole
  2. LayerZero
  3. CCTP

Usage

Same Chain Swap

1. SwapQuotes function

 swapQuotes = async (params: SameChainSwapParams)
SameChainSwapParamsDescription
inputTokenThe input token for the swap operation.
outputTokenThe output token to receive after the swap.
amountInThe amount of input tokens to be swapped.
slippageThe allowed slippage percentage for the swap.
networkThe network ID for the swap operation.
options(Optional) Additional options for the swap.

2. ExecuteSwapInstruction

executeSwapInstruction = async (params: SameChainInstructionParams)
executeSwapInstructionDescription
quoteA CommonRouteInterface representing the quote response format.
addressThe 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.

1 . APTOS :

1.1 .Initialising The SDK

const aptosProvider = new AptosClient('RPC ENDPOINT');

const aptosSigner = AptosAccount.fromAptosAccountObject({
  address: process.env.APTOS_ADDRESS || '',
  publicKeyHex: process.env.APTOS_PUBLICKEY || '',
  privateKeyHex: process.env.APTOS_PRIVATEKEY || '',
});

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

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,
});

const optimalQuote = quote.data[0];

1.3 .Executing Swap Quotes

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

2 . SUI :

2.1 .Initialising The SDK

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

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

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

3 . SOLANA :

3.1 .Initialising The SDK

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

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

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

4 . EVM CHAINS :

4.1 .Initialising The SDK

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

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

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

Cross Chain Swap

1. CrossChainQuote

crossChainQuote = async (params: CrossChainSwapParams)
CrossChainSwapParamsDescription
sourceTokenThe source token to be swapped.
targetTokenThe target token to receive after the swap.
sourceChainThe source network ID or chain for the swap.
targetChainThe target network ID or chain for the swap.
amountInThe amount of source tokens to be swapped.
sourceSlippageThe allowed slippage percentage on the source chain.
targetSlippageThe allowed slippage percentage on the target chain.
options(Optional) An object containing additional options.

2. Execute Cross Chain Transfer

executeTransfer = async (params: ExecuteCrossChainTransferParams)
CrossChainTransferParamsDescription
sourceProviderRPC Provider for source chain
sourceSignerSource chain signer
quoteselected cross chain swap quote
sourceAddresssource chain user address
targetAddressTarget chain user address
targetSignertarget chain signer.
targetProviderRpc provider for target chain.

3. Execute Cross Chain Claim

executeClaim = async (params: ExecuteCrossChainClaimParams)
CrossChainSwapParamsDescription
txHashTransaction hash of the tranfer transaction in source chain
sourceProviderRPC Provider for source chain
targetProviderRpc provider for target chain.
targetSignertarget chain signer.
quoteselected cross chain swap quote
sourceAddresssource chain user address
targetAddressTarget chain user address

A CrossChain swap consists of three steps

  1. Getting Swap Quotes: This step involves obtaining quotes for the selected chains and tokens.
  1. Executing Transfer Instruction: In this phase, tokens are transferred across chains using a bridge to reach the selected destination chain.
  1. Executing Claim Instruction: After the tokens have been successfully bridged to the destination chain, this step allows users to claim their tokens. It's worth noting that bridges utilizing a relayer may not require this specific claim step.

What if the claim fails ?

In the event of a failure, users can redeem their tokens that were transferred via the bridge. Kanalabs exclusively transfers native stable coins / wrapped stable coins via the bridge. Therefore, users have the option to redeem their tokens and manually initiate the swap on the destination chain."

1 . POLYGON TO BSC

1.1 . Initialising SDK

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

const bscRpc = process.env.ETH_NODE_URI_BSC as string;
const bscProvider = ethers.getDefaultProvider(bscRpc);
const bscSigner = new ethers.Wallet(privateKey, bscProvider);

const crossChainAggregator = new SwapAggregator(Environment.production);

1.2 . Getting Cross Chain Swap Quote

const quotes = await crossChainAggregator.crossChainQuote({
  sourceToken: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
  targetToken: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
  sourceChain: NetworkId.polygon,
  targetChain: NetworkId.bsc,
  amountIn: utils.parseUnits('0.01', 18).toString(),
  sourceSlippage: 0.1,
  targetSlippage: 0.1,
});

const optimalQuote = quotes.data[0];

1.3 . Executing Transfer Instruction

const transfer = await crossChainAggregator.executeTransfer({
  quote: optimalQuote,
  sourceAddress: polygonSigner.getAddress() as string,
  targetAddress: bscSigner.getAddress() as string,
  sourceProvider: polygonProvider as providers.BaseProvider,
  sourceSigner: polygonSigner as EvmSignerType,
});

For Bridges using relayers there is no need to execute claim Instruction

1.4 . Execute Claim Instruction

const claim = await crossChainAggregator.executeClaim({
  txHash: transfer.txHash,
  sourceProvider: polygonProvider as providers.BaseProvider,
  targetProvider: bscProvider as providers.BaseProvider,
  targetSigner: bscSigner as EvmSignerType,
  quote: optimalQuote,
  sourceAddress: polygonSigner.getAddress() as string,
  targetAddress: bscSigner.getAddress() as string,
});

1.5 . Redeeming tokens in case of failure

const redeem = await crossChainAggregator.redeem({
  sourceChain: NetworkId.polygon,
  targetChain: NetworkId.bsc,
  sourceProvider: polygonProvider as providers.BaseProvider,
  targetProvider: bscProvider as providers.BaseProvider,
  targetSigner: bscSigner as EvmSignerType,
  SourceHash: transfer.txHash,
  targetAddress: bscSigner.getAddress() as string,
  BridgeId: BridgeId.wormhole,
});

2 . APTOS TO SUI

2.1 . Initialising SDK

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

const suiSigner = new RawSigner(Ed25519Keypair.deriveKeypair(process.env.SUIMNEMONICS || ''), suiProvider);
const aptosSigner = AptosAccount.fromAptosAccountObject({
  address: process.env.APTOS_ADDRESS || '',
  publicKeyHex: process.env.APTOS_PUBLICKEY || '',
  privateKeyHex: process.env.APTOS_PRIVATEKEY || '',
});

const crossChainAggregator = new SwapAggregator(Environment.production);

2.2 . Getting Cross Chain Swap Quote

const crossChainQuotes = await crossChainAggregator.crossChainQuote({
  sourceToken,
  targetToken,
  sourceChain: NetworkId.aptos,
  targetChain: NetworkId.sui,
  amountIn: '100000',
  sourceSlippage: 0.1,
  targetSlippage: 0.1,
});
const optimalRoute = crossChainQuotes.data[0];

2.3 . Executing Transfer Instruction

const transfer = await crossChainAggregator.executeTransfer({
  sourceProvider: aptosProvider,
  sourceAddress: aptosSigner.address().toString(),
  sourceSigner: aptosSigner,
  quote: optimalRoute,
  targetAddress: await suiSigner.getAddress(),
});

For Bridges using relayers there is no need to execute claim Instruction

2.4 . Execute Claim Instruction

const claim = await crossChainAggregator.executeClaim({
  txHash: transfer.txHash,
  sourceProvider: aptosProvider,
  targetProvider: suiProvider,
  targetSigner: suiSigner,
  quote: optimalRoute,
  sourceAddress: aptosSigner.address().toString(),
  targetAddress: await suiSigner.getAddress(),
});

2.5 . Redeeming tokens in case of failure

const redeem = await crossChainAggregator.redeem({
  sourceChain: NetworkId.aptos,
  targetChain: NetworkId.sui,
  sourceProvider: aptosProvider,
  targetProvider: suiProvider,
  targetSigner: suiSigner,
  SourceHash: transfer.txHash,
  targetAddress: await suiSigner.getAddress(),
  BridgeId: BridgeId.wormhole,
});

3 . SOLANA TO APTOS

3.1 . Initialising SDK

const solanaProvider = new Connection('SOLANA RPC NODE);
const solanaSigner = Keypair.fromSecretKey(bs58.decode(process.env.SOLANAPAYER || ''));
const aptosProvider = new AptosClient('APTOS RPC NODE');

const aptosSigner = AptosAccount.fromAptosAccountObject({
  address: process.env.APTOS_ADDRESS || '',
  publicKeyHex: process.env.APTOS_PUBLICKEY || '',
  privateKeyHex: process.env.APTOS_PRIVATEKEY || '',
});

const crossChainAggregator = new SwapAggregator(Environment.production);

3.2 . Getting Cross Chain Swap Quote

const crossChainQuotes = await crossChainAggregator.crossChainQuote({
  'So11111111111111111111111111111111111111112',
  '0x1::aptos_coin::AptosCoin',
  sourceChain: NetworkId.solana,
  targetChain: NetworkId.aptos,
  amountIn: '100000',
  sourceSlippage: 0.5,
  targetSlippage: 0.1,
});
const optimalRoute = crossChainQuotes.data[0];

3.3 . Executing Transfer Instruction

const transfer = await crossChainAggregator.executeTransfer({
  sourceProvider: solanaProvider,
  sourceAddress: solanaSigner.publicKey.toString(),
  sourceSigner: solanaSigner,
  quote: optimalRoute,
  targetAddress: aptosSigner.address().toString(),
});

For Bridges using relayers there is no need to execute claim Instruction

3.4 . Execute Claim Instruction

const claim = await crossChainAggregator.executeClaim({
  txHash: transfer.txHash,
  sourceProvider: solanaProvider,
  targetProvider: aptosProvider,
  targetSigner: aptosSigner,
  quote: optimalRoute,
  sourceAddress: solanaSigner.publicKey.toString(),
  targetAddress: aptosSigner.address().toString(),
});

3.5 . Redeeming tokens in case of failure

const redeem = await crossChainAggregator.redeem({
  sourceChain: NetworkId.solana,
  targetChain: NetworkId.aptos,
  sourceProvider: solanaProvider,
  targetProvider: aptosProvider,
  targetSigner: aptosSigner,
  SourceHash: transfer.txHash,
  targetAddress: aptosSigner.address().toString(),
  BridgeId: BridgeId.wormhole,
});
0.7.9-beta.1

5 days ago

0.7.9-beta.0

9 days ago

0.7.9

17 days ago

0.7.8

18 days ago

0.7.7-beta.1

19 days ago

0.7.7-beta.0

23 days ago

0.7.7

29 days ago

0.7.6

1 month ago

0.7.6-beta.1

1 month ago

0.7.6-beta.0

2 months ago

0.7.5-beta.0

2 months ago

0.7.3-beta.3

2 months ago

0.7.3-beta.1

2 months ago

0.7.3

2 months ago

0.7.3-beta.0

2 months ago

0.7.2-beta.0

3 months ago

0.7.2-beta-1

3 months ago

0.7.2

3 months ago

0.7.1

3 months ago

0.7.0

3 months ago

0.6.0-beta-36

3 months ago

0.6.0-beta-35

3 months ago

0.6.0-beta-34

3 months ago

0.6.0-beta-33

3 months ago

0.6.0-beta-32

3 months ago

0.6.0-beta-31

3 months ago

0.6.0-beta-30

3 months ago

0.6.0-beta-29

4 months ago

0.6.0-beta-28

4 months ago

0.6.0-beta-27

4 months ago

0.6.0-beta-26

5 months ago

0.6.0-beta-25

5 months ago

0.6.0-beta-24

5 months ago

0.6.0-beta-23

5 months ago

0.6.0-beta-22

5 months ago

0.6.0-beta-18

5 months ago

0.6.0-beta-19

5 months ago

0.6.0-beta-21

5 months ago

0.6.0-beta-20

5 months ago

0.6.0-beta-17

5 months ago

0.6.0-beta-16

5 months ago

0.6.0-beta-14

5 months ago

0.6.0-beta-15

5 months ago

0.6.0-beta-13

5 months ago

0.6.0-beta-12

5 months ago

0.6.0-beta-11

5 months ago

0.6.0-beta-10

5 months ago

0.6.0-beta-9

6 months ago

0.6.0-beta-8

6 months ago

0.6.0-beta-7

6 months ago

0.6.0-beta-6

6 months ago

0.6.0-beta-5

6 months ago

0.6.0-beta-4

6 months ago

0.6.0-beta-3

6 months ago

0.6.0-beta-2

6 months ago

0.6.0-beta-1

6 months ago

0.7.0-beta-1

6 months ago

0.6.0

7 months ago

0.5.0

7 months ago

0.4.0

8 months ago

0.3.0

8 months ago

0.2.0

8 months ago

0.1.0

8 months ago