From 856cc6753decef5b483eb3f65dfc1755c733a072 Mon Sep 17 00:00:00 2001 From: Tyler Hall Date: Tue, 12 Dec 2023 21:17:02 +0000 Subject: [PATCH 1/2] feat(sdk)!: messageId -> message on result api --- sdk/README.md | 2 +- sdk/src/lib/result/index.js | 8 ++++---- sdk/src/lib/spawn/verify-inputs.js | 4 +++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/sdk/README.md b/sdk/README.md index 8ce1a04b2..e08363b47 100644 --- a/sdk/README.md +++ b/sdk/README.md @@ -54,7 +54,7 @@ Read the result of the message evaluation from an `ao` Compute Unit `cu` import { result } from "@permaweb/ao-sdk"; let { messages, spawns, output, error } = await result({ - messageId: "VkjFCALjk4xxuCilddKS8ShZ-9HdeqeuYQOgMgWucro", + message: "VkjFCALjk4xxuCilddKS8ShZ-9HdeqeuYQOgMgWucro", }); ``` diff --git a/sdk/src/lib/result/index.js b/sdk/src/lib/result/index.js index dd30916a5..587f6ffb0 100644 --- a/sdk/src/lib/result/index.js +++ b/sdk/src/lib/result/index.js @@ -13,7 +13,7 @@ import { readWith } from './read.js' * @property {string} [error] * * @typedef ReadResultArgs - * @property {string} messageId - the transaction id of the message + * @property {string} message - the transaction id of the message * * @callback ReadResult * @param {ReadResultArgs} args @@ -25,14 +25,14 @@ export function resultWith (env) { const verifyInput = verifyInputWith(env) const read = readWith(env) - return ({ messageId }) => { - return of({ id: messageId }) + return ({ message }) => { + return of({ id: message }) .chain(verifyInput) .chain(read) .map( env.logger.tap( 'readResult result for message "%s": %O', - messageId + message ) ) .map(result => result) diff --git a/sdk/src/lib/spawn/verify-inputs.js b/sdk/src/lib/spawn/verify-inputs.js index 7781761d1..a7857ad9a 100644 --- a/sdk/src/lib/spawn/verify-inputs.js +++ b/sdk/src/lib/spawn/verify-inputs.js @@ -23,8 +23,10 @@ const checkTag = (name, pred, err) => tags => pred(tags[name]) */ function verifyModuleWith ({ loadTransactionMeta, logger }) { + loadTransactionMeta = fromPromise(loadTransactionMetaSchema.implement(loadTransactionMeta)) + return (module) => of(module) - .chain(fromPromise(loadTransactionMetaSchema.implement(loadTransactionMeta))) + .chain(loadTransactionMeta) .map(prop('tags')) .map(parseTags) /** From 5b87d5d5707af83013bd17a6c72276d1985f1333 Mon Sep 17 00:00:00 2001 From: Tyler Hall Date: Tue, 12 Dec 2023 21:19:41 +0000 Subject: [PATCH 2/2] 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 })