Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sdk)!: post process and message to root of MU #208 #218

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
```

Expand Down
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
8 changes: 4 additions & 4 deletions sdk/src/lib/result/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/lib/spawn/verify-inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/**
Expand Down