From 436863a0bb4d79aa09e4f558b27534c7de7b3617 Mon Sep 17 00:00:00 2001 From: Tyler Hall Date: Tue, 12 Dec 2023 21:19:41 +0000 Subject: [PATCH] refactor(sdk)!: post process and message to root of MU #208 --- sdk/src/client/ao-mu.js | 58 +++++++++++++++++++++++++++++++++++- sdk/src/client/ao-mu.test.js | 44 +++++++++++++++++++++++++-- sdk/src/index.common.js | 9 ++---- 3 files changed, 101 insertions(+), 10 deletions(-) diff --git a/sdk/src/client/ao-mu.js b/sdk/src/client/ao-mu.js index e004c10a2..e3a498ce3 100644 --- a/sdk/src/client/ao-mu.js +++ b/sdk/src/client/ao-mu.js @@ -39,7 +39,7 @@ export function deployMessageWith ({ fetch, MU_URL, logger: _logger }) { of(signedDataItem) .chain(fromPromise(async (signedDataItem) => fetch( - `${MU_URL}/message`, + MU_URL, { method: 'POST', headers: { @@ -67,3 +67,59 @@ export function deployMessageWith ({ fetch, MU_URL, logger: _logger }) { .toPromise() } } + +/** + * @typedef Env3 + * @property {fetch} fetch + * @property {string} MU_URL + * + * @typedef RegisterProcess + * @property { any } signedData - DataItem returned from arbundles createData + * + * @callback RegisterProcess + * @returns {Promise} + * + * @param {Env3} env + * @returns {RegisterProcess} + */ +export function deployProcessWith ({ fetch, MU_URL, logger: _logger }) { + const logger = _logger.child('deployProcess') + + return (args) => { + return of(args) + /** + * Sign with the provided signer + */ + .chain(fromPromise(({ data, tags, signer }) => signer({ data, tags }))) + .chain(signedDataItem => + of(signedDataItem) + .chain(fromPromise(async (signedDataItem) => + fetch( + MU_URL, + { + method: 'POST', + headers: { + 'Content-Type': 'application/octet-stream', + Accept: 'application/json' + }, + body: signedDataItem.raw + } + ) + )).bichain( + err => Rejected(new Error(`Error while communicating with MU: ${JSON.stringify(err)}`)), + fromPromise( + async res => { + if (res.ok) return res.json() + throw new Error(`${res.status}: ${await res.text()}`) + } + ) + ) + .bimap( + logger.tap('Error encountered when deploying process via MU'), + logger.tap('Successfully deployed process via MU') + ) + .map(res => ({ res, processId: signedDataItem.id })) + ) + .toPromise() + } +} diff --git a/sdk/src/client/ao-mu.test.js b/sdk/src/client/ao-mu.test.js index 79fcb3fc2..028b33ded 100644 --- a/sdk/src/client/ao-mu.test.js +++ b/sdk/src/client/ao-mu.test.js @@ -2,8 +2,8 @@ import { describe, test } from 'node:test' import * as assert from 'node:assert' import { createLogger } from '../logger.js' -import { deployMessageSchema, signerSchema } from '../dal.js' -import { deployMessageWith } from './ao-mu.js' +import { deployMessageSchema, deployProcessSchema, signerSchema } from '../dal.js' +import { deployMessageWith, deployProcessWith } from './ao-mu.js' const MU_URL = globalThis.MU_URL || 'https://ao-mu-1.onrender.com' const logger = createLogger('@permaweb/ao-sdk:readState') @@ -16,7 +16,7 @@ describe('ao-mu', () => { MU_URL, logger, fetch: async (url, options) => { - assert.equal(url, `${MU_URL}/message`) + assert.equal(url, MU_URL) assert.deepStrictEqual(options, { method: 'POST', headers: { @@ -60,4 +60,42 @@ describe('ao-mu', () => { }) }) }) + + describe('deployProcessWith', () => { + test('register the contract, and return the id', async () => { + const deployProcess = deployProcessSchema.implement( + deployProcessWith({ + MU_URL, + logger, + fetch: async (url, options) => { + assert.equal(url, MU_URL) + assert.deepStrictEqual(options, { + method: 'POST', + headers: { + 'Content-Type': 'application/octet-stream', + Accept: 'application/json' + }, + body: 'raw-buffer' + }) + + return new Response(JSON.stringify({ foo: 'bar' })) + } + }) + ) + + await deployProcess({ + data: '1234', + tags: [{ name: 'foo', value: 'bar' }], + signer: signerSchema.implement( + async ({ data, tags }) => { + assert.ok(data) + assert.deepStrictEqual(tags, [ + { name: 'foo', value: 'bar' } + ]) + return { id: 'data-item-123', raw: 'raw-buffer' } + } + ) + }) + }) + }) }) diff --git a/sdk/src/index.common.js b/sdk/src/index.common.js index 2808c2dbc..3fbaf998b 100644 --- a/sdk/src/index.common.js +++ b/sdk/src/index.common.js @@ -70,17 +70,14 @@ export function connect ({ }) /** - * default createContract that works OOTB + * default spawn that works OOTB * - Verifies the inputs - * - Creates the process - * - In browser, uses Arweave Wallet to upload process to Irys (via dispatch) - * - On server, uses Irys Node to upload process to Irys - * - Registers the Process with SU + * - spawns the process via the MU */ const spawnLogger = logger.child('spawn') const spawn = spawnWith({ loadTransactionMeta: GatewayClient.loadTransactionMetaWith({ fetch, GATEWAY_URL }), - deployProcess: SuClient.deployProcessWith({ fetch, SU_URL, logger: spawnLogger }), + deployProcess: MuClient.deployProcessWith({ fetch, MU_URL, logger: spawnLogger }), logger: spawnLogger })