diff --git a/bun.lockb b/bun.lockb index bb31a5c..718f31e 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index cf90837..e220752 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@inverter-network/sdk", - "version": "0.0.7-alpha.0", + "version": "0.0.7-alpha.1", "description": "INVERTER typescript SDK", "main": "./dist/cjs/index.js", "module": "./dist/index.js", @@ -77,7 +77,7 @@ "abitype": "^1", "eslint": "^8", "typescript": "^5", - "viem": "2.7.6" + "viem": "^2.7.6" }, "dependencies": { "@inverter-network/abis": "0.0.7-alpha.0" diff --git a/src/getWorkflow.ts b/src/getWorkflow.ts index ac7b3a6..2412bfc 100644 --- a/src/getWorkflow.ts +++ b/src/getWorkflow.ts @@ -22,8 +22,7 @@ type WorkflowOrientation = { } export default async function getWorkflow< - // O extends WorkflowOrientation | undefined, - O extends WorkflowOrientation, + O extends WorkflowOrientation | undefined = undefined, W extends WalletClient | undefined = undefined, >({ publicClient, @@ -34,9 +33,7 @@ export default async function getWorkflow< publicClient: PublicClient walletClient?: W orchestratorAddress: Hex - // with optional workflowOrientation TODO - // workflowOrientation?: O - workflowOrientation: O + workflowOrientation?: O }) { if (!publicClient) throw new Error('Public client not initialized') @@ -77,32 +74,78 @@ export default async function getWorkflow< }) // 3. initialize modules with extras - const modules = ( - await Promise.all( - Object.values(workflowOrientation).map(async ({ name, version }) => { - const address = - await orchestrator.read.findModuleAddressInOrchestrator.run(name) + const modules = await (async () => { + // 0. Define the source data based on the optional workflowOrientation + const source = + // 1. Check if workflowOrientation is defined + !!workflowOrientation + ? // 2. If defined, map over the values and find the address of the module + await Promise.all( + Object.values(workflowOrientation).map(async (i) => ({ + ...i, + address: + await orchestrator.read.findModuleAddressInOrchestrator.run( + i.name + ), + })) + ) + : // 3. If not defined, list all modules from the orchestrator for their- + // address then get the title and version + await Promise.all( + (await orchestrator.read.listModules.run()).map(async (address) => { + const flatModule = getModule({ + publicClient, + walletClient, + address, + name: 'Module', + version: 'v1.0', + }), + name = await flatModule.read.title.run(), + [major, minor] = await flatModule.read.version.run(), + version = (() => { + const initialRes = `v${major}.${minor}` + if (initialRes.length > 4) return 'v1.0' + return initialRes + })() - return getModule({ - name, - version, - address, - publicClient, - walletClient, - extras: { - decimals: erc20Decimals, - }, - }) + return { name, version, address } + }) + ) + + console.log('SOURCE: \n', source) + // 4. Map the module array using the source data + const modulesArray = source.map(({ name, version, address }) => + getModule({ + name, + version, + address, + publicClient, + walletClient, + extras: { + decimals: erc20Decimals, + }, }) ) - ).reduce((acc: any, curr) => { - acc[curr.moduleType] = curr - return acc - }, {}) as { - [K in keyof WorkflowOrientation]: ReturnType< - typeof getModule - > - } + + console.log('MODULES ARRAY: \n', modulesArray) + // 5. Reduce the array to an object with the moduleType as key + const result = modulesArray.reduce((acc: any, curr) => { + acc[curr.moduleType] = curr + return acc + }, {}) as { + [K in keyof WorkflowOrientation]: O extends NonNullable + ? ReturnType> + : ReturnType< + typeof getModule< + WorkflowOrientation[K]['name'], + WorkflowOrientation[K]['version'], + W + > + > + } + + return result + })() // RETURN WORKFLOW CONFIG const returns = { @@ -115,31 +158,3 @@ export default async function getWorkflow< return returns } - -// with optional workflowOrientation TODO -// const addressAndVersions = (async () => { -// if (!workflowOrientation) -// return await Promise.all( -// (await orchestrator.read.listModules.run()).map(async (address) => { -// const contract = getContract({ -// client, -// address, -// abi: FlatModule_ABI, -// }) - -// const name = await contract.read.title(), -// version = await contract.read.version() - -// return { name, version, address } -// }) -// ) - -// return await Promise.all( -// Object.values(workflowOrientation).map(async ({ name, version }) => { -// const address = -// await orchestrator.read.findModuleAddressInOrchestrator.run(name) - -// return { name, version, address } -// }) -// ) -// })()