@coinbase/agentkit
Version:
Coinbase AgentKit core primitives
82 lines (81 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractCreatedSuperTokenAddressAbi = extractCreatedSuperTokenAddressAbi;
const constants_1 = require("../constants");
const viem_1 = require("viem");
/**
* Casts the topics to a tuple
*
* @param topics - the topics to search for
* @returns - A tuple of topics
*/
function asTopicTuple(topics) {
if (!topics.length) {
throw new Error("Log has no topics (anonymous event?)");
}
return [topics[0], ...topics.slice(1)];
}
/**
* Checks for records to avoid any cast
*
* @param value - The value to check
* @returns - A boolean if the record is valid
*/
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
const TOKEN_KEYS = ["token", "superToken", "proxy"];
/**
* Pulls a 0x-address string from decoded args under one of: token | superToken | proxy
*
* @param args - The unknown value to check
* @returns - A contract address of the super token, or null
*/
function pickTokenLike(args) {
if (!isRecord(args))
return null;
for (const key of TOKEN_KEYS) {
const v = args[key];
if (typeof v === "string" && v.startsWith("0x")) {
return v;
}
}
return null;
}
/**
* Extracts the super token address using the contract abi
*
* @param receipt - the transaction receipt from creating the super token
* @param factoryAddress - the address of the factory that created the super token
* @returns - The contract address of the created Super Token
*/
function extractCreatedSuperTokenAddressAbi(receipt, factoryAddress) {
const factory = factoryAddress.toLowerCase();
if (!factory)
throw new Error("Missing factory address");
console.log("factory", factory);
console.log("receipt.logs", receipt.logs);
for (const log of receipt.logs) {
if (log.address.toLowerCase() !== factory)
continue;
if (!log.topics.length)
continue; // skip anonymous logs
try {
const { eventName, args } = (0, viem_1.decodeEventLog)({
abi: constants_1.SuperTokenFactoryABI,
data: log.data,
topics: asTopicTuple(log.topics),
});
if (eventName === "SuperTokenCreated") {
const token = pickTokenLike(args);
if (!token)
throw new Error("Token arg missing on SuperTokenCreated");
return (0, viem_1.getAddress)(token);
}
}
catch {
// not a matching event for this signature — ignore
}
}
throw new Error("SuperTokenCreated not found on factory logs");
}