sei-agent-kit
Version:
A package for building AI agents on the SEI blockchain
58 lines (56 loc) • 2.79 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, getStrategyTypeParams, } from "./utils";
/**
*/
export async function createBuySellStrategy(agent, config, type, baseToken, quoteToken, buyRange, sellRange, buyBudget, sellBudget, 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, buyPriceMarginal, buyPriceHigh, buyBudget: parsedBuyBudget, sellPriceLow, sellPriceMarginal, sellPriceHigh, sellBudget: parsedSellBudget, } = await getStrategyTypeParams(type, baseToken, quoteToken, buyBudget, sellBudget, buyRange, sellRange);
// 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");
}
if (sellBudget !== "0") {
carbonERC20InfiniteApproval(agent, baseToken, carbonController);
}
if (buyBudget !== "0") {
carbonERC20InfiniteApproval(agent, quoteToken, carbonController);
}
// 2. Create strategy populated tx
console.log(`
Creating Buy/Sell Strategy
baseToken is ${baseToken}
quoteToken is ${quoteToken}
buyBudget is ${parsedBuyBudget}
buyPriceLow is ${buyPriceLow}
buyPriceMarginal is ${buyPriceMarginal}
buyPriceHigh is ${buyPriceHigh}
sellBudget is ${parsedSellBudget}
sellPriceLow is ${sellPriceLow}
sellPriceMarginal is ${sellPriceMarginal}
sellPriceHigh is ${sellPriceHigh}
`);
const populatedTx = await carbonSDK.createBuySellStrategy(baseToken, quoteToken, buyPriceLow, buyPriceMarginal, buyPriceHigh, parsedBuyBudget, sellPriceLow, sellPriceMarginal, sellPriceHigh, 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=createBuySellStrategy.js.map