Prerequistes
This guide assumes that you have completed the Setup guide.
Initialize wallets and providers
Initialize wallets and providers only for the chains you need.
Copy
Ask AI
import {
BitcoinProvider,
BitcoinNetwork,
BitcoinWallet,
} from '@gardenfi/core';
import { with0x } from '@gardenfi/utils';
import { createWalletClient, http } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { arbitrumSepolia } from 'viem/chains';
import { RpcProvider, Account } from 'starknet';
import * as anchor from '@coral-xyz/anchor';
import { web3 } from '@coral-xyz/anchor';
// Ethereum wallet setup
const ethereumAccount = privateKeyToAccount(with0x('<YOUR_EVM_PRIVATE_KEY>'));
const ethereumWalletClient = createWalletClient({
account: ethereumAccount,
chain: arbitrumSepolia,
transport: http(),
});
// Starknet wallet setup
const starknetProvider = new RpcProvider(); // Using default RPC URL
const starknetWallet = new Account({
provider: starknetProvider,
signer: <YOUR_STARKNET_PRIVATE_KEY>,
address: <YOUR_STARKNET_ADDRESS>,
});
// Solana wallet setup
const solanaPrivKeyBytes = new Uint8Array('<YOUR_SOLANA_PRIVATE_KEY_IN_BYTES>');
const solanaUser = web3.Keypair.fromSecretKey(solanaPrivKeyBytes);
const solanaConnection = new web3.Connection(RPC_URL, { commitment: 'confirmed' });
const solanaWallet = new anchor.Wallet(solanaUser);
const solanaProvider = new anchor.AnchorProvider(solanaConnection, solanaWallet);
Configure Garden instance
For API key, reach out to us in the Townhall or raise a support ticket.
- Default wallet clients
- Custom client configuration
Copy
Ask AI
import { Garden } from '@gardenfi/core';
import { Network } from '@gardenfi/utils';
const garden = Garden.fromWallets({
environment: Network.TESTNET,
apiKey: '<YOUR_API_KEY>',
wallets: {
evm: ethereumWalletClient,
starknet: starknetWallet,
solana: solanaProvider
}
});
You can initialize the Garden instance with your own HTLC client implementations. Check out the IEVMHTLC, IStarknetHTLC, and ISolanaHTLC interfaces for more details.
Copy
Ask AI
import { Garden } from '@gardenfi/core';
import { Network } from '@gardenfi/utils';
const garden = new Garden({
environment: Network.TESTNET,
apiKey: '<YOUR_API_KEY>',
htlc: {
evm: <IEVMHTLC>,
starknet: <IStarknetHTLC>,
solana: <ISolanaHTLC>
},
});
Create a swap
Copy
Ask AI
import { Quote, SwapParams } from '@gardenfi/core';
import { Assets, ChainAsset } from '@gardenfi/orderbook';
const fromAsset = ChainAsset.from(Assets.ethereum_sepolia.WBTC);
const toAsset = ChainAsset.from(Assets.bitcoin_testnet.BTC);
const sendAmount = '1000000'; // 0.01 WBTC
// Get the quote for the send amount
const quote = await garden.quote.getQuote(
fromAsset,
toAsset,
sendAmount,
false,
{},
);
const receiveAmount = quote.val?.[0].destination.amount;
const solverId = quote.val?.[0].solver_id;
const sourceAddress = ethereumAccount.address;
const destinationAddress = '<YOUR_BITCOIN_ADDRESS';
const swapParams: SwapParams = {
fromAsset,
toAsset,
sendAmount,
receiveAmount,
solverId,
sourceAddress,
destinationAddress
};
// Create the swap
const swapResult = await garden.createSwap(swapParams);
if (!swapResult.ok) {
throw new Error(swapResult.error);
}
console.log('Order created and initiated ✅', swapResult.val);