@symmetry-hq/agents-sdk
Version:
Symmetry Agents SDK
123 lines (122 loc) • 6.15 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgentsSDK = void 0;
const web3_js_1 = require("@solana/web3.js");
const uuid_1 = require("uuid");
// Local imports
const constants_1 = require("./constants");
const createAgent_1 = require("./instructions/createAgent");
const topUpBalance_1 = require("./instructions/topUpBalance");
const agent_1 = require("./state/agent");
const programAccounts_1 = require("./utils/programAccounts");
const txUtils_1 = require("./utils/txUtils");
class AgentsSDK {
constructor(params) {
var _a, _b;
this.sdkParams = {
payer: (_a = params.payer) !== null && _a !== void 0 ? _a : web3_js_1.Keypair.generate().publicKey,
connection: new web3_js_1.Connection(params.RPC_URL, "confirmed"),
program: (0, programAccounts_1.getAgentsProgram)(new web3_js_1.Connection(params.RPC_URL, "confirmed")),
priorityFee: (_b = params.priorityFee) !== null && _b !== void 0 ? _b : constants_1.PRIORITY_FEE,
};
}
setPayer(payer) {
return __awaiter(this, void 0, void 0, function* () {
this.sdkParams.payer = payer;
});
}
createAgent(params) {
return __awaiter(this, void 0, void 0, function* () {
let agentUuid = (0, programAccounts_1.getRandomSeed)();
let agentAddress = (0, programAccounts_1.getAgent)(agentUuid);
let agentState = (0, programAccounts_1.getAgentStateAccount)(agentAddress);
const ix = yield (0, createAgent_1.createAgentIx)({
payer: this.sdkParams.payer,
program: this.sdkParams.program,
agentUuid,
agentAddress,
agentState,
});
return Object.assign(Object.assign({}, (yield (0, txUtils_1.prepareV0Transactions)({
connection: this.sdkParams.connection,
payer: this.sdkParams.payer,
priorityFee: this.sdkParams.priorityFee,
multipleIxs: [[ix]],
multipleLookupTableAddresses: [[]],
signers: [[]],
batches: [1],
}))), { agentUuid: (0, uuid_1.stringify)(Uint8Array.from(agentUuid)), agent: agentAddress.toBase58(), agentState: agentState.toBase58() });
});
}
topUpBalance(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const ix = yield (0, topUpBalance_1.topUpBalanceIx)({
payer: this.sdkParams.payer,
program: this.sdkParams.program,
amount: params.amount,
mint: (_a = params.mint) !== null && _a !== void 0 ? _a : constants_1.USDC_MINT,
});
return yield (0, txUtils_1.prepareV0Transactions)({
connection: this.sdkParams.connection,
payer: this.sdkParams.payer,
priorityFee: this.sdkParams.priorityFee,
multipleIxs: [[ix]],
multipleLookupTableAddresses: [[]],
signers: [[]],
batches: [1],
});
});
}
static validateTopUpTx(params) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, programAccounts_1.getTopUpBalanceEventFromTx)(new web3_js_1.Connection(params.RPC_URL, "confirmed"), (0, programAccounts_1.getAgentsProgram)(new web3_js_1.Connection(params.RPC_URL, "confirmed")), params.txId);
});
}
validateTopUpTx(txId) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, programAccounts_1.getTopUpBalanceEventFromTx)(this.sdkParams.connection, this.sdkParams.program, txId);
});
}
static validateCreateAgentTx(params) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, programAccounts_1.getCreateAgentEventFromTx)(new web3_js_1.Connection(params.RPC_URL, "confirmed"), (0, programAccounts_1.getAgentsProgram)(new web3_js_1.Connection(params.RPC_URL, "confirmed")), params.txId);
});
}
validateCreateAgentTx(txId) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, programAccounts_1.getCreateAgentEventFromTx)(this.sdkParams.connection, this.sdkParams.program, txId);
});
}
getAgentState(uuid) {
return __awaiter(this, void 0, void 0, function* () {
let agentState = yield (0, agent_1.fetchAgentState)(this.sdkParams.program, (0, programAccounts_1.getAgentStateAccount)((0, programAccounts_1.getAgent)(Array.from((0, uuid_1.parse)(uuid)))));
return (0, agent_1.parseAgentState)(agentState);
});
}
getAgentsByCreator(creator) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, programAccounts_1.getAgentsByCreator)(this.sdkParams.program, new web3_js_1.PublicKey(creator));
});
}
getAllAgents() {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, programAccounts_1.getAllAgents)(this.sdkParams.program);
});
}
sendSignedVersionedTxs(txs_1) {
return __awaiter(this, arguments, void 0, function* (txs, simulateTransactions = false) {
return yield (0, txUtils_1.sendV0Transactions)(this.sdkParams.connection, txs, simulateTransactions);
});
}
}
exports.AgentsSDK = AgentsSDK;