@inkwell.ar/sdk
Version:
SDK for interacting with the Inkwell Blog CRUD AO process using aoconnect for deployment and interactions
169 lines • 7.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultServices = exports.aosConfig = void 0;
exports.deployBlogInBrowser = deployBlogInBrowser;
exports.messageProcessAndGetResult = messageProcessAndGetResult;
const aoconnect_1 = require("@permaweb/aoconnect");
const registry_1 = require("../config/registry");
const access_control_contract_1 = require("./access-control-contract");
const types_1 = require("../types");
const __1 = require("..");
const utils_1 = require("../utils/utils");
const blog_contract_1 = require("./blog-contract");
exports.aosConfig = {
scheduler: '_GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA',
authority: 'fcoN_xJeisVsPXA-trzVAuIiqO3ydLQxM-L4XbrQKzY',
module: 'JArYBF-D8q2OmZ4Mok00sD2Y_6SYEQ7Hjx-6VZ_jl3g',
};
exports.defaultServices = {
gatewayUrl: 'https://arweave.net',
cuUrl: 'https://cu.ao-testnet.xyz',
muUrl: 'https://mu.ao-testnet.xyz',
};
// Browser-compatible deployment function
async function deployBlogInBrowser(options) {
try {
const logger = new __1.Logger({ level: options.logLevel || types_1.LogLevel.WARN });
logger.info(types_1.LogGroup.DEPLOY, `Deploying blog process with name: ${options.name || 'unnamed'}`);
// Check if registry is configured
if (!registry_1.BLOG_REGISTRY_PROCESS_ID ||
registry_1.BLOG_REGISTRY_PROCESS_ID.includes('YOUR_REGISTRY_PROCESS_ID')) {
throw new Error('Registry process ID not configured. Please run the deployment script first: npm run deploy:registry');
}
const aoconnect = options.aoconnect || (0, aoconnect_1.connect)({ MODE: 'legacy' });
logger.debug(types_1.LogGroup.DEPLOY, `Using aoconnect: ${aoconnect}`);
logger.debug(types_1.LogGroup.DEPLOY, `Using aoconnect.spawn: ${aoconnect.spawn}`);
// Get the appropriate signer (similar to blog-sdk.ts getSigner method)
let signer;
if (options.wallet) {
signer = (0, aoconnect_1.createDataItemSigner)(options.wallet);
logger.debug(types_1.LogGroup.DEPLOY, `Using provided wallet for signing`);
}
else {
// Check for browser wallet
if (typeof globalThis !== 'undefined' &&
globalThis.arweaveWallet) {
signer = (0, aoconnect_1.createDataItemSigner)(globalThis.arweaveWallet);
logger.debug(types_1.LogGroup.DEPLOY, `Using browser wallet for signing`);
}
else {
throw new Error('No wallet provided and no browser wallet available. Please provide a wallet or connect a browser wallet.');
}
}
// Replace the registry process ID in the contract
const contractWithRegistry = blog_contract_1.BLOG_CONTRACT;
// const contractWithRegistry = BLOG_CONTRACT.replace(
// '${BLOG_REGISTRY_PROCESS_ID}',
// BLOG_REGISTRY_PROCESS_ID
// );
logger.debug(types_1.LogGroup.DEPLOY, `Using registry process ID: ${registry_1.BLOG_REGISTRY_PROCESS_ID}`);
// Create the complete contract with both modules
const completeContract = [access_control_contract_1.ACCESS_CONTROL_CONTRACT, contractWithRegistry]
.filter(Boolean)
.join('\n\n')
.trim();
logger.debug(types_1.LogGroup.DEPLOY, `Complete contract length: ${completeContract.length}`);
const module = exports.aosConfig.module;
const scheduler = exports.aosConfig.scheduler;
let tags = [
{ name: 'App-Name', value: 'Inkwell-Blog' },
{ name: 'App-Version', value: '1.0.0' },
{ name: 'Name', value: options.name || 'Inkwell Blog' },
{ name: 'Author', value: '@7i7o' },
{ name: 'Registry-Process-ID', value: registry_1.BLOG_REGISTRY_PROCESS_ID },
{ name: 'Authority', value: exports.aosConfig.authority },
];
// Load contract source code when spawning if onBoot is true
const data = options.onBoot ? completeContract : '1984';
if (options.onBoot) {
tags = [...tags, { name: 'On-Boot', value: 'Data' }];
}
// Deploy the contract using aoconnect
const processId = (await (0, utils_1.retryWithDelay)(() => aoconnect.spawn({ module, signer, tags, data, scheduler }), 10, 3000));
logger.info(types_1.LogGroup.DEPLOY, `Deployed empty contract with process ID: ${processId}`);
if (options.pollForSpawn) {
logger.info(types_1.LogGroup.DEPLOY, `Polling for process spawn`);
await (0, utils_1.pollForProcessSpawn)({
processId,
gatewayUrl: exports.defaultServices.gatewayUrl,
});
}
else {
await (0, utils_1.sleep)(5000);
}
let messageId = '';
// Manually load contract source code to process if onBoot is false
if (!options.onBoot) {
try {
const loadContractResult = await messageProcessAndGetResult({
processId,
signer,
tags: [{ name: 'Action', value: 'Eval' }],
data: completeContract,
aoconnect,
});
messageId = loadContractResult.messageId;
}
catch (error) {
logger.error(types_1.LogGroup.DEPLOY, `Failed to load contract to process`, error);
throw new Error(`Failed to load contract to process: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
// Sync initial permissions with registry
try {
const syncResult = await messageProcessAndGetResult({
processId,
signer,
tags: [{ name: 'Action', value: 'Sync-With-Registry' }],
data: '',
aoconnect,
});
logger.info(types_1.LogGroup.DEPLOY, `Initial permissions synced with registry`);
}
catch (syncError) {
logger.warn(types_1.LogGroup.DEPLOY, `⚠️ Failed to sync initial permissions with registry: ${syncError}`);
// Don't fail the deployment if sync fails
}
return {
processId: processId,
messageId,
};
}
catch (error) {
throw new Error(`Failed to deploy Inkwell Blog process: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async function messageProcessAndGetResult(options) {
const { aoconnect = options.aoconnect, processId = options.processId, signer = options.signer, tags = options.tags, data = options.data, } = options;
const messageId = await (0, utils_1.retryWithDelay)(async () => aoconnect.message({
process: processId,
tags,
data,
signer,
}), 10, 3000);
const results = await (0, utils_1.retryWithDelay)(async () => aoconnect.result({
process: processId,
message: messageId,
}), 10, 3000);
const { Output, Error: error } = results;
let errorMessage = null;
if (Output?.data?.output) {
errorMessage = Output.data.output;
}
else if (error) {
if (typeof error === 'object' && Object.keys(error).length > 0) {
errorMessage = JSON.stringify(error);
}
else {
errorMessage = String(error);
}
}
if (errorMessage) {
throw new Error(errorMessage);
}
return {
messageId: messageId,
results,
};
}
//# sourceMappingURL=browser-deploy.js.map