sei-agent-kit
Version:
A package for building AI agents on the SEI blockchain
54 lines (52 loc) • 2.88 kB
JavaScript
import { Toolkit } from "@bancor/carbon-sdk/strategy-management";
import { initSyncedCache } from "@bancor/carbon-sdk/chain-cache";
import { ContractsApi, } from "@bancor/carbon-sdk/contracts-api";
import { JsonRpcProvider } from "@ethersproject/providers";
import { SEI_RPC_URL, MAX_BLOCK_AGE } from "../../constants";
import { carbonERC20InfiniteApproval, getOverlappingStrategyParams, } from "./utils";
/**
*/
export async function createOverlappingStrategy(agent, config, baseToken, quoteToken, buyPriceLow, sellPriceHigh, buyBudget, sellBudget, fee, range, marketPriceOverride, overrides) {
const provider = new JsonRpcProvider(SEI_RPC_URL);
const api = new ContractsApi(provider, config);
const { cache } = initSyncedCache(api.reader, undefined, MAX_BLOCK_AGE);
const carbonSDK = new Toolkit(api, cache, undefined);
const { buyPriceLow: parsedBuyPriceLow, buyPriceMarginal, buyPriceHigh, buyBudget: parsedBuyBudget, sellPriceLow, sellPriceMarginal, sellPriceHigh: parsedSellPriceHigh, sellBudget: parsedSellBudget, } = await getOverlappingStrategyParams(carbonSDK, baseToken, quoteToken, buyPriceLow, sellPriceHigh, buyBudget, sellBudget, fee, range, marketPriceOverride);
// 1. Approve required tokens
console.log(`Setting approval for ${baseToken} and ${quoteToken}`);
const carbonController = config.carbonControllerAddress;
if (!carbonController) {
throw new Error("Carbon Controller Address cannot be undefined");
}
carbonERC20InfiniteApproval(agent, baseToken, carbonController);
carbonERC20InfiniteApproval(agent, quoteToken, carbonController);
console.log(`
Creating Overlapping Strategy
baseToken is ${baseToken}
quoteToken is ${quoteToken}
buyBudget is ${parsedBuyBudget}
buyPriceLow is ${parsedBuyPriceLow}
buyPriceMarginal is ${buyPriceMarginal}
buyPriceHigh is ${buyPriceHigh}
sellBudget is ${parsedSellBudget}
sellPriceLow is ${sellPriceLow}
sellPriceMarginal is ${sellPriceMarginal}
sellPriceHigh is ${parsedSellPriceHigh}
`);
// 2. Create strategy populated tx
const populatedTx = await carbonSDK.createBuySellStrategy(baseToken, quoteToken, parsedBuyPriceLow, buyPriceMarginal, buyPriceHigh, parsedBuyBudget, sellPriceLow, sellPriceMarginal, parsedSellPriceHigh, parsedSellBudget, overrides);
const viemTx = {
chain: agent.walletClient.chain,
account: agent.walletClient.account,
to: populatedTx.to,
data: populatedTx.data,
value: populatedTx.value ? BigInt(populatedTx.value.toString()) : 0n,
gas: populatedTx.gasLimit
? BigInt(populatedTx.gasLimit.toString())
: undefined,
nonce: populatedTx.nonce,
};
const txHash = await agent.walletClient.sendTransaction(viemTx);
return txHash;
}
//# sourceMappingURL=createOverlappingStrategy.js.map