Skip to content

Commit

Permalink
refactor(sdk)!: post process and message to root of MU #208
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Dec 12, 2023
1 parent 95fb277 commit 436863a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 10 deletions.
58 changes: 57 additions & 1 deletion sdk/src/client/ao-mu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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<Record<string, any>}
*
* @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()
}
}
44 changes: 41 additions & 3 deletions sdk/src/client/ao-mu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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: {
Expand Down Expand Up @@ -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' }
}
)
})
})
})
})
9 changes: 3 additions & 6 deletions sdk/src/index.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down

0 comments on commit 436863a

Please sign in to comment.