From f923e88b98c056736ca5e816a44dcc2797c2beb1 Mon Sep 17 00:00:00 2001 From: nhenin Date: Sun, 27 Aug 2023 17:27:21 +0200 Subject: [PATCH] [runtime-lifecycle] added payouts logic --- Readme.md | 2 +- package.json | 4 +- packages/runtime/api/Readme.md | 2 - packages/runtime/api/src/apis/runtime.ts | 13 -- packages/runtime/api/src/index.ts | 4 - .../api/src/instance/overRestAPI/runtime.ts | 63 ---------- .../runtime/client/rest/src/contract/index.ts | 3 + .../client/rest/src/contract/withdrawal/id.ts | 17 --- packages/runtime/client/rest/src/index.ts | 9 +- .../runtime/client/rest/src/payout/details.ts | 6 +- .../rest/src/payout/endpoints/collection.ts | 4 +- .../runtime/client/rest/src/payout/header.ts | 3 +- .../runtime/client/rest/src/payout/index.ts | 3 + .../src/{contract => }/withdrawal/details.ts | 8 +- .../withdrawal/endpoints/collection.ts | 8 +- .../withdrawal/endpoints/singleton.ts | 3 +- .../src/{contract => }/withdrawal/header.ts | 5 +- .../src/{contract => }/withdrawal/index.ts | 1 - packages/runtime/core/src/asset/index.ts | 17 ++- packages/runtime/core/src/index.ts | 2 +- packages/runtime/core/src/payout/id.ts | 23 ---- packages/runtime/core/src/payout/index.ts | 53 +++++++++ packages/runtime/core/src/policyId.ts | 2 +- .../runtime/{api => lifecycle}/.npmignore | 0 packages/runtime/lifecycle/Readme.md | 2 + .../runtime/{api => lifecycle}/package.json | 18 +-- .../lifecycle/src/apis/runtimeLifecycle.ts | 36 ++++++ .../runtime/{api => lifecycle}/src/apis/tx.ts | 4 +- packages/runtime/lifecycle/src/index.ts | 4 + .../instance/overRestAPI/browser/index.ts} | 10 +- .../src/instance/overRestAPI/index.ts | 111 ++++++++++++++++++ .../src/instance/overRestAPI/nodejs/index.ts} | 10 +- .../src/instance/overRestAPI/tx.ts | 7 +- .../{api => lifecycle}/src/tsconfig.json | 0 .../{api => lifecycle}/test/context.ts | 0 .../test/examples/swap.ada.token.spec.e2e.ts | 43 +++---- .../test/jest.e2e.config.mjs | 0 .../{api => lifecycle}/test/provisionning.ts | 21 ++-- .../{api => lifecycle}/test/tsconfig.json | 2 +- .../test/tx/apply.inputs.spec.e2e.ts | 4 +- .../test/tx/create.spec.e2e.ts | 4 +- .../test/tx/withdraw.spec.e2e.ts | 45 +++---- .../runtime/{api => lifecycle}/typedoc.json | 0 packages/wallet/src/api.ts | 7 +- packages/wallet/src/browser/index.ts | 37 +++--- packages/wallet/src/nodejs/index.ts | 44 +++---- rollup/runtime-esm.config.mjs | 2 +- tsconfig.json | 2 +- 48 files changed, 389 insertions(+), 279 deletions(-) delete mode 100644 packages/runtime/api/Readme.md delete mode 100644 packages/runtime/api/src/apis/runtime.ts delete mode 100644 packages/runtime/api/src/index.ts delete mode 100644 packages/runtime/api/src/instance/overRestAPI/runtime.ts create mode 100644 packages/runtime/client/rest/src/contract/index.ts delete mode 100644 packages/runtime/client/rest/src/contract/withdrawal/id.ts create mode 100644 packages/runtime/client/rest/src/payout/index.ts rename packages/runtime/client/rest/src/{contract => }/withdrawal/details.ts (65%) rename packages/runtime/client/rest/src/{contract => }/withdrawal/endpoints/collection.ts (94%) rename packages/runtime/client/rest/src/{contract => }/withdrawal/endpoints/singleton.ts (91%) rename packages/runtime/client/rest/src/{contract => }/withdrawal/header.ts (69%) rename packages/runtime/client/rest/src/{contract => }/withdrawal/index.ts (70%) delete mode 100644 packages/runtime/core/src/payout/id.ts create mode 100644 packages/runtime/core/src/payout/index.ts rename packages/runtime/{api => lifecycle}/.npmignore (100%) create mode 100644 packages/runtime/lifecycle/Readme.md rename packages/runtime/{api => lifecycle}/package.json (75%) create mode 100644 packages/runtime/lifecycle/src/apis/runtimeLifecycle.ts rename packages/runtime/{api => lifecycle}/src/apis/tx.ts (87%) create mode 100644 packages/runtime/lifecycle/src/index.ts rename packages/runtime/{api/src/instance/overRestAPI/browser/runtime.ts => lifecycle/src/instance/overRestAPI/browser/index.ts} (59%) create mode 100644 packages/runtime/lifecycle/src/instance/overRestAPI/index.ts rename packages/runtime/{api/src/instance/overRestAPI/nodejs/runtime.ts => lifecycle/src/instance/overRestAPI/nodejs/index.ts} (62%) rename packages/runtime/{api => lifecycle}/src/instance/overRestAPI/tx.ts (94%) rename packages/runtime/{api => lifecycle}/src/tsconfig.json (100%) rename packages/runtime/{api => lifecycle}/test/context.ts (100%) rename packages/runtime/{api => lifecycle}/test/examples/swap.ada.token.spec.e2e.ts (57%) rename packages/runtime/{api => lifecycle}/test/jest.e2e.config.mjs (100%) rename packages/runtime/{api => lifecycle}/test/provisionning.ts (84%) rename packages/runtime/{api => lifecycle}/test/tsconfig.json (93%) rename packages/runtime/{api => lifecycle}/test/tx/apply.inputs.spec.e2e.ts (92%) rename packages/runtime/{api => lifecycle}/test/tx/create.spec.e2e.ts (93%) rename packages/runtime/{api => lifecycle}/test/tx/withdraw.spec.e2e.ts (65%) rename packages/runtime/{api => lifecycle}/typedoc.json (100%) diff --git a/Readme.md b/Readme.md index 1c3730b8..57379d97 100644 --- a/Readme.md +++ b/Readme.md @@ -26,7 +26,7 @@ It is composed of the following [npm workspaces](https://docs.npmjs.com/cli/v7/u - Wallet Extension Capabalities (Browser / CIP-30) - Single Wallet Address Capabalities(NodeJS / Version used for e2e tests only) - Runtime - - [@marlowe.io/runtime](./packages/runtime/api/) : Entry Point for Running remotely Marlowe Contracts over a backend instance of the runtime using a connected wallet. + - [@marlowe.io/runtime-lifecycle](./packages/runtime/lifecycle/) : Entry Point for Running remotely Marlowe Contracts over a backend instance of the runtime using a connected wallet. - Marlowe Tx Commands - Create - Applying Inputs diff --git a/package.json b/package.json index 46459f7e..d99a9c6b 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "packages/wallet", "packages/runtime/client/rest", "packages/runtime/core", - "packages/runtime/api" + "packages/runtime/lifecycle" ], "devDependencies": { "@blockfrost/blockfrost-js": "5.2.0", @@ -42,10 +42,10 @@ "@types/jest": "^26.0.24", "@types/json-bigint": "^1.0.1", "@types/node": "^18.14.2", + "axios-curlirize": "2.0.0", "dotenv": "^16.0.3", "eslint": "^7.32.0", "http-server": "^14.1.1", - "axios-curlirize":"2.0.0", "jest": "^29.4", "jest-serial-runner": "^1.2.1", "npm-run-all": "^4.1.5", diff --git a/packages/runtime/api/Readme.md b/packages/runtime/api/Readme.md deleted file mode 100644 index 7632ccdd..00000000 --- a/packages/runtime/api/Readme.md +++ /dev/null @@ -1,2 +0,0 @@ -# @marlowe.io/runtime -TODO \ No newline at end of file diff --git a/packages/runtime/api/src/apis/runtime.ts b/packages/runtime/api/src/apis/runtime.ts deleted file mode 100644 index 2403d8d0..00000000 --- a/packages/runtime/api/src/apis/runtime.ts +++ /dev/null @@ -1,13 +0,0 @@ - - -import * as TE from 'fp-ts/lib/TaskEither.js' -import { WalletAPI } from '@marlowe.io/wallet/api'; - -import { RestAPI } from '@marlowe.io/runtime-rest-client/index.js'; -import { TxAPI } from './tx.js'; - -export type Runtime = - { wallet : WalletAPI & { getLovelaces : TE.TaskEither} - restAPI : RestAPI - txCommand : TxAPI - } diff --git a/packages/runtime/api/src/index.ts b/packages/runtime/api/src/index.ts deleted file mode 100644 index e11c99b3..00000000 --- a/packages/runtime/api/src/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * as Browser from './instance/overRestAPI/browser/runtime.js' -export * as NodeJS from './instance/overRestAPI/browser/runtime.js' - - diff --git a/packages/runtime/api/src/instance/overRestAPI/runtime.ts b/packages/runtime/api/src/instance/overRestAPI/runtime.ts deleted file mode 100644 index c2992cfb..00000000 --- a/packages/runtime/api/src/instance/overRestAPI/runtime.ts +++ /dev/null @@ -1,63 +0,0 @@ - - - -import * as Command from './tx.js'; -import * as A from 'fp-ts/lib/Array.js' -import * as TE from 'fp-ts/lib/TaskEither.js' -import * as T from 'fp-ts/lib/Task.js' -import { pipe } from 'fp-ts/lib/function.js'; -import * as O from 'fp-ts/lib/Option.js' -import { mkEnvironment } from '@marlowe.io/language-core-v1/environment'; -import { addMinutes, subMinutes } from 'date-fns'; - -import { Party } from '@marlowe.io/language-core-v1/semantics/contract/common/payee/party.js'; -import { Runtime } from '../../apis/runtime.js'; -import { CreateRequest, ProvideInput } from '../../apis/tx.js'; -import { WalletAPI } from '@marlowe.io/wallet/api'; -import { PolicyId,ContractId, PayoutIds } from '@marlowe.io/runtime-core'; - -import { RestAPI } from '@marlowe.io/runtime-rest-client'; - -export const mkRuntime - : (restAPI : RestAPI) - => (walletAPI : WalletAPI) - => Runtime - = (restAPI) => (walletAPI) => - ({ wallet : {...walletAPI - ,getLovelaces : getLovelaces (walletAPI)} - , restAPI: restAPI - , txCommand : - { create : (payload : CreateRequest) => Command.create (restAPI)(walletAPI)(payload) - , applyInputs : (contractId : ContractId) => (provideInput:ProvideInput) => - pipe( restAPI.contracts.contract.get (contractId) - , TE.chain (header => TE.fromTask(getParties(walletAPI)(header.roleTokenMintingPolicyId))) - , TE.chain( parties => - restAPI.contracts.contract.next - (contractId) - (mkEnvironment - (pipe(Date.now(),(date) => subMinutes(date,15))) - (pipe(Date.now(),(date) => addMinutes(date,15)))) - (parties)) - , TE.chain( next => - Command.applyInputs - (restAPI) - (walletAPI) - (contractId) - (provideInput(next)))) - , withdraw : (payoutIds : PayoutIds) => Command.withdraw (restAPI) (walletAPI) (payoutIds)}}) - -const getLovelaces : (walletApi : WalletAPI) => TE.TaskEither - = (walletAPI) => - pipe( walletAPI.getTokenValues - , TE.map(tokenvalues => - pipe(tokenvalues - , A.filter((tokenValue) => tokenValue.token.currency_symbol == '') - , A.map(tokenValue => tokenValue.amount) - , A.head - , O.getOrElse(() => 0n)))) - -const getParties - : (walletApi : WalletAPI) - => (roleTokenMintingPolicyId : PolicyId) - => T.Task - = (walletAPI) => (roleMintingPolicyId) => T.of ([]) \ No newline at end of file diff --git a/packages/runtime/client/rest/src/contract/index.ts b/packages/runtime/client/rest/src/contract/index.ts new file mode 100644 index 00000000..902a4da5 --- /dev/null +++ b/packages/runtime/client/rest/src/contract/index.ts @@ -0,0 +1,3 @@ +export * from './header.js' +export * from './details.js' +export * from './role.js' \ No newline at end of file diff --git a/packages/runtime/client/rest/src/contract/withdrawal/id.ts b/packages/runtime/client/rest/src/contract/withdrawal/id.ts deleted file mode 100644 index fb091d60..00000000 --- a/packages/runtime/client/rest/src/contract/withdrawal/id.ts +++ /dev/null @@ -1,17 +0,0 @@ -import * as t from "io-ts/lib/index.js"; -import { iso, Newtype } from "newtype-ts"; -import { fromNewtype } from "io-ts-types"; -import { pipe } from "fp-ts/lib/function.js"; -import { TxId } from "@marlowe.io/runtime-core"; - - -export type WithdrawalId = Newtype<{ readonly WithdrawalId: unique symbol }, string> -export const WithdrawalId = fromNewtype(t.string) -export const unWithdrawalId = iso().unwrap -export const withdrawalId= iso().wrap - - -export const idToTxId : (withdrawalId : WithdrawalId) => TxId - = (withdrawalId) => - pipe( withdrawalId - , unWithdrawalId) \ No newline at end of file diff --git a/packages/runtime/client/rest/src/index.ts b/packages/runtime/client/rest/src/index.ts index b01dba63..71b56540 100644 --- a/packages/runtime/client/rest/src/index.ts +++ b/packages/runtime/client/rest/src/index.ts @@ -9,8 +9,8 @@ import * as HTTP from '@marlowe.io/adapter/http'; import * as Payouts from './payout/endpoints/collection.js'; import * as Payout from './payout/endpoints/singleton.js'; -import * as Withdrawal from './contract/withdrawal/endpoints/singleton.js'; -import * as Withdrawals from './contract/withdrawal/endpoints/collection.js'; +import * as Withdrawal from './withdrawal/endpoints/singleton.js'; +import * as Withdrawals from './withdrawal/endpoints/collection.js'; import * as Contract from './contract/endpoints/singleton.js'; import * as Contracts from './contract/endpoints/collection.js'; import * as Transaction from './contract/transaction/endpoints/singleton.js'; @@ -18,6 +18,11 @@ import * as Transactions from './contract/transaction/endpoints/collection.js'; import * as ContractNext from './contract/next/endpoint.js'; import curlirize from 'axios-curlirize'; + +export * from './contract/index.js' +export * from './withdrawal/index.js' +export * from './payout/index.js' + export interface RestAPI { healthcheck : () => TE.TaskEither payouts : { diff --git a/packages/runtime/client/rest/src/payout/details.ts b/packages/runtime/client/rest/src/payout/details.ts index 45e8f1dd..74d8d43c 100644 --- a/packages/runtime/client/rest/src/payout/details.ts +++ b/packages/runtime/client/rest/src/payout/details.ts @@ -2,8 +2,8 @@ import * as t from "io-ts/lib/index.js"; import { optionFromNullable } from "io-ts-types"; -import { ContractId, AssetId, PayoutId, AddressBech32, Assets } from "@marlowe.io/runtime-core"; -import { WithdrawalId } from "../contract/withdrawal/id.js"; +import { ContractId, AssetId, PayoutId, AddressBech32, Assets, WithdrawalId } from "@marlowe.io/runtime-core"; + import { PayoutStatus } from "./status.js"; export type PayoutDetails = t.TypeOf @@ -11,7 +11,7 @@ export const PayoutDetails = t.type( { payoutId: PayoutId , contractId: ContractId - , withdrawalId : optionFromNullable(WithdrawalId) + , withdrawalIdOption : optionFromNullable(WithdrawalId) , role: AssetId , payoutValidatorAddress: AddressBech32 , status : PayoutStatus diff --git a/packages/runtime/client/rest/src/payout/endpoints/collection.ts b/packages/runtime/client/rest/src/payout/endpoints/collection.ts index b08e427d..c83dbe4d 100644 --- a/packages/runtime/client/rest/src/payout/endpoints/collection.ts +++ b/packages/runtime/client/rest/src/payout/endpoints/collection.ts @@ -32,14 +32,14 @@ export type GETHeadersByRange = (rangeOption: O.Option) => (statusOption : O.Option) => TE.TaskEither -const roleToParameter = (role : AssetId) => `${unPolicyId(role.policyId)}.${role.assetName}` +const roleToParameter = (roleToken : AssetId) => `${unPolicyId(roleToken.policyId)}.${roleToken.assetName}` const contractIdToParameter = (contractId : ContractId) => unContractId(contractId) const statusOptionToParameter = (statusOption : O.Option) => pipe ( statusOption, O.match(() => '', a => `status=${a}&`)) export const getHeadersByRangeViaAxios:(axiosInstance: AxiosInstance) => GETHeadersByRange = (axiosInstance) => (rangeOption) => (contractIds) => (roles) => (statusOption) => pipe( ({ url : '/payouts?' + statusOptionToParameter(statusOption) + stringify(({contractId:contractIds.map(contractIdToParameter) - ,roles:roles.map(roleToParameter)}), { indices: false }) + ,roleToken:roles.map(roleToParameter)}), { indices: false }) , configs : pipe(rangeOption , O.match( () => ({}) , range => ({ headers: { Range: unContractsRange(range) }})))}) diff --git a/packages/runtime/client/rest/src/payout/header.ts b/packages/runtime/client/rest/src/payout/header.ts index e7d57699..30eb7455 100644 --- a/packages/runtime/client/rest/src/payout/header.ts +++ b/packages/runtime/client/rest/src/payout/header.ts @@ -1,9 +1,8 @@ import * as t from "io-ts/lib/index.js"; -import { ContractId, AssetId, PayoutId } from "@marlowe.io/runtime-core"; +import { ContractId, AssetId, PayoutId, WithdrawalId } from "@marlowe.io/runtime-core"; import { PayoutStatus } from "./status.js"; -import { WithdrawalId } from "../contract/withdrawal/id.js"; import { optionFromNullable } from "io-ts-types"; export type PayoutHeader = t.TypeOf diff --git a/packages/runtime/client/rest/src/payout/index.ts b/packages/runtime/client/rest/src/payout/index.ts new file mode 100644 index 00000000..ba910381 --- /dev/null +++ b/packages/runtime/client/rest/src/payout/index.ts @@ -0,0 +1,3 @@ +export * from './details.js' +export * from './header.js' +export * from './status.js' \ No newline at end of file diff --git a/packages/runtime/client/rest/src/contract/withdrawal/details.ts b/packages/runtime/client/rest/src/withdrawal/details.ts similarity index 65% rename from packages/runtime/client/rest/src/contract/withdrawal/details.ts rename to packages/runtime/client/rest/src/withdrawal/details.ts index 996d5e60..fce27b69 100644 --- a/packages/runtime/client/rest/src/contract/withdrawal/details.ts +++ b/packages/runtime/client/rest/src/withdrawal/details.ts @@ -1,13 +1,11 @@ import * as t from "io-ts/lib/index.js"; - -import { WithdrawalId } from "./id.js"; -import { TxStatus } from "../transaction/status.js"; +import { TxStatus } from "../contract/transaction/status.js"; import { optionFromNullable } from "io-ts-types/lib/optionFromNullable.js"; -import { BlockHeader } from "@marlowe.io/runtime-core"; -import { PayoutHeader } from "../../payout/header.js"; +import { BlockHeader, WithdrawalId } from "@marlowe.io/runtime-core"; +import { PayoutHeader } from "../payout/header.js"; export type WithdrawalDetails = t.TypeOf diff --git a/packages/runtime/client/rest/src/contract/withdrawal/endpoints/collection.ts b/packages/runtime/client/rest/src/withdrawal/endpoints/collection.ts similarity index 94% rename from packages/runtime/client/rest/src/contract/withdrawal/endpoints/collection.ts rename to packages/runtime/client/rest/src/withdrawal/endpoints/collection.ts index 8a4d7eee..bcf244b4 100644 --- a/packages/runtime/client/rest/src/contract/withdrawal/endpoints/collection.ts +++ b/packages/runtime/client/rest/src/withdrawal/endpoints/collection.ts @@ -16,9 +16,8 @@ import { formatValidationErrors } from 'jsonbigint-io-ts-reporters' import * as HTTP from '@marlowe.io/adapter/http'; import { DecodingError } from '@marlowe.io/adapter/codec'; -import { AddressesAndCollaterals, PayoutIds, TextEnvelope, unAddressBech32, unTxOutRef } from '@marlowe.io/runtime-core'; +import { AddressesAndCollaterals, PayoutId, TextEnvelope, WithdrawalId, unAddressBech32, unTxOutRef } from '@marlowe.io/runtime-core'; -import { WithdrawalId } from '../id.js'; import { WithdrawalHeader } from '../header.js'; @@ -57,7 +56,8 @@ export const GETByRangeResponse , nextRange :optionFromNullable(WithdrawalsRange) }); -export type POST = ( payoutIds: PayoutIds + +export type POST = ( payoutIds: PayoutId[] , addressesAndCollaterals: AddressesAndCollaterals) => TE.TaskEither export type WithdrawalTextEnvelope = t.TypeOf; @@ -73,7 +73,7 @@ export const postViaAxios:(axiosInstance: AxiosInstance) => POST = (axiosInstance) => (payoutIds, addressesAndCollaterals) => pipe( HTTP.Post (axiosInstance) ( '/withdrawals' - , payoutIds + , {payouts : payoutIds} , { headers: { 'Accept': 'application/vendor.iog.marlowe-runtime.withdraw-tx-json', 'Content-Type':'application/json', diff --git a/packages/runtime/client/rest/src/contract/withdrawal/endpoints/singleton.ts b/packages/runtime/client/rest/src/withdrawal/endpoints/singleton.ts similarity index 91% rename from packages/runtime/client/rest/src/contract/withdrawal/endpoints/singleton.ts rename to packages/runtime/client/rest/src/withdrawal/endpoints/singleton.ts index 3ebaaff5..1df4a607 100644 --- a/packages/runtime/client/rest/src/contract/withdrawal/endpoints/singleton.ts +++ b/packages/runtime/client/rest/src/withdrawal/endpoints/singleton.ts @@ -9,10 +9,9 @@ import * as HTTP from '@marlowe.io/adapter/http'; import {formatValidationErrors} from 'jsonbigint-io-ts-reporters' -import { unWithdrawalId, WithdrawalId } from '../id.js'; import { WithdrawalDetails } from '../details.js'; import { DecodingError } from '@marlowe.io/adapter/codec'; -import { HexTransactionWitnessSet, transactionWitnessSetTextEnvelope } from '@marlowe.io/runtime-core'; +import { HexTransactionWitnessSet, WithdrawalId, transactionWitnessSetTextEnvelope, unWithdrawalId } from '@marlowe.io/runtime-core'; export type GET = ( withdrawalId: WithdrawalId) => TE.TaskEither diff --git a/packages/runtime/client/rest/src/contract/withdrawal/header.ts b/packages/runtime/client/rest/src/withdrawal/header.ts similarity index 69% rename from packages/runtime/client/rest/src/contract/withdrawal/header.ts rename to packages/runtime/client/rest/src/withdrawal/header.ts index 0c883ee9..88eb89b8 100644 --- a/packages/runtime/client/rest/src/contract/withdrawal/header.ts +++ b/packages/runtime/client/rest/src/withdrawal/header.ts @@ -2,10 +2,9 @@ import * as t from "io-ts/lib/index.js"; -import { WithdrawalId } from "./id.js"; -import { TxStatus } from "../transaction/status.js"; +import { TxStatus } from "../contract/transaction/status.js"; import { optionFromNullable } from "io-ts-types/lib/optionFromNullable.js"; -import { BlockHeader } from "@marlowe.io/runtime-core"; +import { BlockHeader, WithdrawalId } from "@marlowe.io/runtime-core"; export type WithdrawalHeader = t.TypeOf diff --git a/packages/runtime/client/rest/src/contract/withdrawal/index.ts b/packages/runtime/client/rest/src/withdrawal/index.ts similarity index 70% rename from packages/runtime/client/rest/src/contract/withdrawal/index.ts rename to packages/runtime/client/rest/src/withdrawal/index.ts index b3221391..cdd64738 100644 --- a/packages/runtime/client/rest/src/contract/withdrawal/index.ts +++ b/packages/runtime/client/rest/src/withdrawal/index.ts @@ -1,3 +1,2 @@ -export * from './id.js' export * from './header.js' export * from './details.js' \ No newline at end of file diff --git a/packages/runtime/core/src/asset/index.ts b/packages/runtime/core/src/asset/index.ts index 7193892b..a6faaae3 100644 --- a/packages/runtime/core/src/asset/index.ts +++ b/packages/runtime/core/src/asset/index.ts @@ -1,6 +1,7 @@ import * as t from "io-ts/lib/index.js"; -import { PolicyId } from "../policyId.js"; +import { PolicyId, mkPolicyId, unPolicyId } from "../policyId.js"; +import * as Marlowe from '@marlowe.io/language-core-v1/tokenValue' export type AssetName = t.TypeOf export const AssetName = t.string @@ -14,12 +15,18 @@ export const AssetId { policyId: PolicyId , assetName: AssetName }) +export const assetId : (policyId : PolicyId) => (assetName : AssetName) => AssetId + = (policyId) => (assetName) => ({ policyId: policyId, assetName: assetName}) + export type Token = t.TypeOf export const Token = t.type( { quantity: AssetQuantity , assetId: AssetId }) + +export const token : (quantity : AssetQuantity) => (assetId : AssetId)=> Token = (quantity) => (assetId) => ({quantity: quantity, assetId : assetId}) +export const lovelaces : (quantity : AssetQuantity) => Token = (quantity) => token(quantity)(assetId(mkPolicyId(''))('')) export type Tokens = t.TypeOf export const Tokens = t.record(PolicyId, t.record(AssetName, AssetQuantity)) @@ -31,3 +38,11 @@ export const Assets , tokens: Tokens }) +export const assetIdToString : (assetId : AssetId) => string = (assetId) => `${unPolicyId(assetId.policyId)}|${assetId.assetName}` + +export const runtimeTokenToMarloweTokenValue : (runtimeToken: Token) => Marlowe.TokenValue + = (runtimeToken) => + ({ amount :runtimeToken.quantity + , token: + { currency_symbol:unPolicyId(runtimeToken.assetId.policyId) + , token_name:runtimeToken.assetId.assetName} }) \ No newline at end of file diff --git a/packages/runtime/core/src/index.ts b/packages/runtime/core/src/index.ts index e06d61bd..124b2506 100644 --- a/packages/runtime/core/src/index.ts +++ b/packages/runtime/core/src/index.ts @@ -7,4 +7,4 @@ export * from './metadata.js' export * from './tag.js' export * from './contract/id.js' export * from './asset/index.js' -export * from './payout/id.js' \ No newline at end of file +export * from './payout/index.js' \ No newline at end of file diff --git a/packages/runtime/core/src/payout/id.ts b/packages/runtime/core/src/payout/id.ts deleted file mode 100644 index 7ae55b1c..00000000 --- a/packages/runtime/core/src/payout/id.ts +++ /dev/null @@ -1,23 +0,0 @@ -import * as t from "io-ts/lib/index.js"; -import { iso, Newtype } from "newtype-ts"; -import { fromNewtype } from "io-ts-types"; -import { split } from "fp-ts/lib/string.js"; -import { pipe } from "fp-ts/lib/function.js"; -import { head } from "fp-ts/lib/ReadonlyNonEmptyArray.js"; -import { TxId } from "../tx/id.js"; - - -export type PayoutId = Newtype<{ readonly ContractId: unique symbol }, string> -export const PayoutId = fromNewtype(t.string) -export const unPayoutId = iso().unwrap -export const payoutId = iso().wrap - -export type PayoutIds = t.TypeOf -export const PayoutIds = t.type({ payouts: t.array(PayoutId)}) - -export const payoutIdToTxId : (payoutId : PayoutId) => TxId - = (payoutId) => - pipe( payoutId - , unPayoutId - , split('#') - , head) \ No newline at end of file diff --git a/packages/runtime/core/src/payout/index.ts b/packages/runtime/core/src/payout/index.ts new file mode 100644 index 00000000..69a5e6b9 --- /dev/null +++ b/packages/runtime/core/src/payout/index.ts @@ -0,0 +1,53 @@ +import * as t from "io-ts/lib/index.js"; +import { iso, Newtype } from "newtype-ts"; +import { fromNewtype } from "io-ts-types"; +import { split } from "fp-ts/lib/string.js"; +import { pipe } from "fp-ts/lib/function.js"; +import { head } from "fp-ts/lib/ReadonlyNonEmptyArray.js"; +import { TxId } from "../tx/id.js"; +import { ContractId } from "../contract/id.js"; +import { AssetId, Assets } from "../asset/index.js"; + + +export type PayoutId = Newtype<{ readonly ContractId: unique symbol }, string> +export const PayoutId = fromNewtype(t.string) +export const unPayoutId = iso().unwrap +export const payoutId = iso().wrap + +export const payoutIdToTxId : (payoutId : PayoutId) => TxId + = (payoutId) => + pipe( payoutId + , unPayoutId + , split('#') + , head) + +export type WithdrawalId = Newtype<{ readonly WithdrawalId: unique symbol }, string> +export const WithdrawalId = fromNewtype(t.string) +export const unWithdrawalId = iso().unwrap +export const withdrawalId= iso().wrap + +export const withdrawalIdToTxId : (withdrawalId : WithdrawalId) => TxId + = (withdrawalId) => + pipe( withdrawalId + , unWithdrawalId) + + +export type PayoutAvailable = t.TypeOf +export const PayoutAvailable + = t.type( + { payoutId: PayoutId + , contractId: ContractId + , role: AssetId + , assets : Assets + }) + +export type PayoutWithdrawn = t.TypeOf +export const PayoutWithdrawn + = t.type( + { withdrawalId : WithdrawalId + , payoutId: PayoutId + , contractId: ContractId + , role: AssetId + , assets : Assets + }) + \ No newline at end of file diff --git a/packages/runtime/core/src/policyId.ts b/packages/runtime/core/src/policyId.ts index 940034b0..0104a6d9 100644 --- a/packages/runtime/core/src/policyId.ts +++ b/packages/runtime/core/src/policyId.ts @@ -5,4 +5,4 @@ import { fromNewtype } from "io-ts-types"; export type PolicyId = Newtype<{ readonly PolicyId: unique symbol }, string> export const PolicyId = fromNewtype(t.string) export const unPolicyId = iso().unwrap -export const policyId = iso().wrap +export const mkPolicyId = iso().wrap diff --git a/packages/runtime/api/.npmignore b/packages/runtime/lifecycle/.npmignore similarity index 100% rename from packages/runtime/api/.npmignore rename to packages/runtime/lifecycle/.npmignore diff --git a/packages/runtime/lifecycle/Readme.md b/packages/runtime/lifecycle/Readme.md new file mode 100644 index 00000000..16924e65 --- /dev/null +++ b/packages/runtime/lifecycle/Readme.md @@ -0,0 +1,2 @@ +# @marlowe.io/runtime-lifecylce +TODO \ No newline at end of file diff --git a/packages/runtime/api/package.json b/packages/runtime/lifecycle/package.json similarity index 75% rename from packages/runtime/api/package.json rename to packages/runtime/lifecycle/package.json index 2c867183..05fd5930 100644 --- a/packages/runtime/api/package.json +++ b/packages/runtime/lifecycle/package.json @@ -1,5 +1,5 @@ { - "name": "@marlowe.io/runtime", + "name": "@marlowe.io/runtime-lifecycle", "version": "0.1.0-beta", "description": "Entry Point for Running remotely Marlowe Contracts over a backend instance of the runtime using a connected wallet.", "repository": "https://github.com/input-output-hk/marlowe-ts-sdk", @@ -32,20 +32,20 @@ "types": "./dist/index.d.ts" }, "./api": { - "import": "./dist/api.js", - "types": "./dist/api.d.ts" + "import": "./dist/apis/runtimeLifecycle.js", + "types": "./dist/apis/runtimeLifecycle.d.ts" }, "./overRestAPI": { - "import": "./dist/instance/overRestAPI/runtime.js", - "types": "./dist/instance/overRestAPI/runtime.d.ts" + "import": "./dist/instance/overRestAPI/index.js", + "types": "./dist/instance/overRestAPI/index.d.ts" }, "./browser": { - "import": "./dist/instance/overRestAPI/browser/runtime.js", - "types": "./dist/instance/overRestAPI/browser/runtime.d.ts" + "import": "./dist/instance/overRestAPI/browser/index.js", + "types": "./dist/instance/overRestAPI/browser/index.d.ts" }, "./nodejs": { - "import": "./dist/instance/overRestAPI/nodejs/runtime.js", - "types": "./dist/instance/overRestAPI/nodejs/runtime.d.ts" + "import": "./dist/instance/overRestAPI/nodejs/index.js", + "types": "./dist/instance/overRestAPI/nodejs/index.d.ts" }, "./common/*": "./dist/common/*.js", "./wallet/*": "./dist/wallet/*.js", diff --git a/packages/runtime/lifecycle/src/apis/runtimeLifecycle.ts b/packages/runtime/lifecycle/src/apis/runtimeLifecycle.ts new file mode 100644 index 00000000..d0fabe77 --- /dev/null +++ b/packages/runtime/lifecycle/src/apis/runtimeLifecycle.ts @@ -0,0 +1,36 @@ + + +import * as TE from 'fp-ts/lib/TaskEither.js' +import * as F from 'fp-ts/lib' +import { WalletAPI } from '@marlowe.io/wallet/api'; +import * as O from 'fp-ts/lib/Option.js' +import { CreateRequest, ProvideInput, TxAPI } from './tx.js'; +import { AssetId, ContractId, PayoutAvailable, PayoutId, PayoutWithdrawn } from '@marlowe.io/runtime-core'; +import { DecodingError } from '@marlowe.io/adapter/codec'; + +export type RuntimeLifecycle = + { wallet : WalletAPI & { getLovelaces : TE.TaskEither} + contracts : { + create : (payload: CreateRequest) => TE.TaskEither + applyInputs : (contractId: ContractId) => (provideInput : ProvideInput) => TE.TaskEither + } + payouts : + { available : (filtersOption : O.Option) => TE.TaskEither + , withdraw : (payoutIds : PayoutId[]) => TE.TaskEither + , withdrawn : (filtersOption : O.Option) => TE.TaskEither + } + + } + +export const onlyByContractIds : (contractIds : ContractId[]) => O.Option + = (contractIds) => + O.some ({ + byContractIds : contractIds + , byMyRoleTokens : (myRoles) => myRoles + }) + +export type Filters = ({ + byContractIds : ContractId[] + byMyRoleTokens : (myRolesTokens : AssetId[]) => AssetId[] +}) + diff --git a/packages/runtime/api/src/apis/tx.ts b/packages/runtime/lifecycle/src/apis/tx.ts similarity index 87% rename from packages/runtime/api/src/apis/tx.ts rename to packages/runtime/lifecycle/src/apis/tx.ts index 1c2d17b6..156e3e71 100644 --- a/packages/runtime/api/src/apis/tx.ts +++ b/packages/runtime/lifecycle/src/apis/tx.ts @@ -8,7 +8,7 @@ import { Contract } from '@marlowe.io/language-core-v1/semantics/contract/index. import { Input } from '@marlowe.io/language-core-v1/semantics/contract/when/input/index.js'; import { Next } from '@marlowe.io/language-core-v1/semantics/next/index.js'; -import { Tags, Metadata,ContractId, PayoutIds } from '@marlowe.io/runtime-core'; +import { Tags, Metadata,ContractId, PayoutId } from '@marlowe.io/runtime-core'; import { RolesConfig } from '@marlowe.io/runtime-rest-client/contract/role.js'; @@ -16,7 +16,7 @@ import { RolesConfig } from '@marlowe.io/runtime-rest-client/contract/role.js'; export type TxAPI = { create : (payload: CreateRequest) => TE.TaskEither applyInputs : (contractId: ContractId) => (provideInput : ProvideInput) => TE.TaskEither - withdraw : (payoutIds : PayoutIds) => TE.TaskEither + withdraw : (payoutIds : PayoutId[]) => TE.TaskEither } export type ProvideInput = (next:Next) => ApplyInputsRequest diff --git a/packages/runtime/lifecycle/src/index.ts b/packages/runtime/lifecycle/src/index.ts new file mode 100644 index 00000000..dfecac02 --- /dev/null +++ b/packages/runtime/lifecycle/src/index.ts @@ -0,0 +1,4 @@ +export * as Browser from './instance/overRestAPI/browser/index.js' +export * as NodeJS from './instance/overRestAPI/browser/index.js' + + diff --git a/packages/runtime/api/src/instance/overRestAPI/browser/runtime.ts b/packages/runtime/lifecycle/src/instance/overRestAPI/browser/index.ts similarity index 59% rename from packages/runtime/api/src/instance/overRestAPI/browser/runtime.ts rename to packages/runtime/lifecycle/src/instance/overRestAPI/browser/index.ts index 9a37854e..64c0677f 100644 --- a/packages/runtime/api/src/instance/overRestAPI/browser/runtime.ts +++ b/packages/runtime/lifecycle/src/instance/overRestAPI/browser/index.ts @@ -2,16 +2,16 @@ import * as T from 'fp-ts/lib/Task.js' import { pipe } from 'fp-ts/lib/function.js'; import { getExtensionInstance } from '@marlowe.io/wallet/browser'; -import * as Generic from '../runtime.js'; -import { Runtime } from '../../../apis/runtime.js'; +import * as Generic from '../index.js'; +import { RuntimeLifecycle } from '../../../apis/runtimeLifecycle.js'; import { mkRestClient } from '@marlowe.io/runtime-rest-client'; -export const mkRuntime +export const mkRuntimeLifecycle : (runtimeURL : string) => (extensionName : string) - => T.Task = + => T.Task = (runtimeURL) => (extensionName) => pipe( getExtensionInstance (extensionName) - , T.map (Generic.mkRuntime (mkRestClient(runtimeURL)))) + , T.map (Generic.mkRuntimeLifecycle (mkRestClient(runtimeURL)))) diff --git a/packages/runtime/lifecycle/src/instance/overRestAPI/index.ts b/packages/runtime/lifecycle/src/instance/overRestAPI/index.ts new file mode 100644 index 00000000..3376bb6c --- /dev/null +++ b/packages/runtime/lifecycle/src/instance/overRestAPI/index.ts @@ -0,0 +1,111 @@ + + + +import * as Command from './tx.js'; +import * as TE from 'fp-ts/lib/TaskEither.js' +import * as T from 'fp-ts/lib/Task.js' +import { pipe } from 'fp-ts/lib/function.js'; +import * as O from 'fp-ts/lib/Option.js' +import { mkEnvironment } from '@marlowe.io/language-core-v1/environment'; +import { addMinutes, subMinutes } from 'date-fns'; + +import { Party } from '@marlowe.io/language-core-v1/semantics/contract/common/payee/party.js'; +import { Filters, RuntimeLifecycle } from '../../apis/runtimeLifecycle.js'; +import { CreateRequest, ProvideInput } from '../../apis/tx.js'; +import { WalletAPI } from '@marlowe.io/wallet/api'; +import { PolicyId,ContractId, PayoutId, PayoutAvailable, AssetId, PayoutWithdrawn, unPolicyId } from '@marlowe.io/runtime-core'; + +import { RestAPI } from '@marlowe.io/runtime-rest-client'; +import { DecodingError } from '@marlowe.io/adapter/codec'; + +export const mkRuntimeLifecycle + : (restAPI : RestAPI) + => (walletAPI : WalletAPI) + => RuntimeLifecycle + = (restAPI) => (walletAPI) => + ({ wallet : walletAPI + , contracts : + { create : (payload : CreateRequest) => Command.create (restAPI)(walletAPI)(payload) + , applyInputs : (contractId : ContractId) => (provideInput:ProvideInput) => + pipe( restAPI.contracts.contract.get (contractId) + , TE.chain (header => TE.fromTask(getParties(walletAPI)(header.roleTokenMintingPolicyId))) + , TE.chain( parties => + restAPI.contracts.contract.next + (contractId) + (mkEnvironment + (pipe(Date.now(),(date) => subMinutes(date,15))) + (pipe(Date.now(),(date) => addMinutes(date,15)))) + (parties)) + , TE.chain( next => + Command.applyInputs + (restAPI) + (walletAPI) + (contractId) + (provideInput(next))))} + , payouts : + { available : (filtersOption : O.Option) => availablePayouts(restAPI) (walletAPI) (filtersOption) + , withdraw : (payoutIds : PayoutId[]) => Command.withdraw (restAPI) (walletAPI) (payoutIds) + , withdrawn : (filtersOption : O.Option) => withdrawnPayouts(restAPI) (walletAPI) (filtersOption)}}) + + + +const availablePayouts : (restAPI : RestAPI) => (walletApi : WalletAPI) => (filtersOption : O.Option) => TE.TaskEither + = (restAPI) => (walletApi) => (filtersOption) => + pipe(getAssetIds(walletApi) + ,TE.chain ( (walletAssetIds) => + pipe + ( restAPI.payouts.getHeadersByRange + (O.none) + (pipe(filtersOption,O.match(() => [], filters => filters.byContractIds))) + (pipe(filtersOption,O.match(() => walletAssetIds, filters => filters.byMyRoleTokens(walletAssetIds)))) + (O.some("available")) + , TE.map(result => result.headers))) + ,TE.chain(headers => TE.sequenceArray( headers.map (header => restAPI.payouts.get(header.payoutId) ) )) + ,TE.map (payoutsDetails => + payoutsDetails.map(payoutDetails => + ({ payoutId: payoutDetails.payoutId + , contractId: payoutDetails.contractId + , role: payoutDetails.role + , assets : payoutDetails.assets + })))) + +const withdrawnPayouts : (restAPI : RestAPI) => (walletApi : WalletAPI) => (filtersOption : O.Option) => TE.TaskEither + = (restAPI) => (walletApi) => (filtersOption) => + pipe(getAssetIds(walletApi) + ,TE.chain ( (walletAssetIds) => + pipe(restAPI.payouts.getHeadersByRange + (O.none) + (pipe(filtersOption,O.match(() => [], filters => filters.byContractIds))) + (pipe(filtersOption,O.match(() => walletAssetIds, filters => filters.byMyRoleTokens(walletAssetIds)))) + (O.some("withdrawn")) + , TE.map(result => result.headers))) + ,TE.chain(headers => TE.sequenceArray( headers.map (header => restAPI.payouts.get(header.payoutId)))) + ,TE.map (payoutsDetails => + payoutsDetails.map(payoutDetails => + pipe( payoutDetails.withdrawalIdOption + , O.match + ( () => { throw "Rest API Inconsistencies for Payout API (payout withdrawn wihouth a withdrawalID)"} + , withdrawalId => + ({ withdrawalId : withdrawalId + , payoutId: payoutDetails.payoutId + , contractId: payoutDetails.contractId + , role: payoutDetails.role + , assets : payoutDetails.assets + }) + ))))) + + + +const getAssetIds + : (walletApi : WalletAPI) + => TE.TaskEither + = (walletAPI) => + pipe (walletAPI.getTokens,TE.map(tokens => tokens.map (token => token.assetId))) + +const getParties + : (walletApi : WalletAPI) + => (roleTokenMintingPolicyId : PolicyId) + => T.Task + = (walletAPI) => (roleMintingPolicyId) => T.of ([]) + + diff --git a/packages/runtime/api/src/instance/overRestAPI/nodejs/runtime.ts b/packages/runtime/lifecycle/src/instance/overRestAPI/nodejs/index.ts similarity index 62% rename from packages/runtime/api/src/instance/overRestAPI/nodejs/runtime.ts rename to packages/runtime/lifecycle/src/instance/overRestAPI/nodejs/index.ts index 9470b7b2..753def6d 100644 --- a/packages/runtime/api/src/instance/overRestAPI/nodejs/runtime.ts +++ b/packages/runtime/lifecycle/src/instance/overRestAPI/nodejs/index.ts @@ -3,14 +3,14 @@ import * as T from 'fp-ts/lib/Task.js'; import { pipe } from 'fp-ts/lib/function.js'; import * as S from '@marlowe.io/wallet/nodejs';; -import * as Generic from '../runtime.js'; -import { Runtime } from '../../../apis/runtime.js'; +import * as Generic from '../index.js'; +import { RuntimeLifecycle } from '../../../apis/runtimeLifecycle.js'; -export const mkRuntime +export const mkRuntimeLifecycle : (runtimeURL : string) => ( context:S.Context) => (privateKeyBech32: string) - => T.Task = + => T.Task = (runtimeURL) =>(context) => (privateKeyBech32) => pipe( S.SingleAddressWallet.Initialise (context,privateKeyBech32) - , T.map (Generic.mkRuntime (mkRestClient(runtimeURL)))) + , T.map (Generic.mkRuntimeLifecycle (mkRestClient(runtimeURL)))) diff --git a/packages/runtime/api/src/instance/overRestAPI/tx.ts b/packages/runtime/lifecycle/src/instance/overRestAPI/tx.ts similarity index 94% rename from packages/runtime/api/src/instance/overRestAPI/tx.ts rename to packages/runtime/lifecycle/src/instance/overRestAPI/tx.ts index a7c13c57..67375fe7 100644 --- a/packages/runtime/api/src/instance/overRestAPI/tx.ts +++ b/packages/runtime/lifecycle/src/instance/overRestAPI/tx.ts @@ -6,10 +6,9 @@ import { RestAPI } from '@marlowe.io/runtime-rest-client/index.js'; import { WalletAPI, getAddressesAndCollaterals } from '@marlowe.io/wallet/api'; import { DecodingError } from '@marlowe.io/adapter/codec'; import { CreateRequest, ApplyInputsRequest } from '../../apis/tx.js'; -import { ContractId, PayoutIds, contractIdToTxId } from "@marlowe.io/runtime-core";; +import { ContractId, PayoutId, contractIdToTxId, withdrawalIdToTxId } from "@marlowe.io/runtime-core";; import * as Tx from '@marlowe.io/runtime-rest-client/transaction'; -import * as Withdrawal from '@marlowe.io/runtime-rest-client/withdrawal'; export const create : (client : RestAPI) @@ -66,7 +65,7 @@ export const applyInputs export const withdraw : (client : RestAPI) => (wallet : WalletAPI) - => (payoutIds : PayoutIds) + => (payoutIds : PayoutId[]) => TE.TaskEither = (client) => (wallet) => (payoutIds) => pipe( getAddressesAndCollaterals (wallet) @@ -76,5 +75,5 @@ export const withdraw pipe ( wallet.signTxTheCIP30Way(withdrawalTextEnvelope.tx.cborHex) , TE.chain ((hexTransactionWitnessSet) => client.withdrawals.withdrawal.put(withdrawalTextEnvelope.withdrawalId,hexTransactionWitnessSet)) , TE.map (() => withdrawalTextEnvelope.withdrawalId))) - , TE.chainFirstW((withdrawalId) => wallet.waitConfirmation(pipe(withdrawalId, Withdrawal.idToTxId))) + , TE.chainFirstW((withdrawalId) => wallet.waitConfirmation(pipe(withdrawalId, withdrawalIdToTxId))) , TE.map (constVoid)) \ No newline at end of file diff --git a/packages/runtime/api/src/tsconfig.json b/packages/runtime/lifecycle/src/tsconfig.json similarity index 100% rename from packages/runtime/api/src/tsconfig.json rename to packages/runtime/lifecycle/src/tsconfig.json diff --git a/packages/runtime/api/test/context.ts b/packages/runtime/lifecycle/test/context.ts similarity index 100% rename from packages/runtime/api/test/context.ts rename to packages/runtime/lifecycle/test/context.ts diff --git a/packages/runtime/api/test/examples/swap.ada.token.spec.e2e.ts b/packages/runtime/lifecycle/test/examples/swap.ada.token.spec.e2e.ts similarity index 57% rename from packages/runtime/api/test/examples/swap.ada.token.spec.e2e.ts rename to packages/runtime/lifecycle/test/examples/swap.ada.token.spec.e2e.ts index 33180d99..f03af883 100644 --- a/packages/runtime/api/test/examples/swap.ada.token.spec.e2e.ts +++ b/packages/runtime/lifecycle/test/examples/swap.ada.token.spec.e2e.ts @@ -8,10 +8,11 @@ import { toInput } from '@marlowe.io/language-core-v1/next'; import * as Examples from '@marlowe.io/language-core-v1/examples' import { datetoTimeout, adaValue } from '@marlowe.io/language-core-v1' import { mkRestClient } from '@marlowe.io/runtime-rest-client/index.js'; -import * as O from 'fp-ts/lib/Option.js'; import { getBankPrivateKey, getBlockfrostContext, getMarloweRuntimeUrl } from '../context.js'; import { provisionAnAdaAndTokenProvider } from '../provisionning.js' import console from "console" +import { runtimeTokenToMarloweTokenValue } from '@marlowe.io/runtime-core'; +import { onlyByContractIds } from '@marlowe.io/runtime-lifecycle/api'; global.console = console @@ -35,41 +36,41 @@ describe('swap', () => { , swapper : { roleName : 'Token provider' , depositTimeout : pipe(Date.now(),addDays(2),datetoTimeout) - , value : tokenValueMinted} + , value : runtimeTokenToMarloweTokenValue(tokenValueMinted)} })) , TE.let (`swapContract`, ({swapRequest}) => Examples.SwapADAToken.mkSwapContract(swapRequest)) , TE.bindW('swapClosedResult',({runtime,adaProvider,tokenProvider,swapRequest,swapContract}) => - pipe( runtime(adaProvider).txCommand.create + pipe( runtime(adaProvider).contracts.create ( { contract: swapContract , roles: {[swapRequest.provider.roleName] : adaProvider.address ,[swapRequest.swapper.roleName] : tokenProvider.address}}) , TE.chainW ((contractId) => - runtime(adaProvider).txCommand.applyInputs(contractId) + runtime(adaProvider).contracts.applyInputs(contractId) ((next) => ({ inputs : [pipe(next.applicable_inputs.deposits[0],toInput)]}))) , TE.chainW ((contractId) => - runtime(tokenProvider).txCommand.applyInputs(contractId) + runtime(tokenProvider).contracts.applyInputs(contractId) ((next) => ({ inputs : [pipe(next.applicable_inputs.deposits[0],toInput)]}))) - , TE.chainW((contractId) => - pipe (runtime(adaProvider).restAPI.payouts.getHeadersByRange - (O.none) - ([contractId]) - ([]) - (O.none) - , TE.map((result) => result.headers))) - , TE.chainW ((payouts) => - TE.sequenceArray( - [ runtime(adaProvider).txCommand.withdraw ( { payouts : payouts - .filter(payout => payout.role.assetName === swapRequest.provider.roleName) - .map (payout => payout.payoutId)}) - , runtime(tokenProvider).txCommand.withdraw ( { payouts : payouts - .filter(payout => payout.role.assetName === swapRequest.swapper.roleName) - .map (payout => payout.payoutId)}) + , TE.chainW ((contractId) => + TE.sequenceArray( + [ pipe + ( runtime(adaProvider).payouts.available (onlyByContractIds([contractId])) + , TE.map (payoutsAvalaible => { expect(payoutsAvalaible.length).toBe(1);return payoutsAvalaible.map(payout => payout.payoutId)}) + , TE.chain (payoutIds => runtime(adaProvider).payouts.withdraw(payoutIds)) + , TE.chain ( _ => runtime(adaProvider).payouts.withdrawn (onlyByContractIds([contractId]))) + , TE.map (payoutsWthdrawn => expect(payoutsWthdrawn.length).toBe(1))) + , pipe + ( runtime(tokenProvider).payouts.available (onlyByContractIds([contractId])) + , TE.map (payoutsAvalaible => { expect(payoutsAvalaible.length).toBe(1);return payoutsAvalaible.map(payout => payout.payoutId)}) + , TE.chain (payoutIds => runtime(tokenProvider).payouts.withdraw (payoutIds)) + , TE.chain ( _ => runtime(adaProvider).payouts.withdrawn (onlyByContractIds([contractId]))) + , TE.map (payoutsWthdrawn => expect(payoutsWthdrawn.length).toBe(1)) + ) ])) )) , TE.match( (e) => { console.dir(e, { depth: null }); expect(e).not.toBeDefined()}, () => { } )) () - },1000_000); + },1_000_000); }); diff --git a/packages/runtime/api/test/jest.e2e.config.mjs b/packages/runtime/lifecycle/test/jest.e2e.config.mjs similarity index 100% rename from packages/runtime/api/test/jest.e2e.config.mjs rename to packages/runtime/lifecycle/test/jest.e2e.config.mjs diff --git a/packages/runtime/api/test/provisionning.ts b/packages/runtime/lifecycle/test/provisionning.ts similarity index 84% rename from packages/runtime/api/test/provisionning.ts rename to packages/runtime/lifecycle/test/provisionning.ts index d61f6726..5395a367 100644 --- a/packages/runtime/api/test/provisionning.ts +++ b/packages/runtime/lifecycle/test/provisionning.ts @@ -2,10 +2,11 @@ import * as TE from 'fp-ts/lib/TaskEither.js' import * as T from 'fp-ts/lib/Task.js' import { pipe } from 'fp-ts/lib/function.js' -import { tokenToString, TokenName } from '@marlowe.io/language-core-v1' -import { mkRuntime } from '@marlowe.io/runtime/overRestAPI' +import { TokenName } from '@marlowe.io/language-core-v1' +import { mkRuntimeLifecycle } from '@marlowe.io/runtime-lifecycle/overRestAPI' import { RestAPI } from '@marlowe.io/runtime-rest-client/index.js' import { Context, SingleAddressWallet, PrivateKeysAsHex } from '@marlowe.io/wallet/nodejs'; +import { assetIdToString } from '@marlowe.io/runtime-core' const log = (message:string) => console.log(`\t## - ${message}`); const formatADA = (lovelaces:bigint): String => new Intl.NumberFormat().format((lovelaces / 1_000_000n)).concat(" ₳"); @@ -27,7 +28,7 @@ export const provisionAnAdaAndTokenProvider , T.bind('tokenProvider',() => SingleAddressWallet.Random(walletContext)) , TE.fromTask // Check Banks treasury - , TE.bind('bankBalance',({bank}) => bank.adaBalance) + , TE.bind('bankBalance',({bank}) => bank.getLovelaces) , TE.chainFirst(({bankBalance,bank}) => TE.of(pipe( log(`Bank (${bank.address})`), () => log(` - ${formatADA(bankBalance)}`)))) @@ -39,24 +40,24 @@ export const provisionAnAdaAndTokenProvider , TE.bind('tokenValueMinted',({tokenProvider}) => tokenProvider.mintRandomTokens(scheme.swapper.tokenName,scheme.swapper.tokenAmount)) // Provisionning Checks // Ada Provider - , TE.bind('adaProviderBalance',({adaProvider}) => adaProvider.adaBalance) + , TE.bind('adaProviderBalance',({adaProvider}) => adaProvider.getLovelaces) , TE.chainFirst(({adaProvider,adaProviderBalance}) => TE.of(pipe( log( `Ada Provider (${adaProvider.address}`), () => log(` - ${formatADA(adaProviderBalance)}`)))) // Token Provider - , TE.bind('tokenProviderADABalance',({tokenProvider}) => tokenProvider.adaBalance) - , TE.bind('tokenBalance' ,({tokenProvider,tokenValueMinted}) => tokenProvider.tokenBalance(tokenValueMinted.token)) + , TE.bind('tokenProviderADABalance',({tokenProvider}) => tokenProvider.getLovelaces) + , TE.bind('tokenBalance' ,({tokenProvider,tokenValueMinted}) => tokenProvider.tokenBalance(tokenValueMinted.assetId)) , TE.chainFirst(({tokenProvider,tokenProviderADABalance,tokenValueMinted,tokenBalance}) => TE.of(pipe( log(`Token Provider (${tokenProvider.address})`), () => log( ` - ${formatADA(tokenProviderADABalance)}`), - () => log( ` - ${tokenBalance} ${tokenToString(tokenValueMinted.token)}`)))) + () => log( ` - ${tokenBalance} ${assetIdToString(tokenValueMinted.assetId)}`)))) , TE.chainFirst(({tokenBalance}) => TE.of(expect(tokenBalance).toBe(scheme.swapper.tokenAmount))) , TE.map (({adaProvider,tokenProvider,tokenValueMinted}) => ({ adaProvider:adaProvider , tokenProvider:tokenProvider , tokenValueMinted:tokenValueMinted , restAPI : restAPI - , runtime : mkRuntime(restAPI)}))) + , runtime : mkRuntimeLifecycle(restAPI)}))) export const initialiseBankAndverifyProvisionning @@ -67,7 +68,7 @@ export const initialiseBankAndverifyProvisionning , T.bind('bank',() => SingleAddressWallet.Initialise (walletContext,bankPrivateKey)) , TE.fromTask // Check Banks treasury - , TE.bind('bankBalance',({bank}) => bank.adaBalance) + , TE.bind('bankBalance',({bank}) => bank.getLovelaces) , TE.chainFirst(({bankBalance,bank}) => TE.of(pipe( log(`Bank (${bank.address})`), () => log(` - ${formatADA(bankBalance)}`)))) @@ -75,4 +76,4 @@ export const initialiseBankAndverifyProvisionning , TE.map (({bank}) => ({ bank : bank , restAPI : restAPI - , runtime : mkRuntime(restAPI)(bank)}))) \ No newline at end of file + , runtime : mkRuntimeLifecycle(restAPI)(bank)}))) \ No newline at end of file diff --git a/packages/runtime/api/test/tsconfig.json b/packages/runtime/lifecycle/test/tsconfig.json similarity index 93% rename from packages/runtime/api/test/tsconfig.json rename to packages/runtime/lifecycle/test/tsconfig.json index 49670368..cc5f73c2 100644 --- a/packages/runtime/api/test/tsconfig.json +++ b/packages/runtime/lifecycle/test/tsconfig.json @@ -9,7 +9,7 @@ "@marlowe.io/wallet/*": ["../../../wallet/src/*"], "@marlowe.io/runtime-core/*": ["../../core/src/*"], "@marlowe.io/runtime-rest-client/*": ["../../client/rest/src/*"], - "@marlowe.io/runtime/*": ["../src/*"] + "@marlowe.io/runtime-lifecycle/*": ["../src/*"] }, "types": ["node", "jest"] }, diff --git a/packages/runtime/api/test/tx/apply.inputs.spec.e2e.ts b/packages/runtime/lifecycle/test/tx/apply.inputs.spec.e2e.ts similarity index 92% rename from packages/runtime/api/test/tx/apply.inputs.spec.e2e.ts rename to packages/runtime/lifecycle/test/tx/apply.inputs.spec.e2e.ts index b41ea714..472ff02f 100644 --- a/packages/runtime/api/test/tx/apply.inputs.spec.e2e.ts +++ b/packages/runtime/lifecycle/test/tx/apply.inputs.spec.e2e.ts @@ -25,8 +25,8 @@ describe('Marlowe Tx Commands', () => { , TE.let (`notifyTimeout`, () => pipe(Date.now(),addDays(1),datetoTimeout)) , TE.bind('result',({restAPI,runtime,bank,notifyTimeout}) => pipe - ( runtime.txCommand.create ({ contract: oneNotifyTrue(notifyTimeout)}) - , TE.chainW ((contractId) => runtime.txCommand.applyInputs(contractId) ((next) => ({ inputs : [inputNotify]}))) + ( runtime.contracts.create ({ contract: oneNotifyTrue(notifyTimeout)}) + , TE.chainW ((contractId) => runtime.contracts.applyInputs(contractId) ((next) => ({ inputs : [inputNotify]}))) , TE.chainW ( contractId => restAPI.contracts.contract.transactions.getHeadersByRange (contractId,O.none)) , TE.map ((result) => expect(result.headers.length).toBe(1)))) , TE.match( diff --git a/packages/runtime/api/test/tx/create.spec.e2e.ts b/packages/runtime/lifecycle/test/tx/create.spec.e2e.ts similarity index 93% rename from packages/runtime/api/test/tx/create.spec.e2e.ts rename to packages/runtime/lifecycle/test/tx/create.spec.e2e.ts index e470f90c..aab14ad4 100644 --- a/packages/runtime/api/test/tx/create.spec.e2e.ts +++ b/packages/runtime/lifecycle/test/tx/create.spec.e2e.ts @@ -4,7 +4,7 @@ import * as TE from 'fp-ts/lib/TaskEither.js'; import * as O from 'fp-ts/lib/Option.js'; import { close } from '@marlowe.io/language-core-v1' -import { create } from '@marlowe.io/runtime/tx'; +import { create } from '@marlowe.io/runtime-lifecycle/tx'; import { mkRestClient } from '@marlowe.io/runtime-rest-client/index.js'; import { initialiseBankAndverifyProvisionning } from '../provisionning.js'; @@ -31,6 +31,6 @@ describe('Marlowe Tx Commands', () => { }, (result) => {console.log("contractID created" ,result.contracId) }))() - }, 100_000) + }, 1_000_000) }) diff --git a/packages/runtime/api/test/tx/withdraw.spec.e2e.ts b/packages/runtime/lifecycle/test/tx/withdraw.spec.e2e.ts similarity index 65% rename from packages/runtime/api/test/tx/withdraw.spec.e2e.ts rename to packages/runtime/lifecycle/test/tx/withdraw.spec.e2e.ts index 431a6a3c..8af3cfc2 100644 --- a/packages/runtime/api/test/tx/withdraw.spec.e2e.ts +++ b/packages/runtime/lifecycle/test/tx/withdraw.spec.e2e.ts @@ -1,6 +1,5 @@ import { pipe } from 'fp-ts/lib/function.js'; import * as TE from 'fp-ts/lib/TaskEither.js'; -import * as O from 'fp-ts/lib/Option.js'; import { addDays } from 'date-fns/fp'; import * as Examples from '@marlowe.io/language-core-v1/examples'; @@ -11,10 +10,12 @@ import { mkRestClient } from '@marlowe.io/runtime-rest-client/index.js'; import { getBankPrivateKey, getBlockfrostContext, getMarloweRuntimeUrl } from '../context.js'; import { provisionAnAdaAndTokenProvider } from '../provisionning.js'; import console from "console" +import { runtimeTokenToMarloweTokenValue } from '@marlowe.io/runtime-core'; +import { onlyByContractIds } from '@marlowe.io/runtime-lifecycle/api'; global.console = console -describe('Marlowe Tx Commands', () => { +describe('Payouts', () => { const restAPI = mkRestClient(getMarloweRuntimeUrl()) const provisionScheme = @@ -35,51 +36,43 @@ describe('Marlowe Tx Commands', () => { , swapper : { roleName : 'Token provider' , depositTimeout : pipe(Date.now(),addDays(2),datetoTimeout) - , value : tokenValueMinted} + , value : runtimeTokenToMarloweTokenValue(tokenValueMinted)} })) , TE.let (`swapContract`, ({swapRequest}) => Examples.SwapADAToken.mkSwapContract(swapRequest)) , TE.bindW('contractId',({runtime,adaProvider,tokenProvider,swapRequest,swapContract}) => - pipe( runtime(adaProvider).txCommand.create + pipe( runtime(adaProvider).contracts.create ( { contract: swapContract , roles: {[swapRequest.provider.roleName] : adaProvider.address ,[swapRequest.swapper.roleName] : tokenProvider.address}}) , TE.chainW ((contractId) => - runtime(adaProvider).txCommand.applyInputs + runtime(adaProvider).contracts.applyInputs (contractId) ((next) => ({ inputs : [pipe(next.applicable_inputs.deposits[0],toInput)]}))) , TE.chainW (contractId => - runtime(tokenProvider).txCommand.applyInputs + runtime(tokenProvider).contracts.applyInputs (contractId) - ((next) => ({ inputs : [pipe(next.applicable_inputs.deposits[0],toInput)]}))))) - , TE.bindW(`payouts`,({contractId}) => - pipe (restAPI.payouts.getHeadersByRange - (O.none) - ([contractId]) - ([]) - (O.none) - , TE.map((result) => result.headers)) - )) + ((next) => ({ inputs : [pipe(next.applicable_inputs.deposits[0],toInput)]})))))) - it('can withdraw' , async () => { + it('Payouts can be withdrawn' , async () => { await pipe( executeSwapWithRequiredWithdrawalTillClosing - , TE.bindW('result',({adaProvider,tokenProvider,payouts,swapRequest,runtime}) => + , TE.bindW('result',({adaProvider,tokenProvider,contractId,runtime}) => TE.sequenceArray( - [ runtime(adaProvider).txCommand.withdraw - ( { payouts : payouts - .filter(payout => payout.role.assetName === swapRequest.provider.roleName) - .map (payout => payout.payoutId)}) - , runtime(tokenProvider).txCommand.withdraw - ( { payouts : payouts - .filter(payout => payout.role.assetName === swapRequest.swapper.roleName) - .map (payout => payout.payoutId)}) + [ pipe + ( runtime(adaProvider).payouts.available (onlyByContractIds([contractId])) + , TE.map (payouts => { expect(payouts.length).toBe(1);return payouts.map(payout => payout.payoutId)}) + , TE.chain (payoutIds => runtime(adaProvider).payouts.withdraw(payoutIds))) + , pipe + ( runtime(tokenProvider).payouts.available (onlyByContractIds([contractId])) + , TE.map (payouts => { expect(payouts.length).toBe(1);return payouts.map(payout => payout.payoutId)}) + , TE.chain (payoutIds => runtime(tokenProvider).payouts.withdraw (payoutIds))) ])) , TE.match( (e) => { console.dir(e, { depth: null }); expect(e).not.toBeDefined()}, () => {} )) () - },1000_000); + },1_000_000); }); diff --git a/packages/runtime/api/typedoc.json b/packages/runtime/lifecycle/typedoc.json similarity index 100% rename from packages/runtime/api/typedoc.json rename to packages/runtime/lifecycle/typedoc.json diff --git a/packages/wallet/src/api.ts b/packages/wallet/src/api.ts index 4f8f753f..04539673 100644 --- a/packages/wallet/src/api.ts +++ b/packages/wallet/src/api.ts @@ -4,8 +4,8 @@ import * as O from 'fp-ts/lib/Option.js' import { pipe } from 'fp-ts/lib/function.js'; import * as TE from 'fp-ts/lib/TaskEither.js' -import { AddressBech32, AddressesAndCollaterals, HexTransactionWitnessSet, MarloweTxCBORHex, TxOutRef } from '@marlowe.io/runtime-core' -import { TokenValue } from '@marlowe.io/language-core-v1/semantics/contract/common/tokenValue.js'; +import { AddressBech32, AddressesAndCollaterals, HexTransactionWitnessSet, MarloweTxCBORHex, Token, TxOutRef } from '@marlowe.io/runtime-core' + // N.B : Network Id returned by CIP30 Interface doesn't provide information on which Testnet Network @@ -19,7 +19,8 @@ export interface WalletAPI { getUsedAddresses : T.Task getCollaterals : T.Task getCIP30Network: T.Task - getTokenValues : TE.TaskEither + getTokens : TE.TaskEither + getLovelaces : TE.TaskEither } export const getAddressesAndCollaterals : (walletAPI : WalletAPI) => T.Task = diff --git a/packages/wallet/src/browser/index.ts b/packages/wallet/src/browser/index.ts index 90ca204f..08f583de 100644 --- a/packages/wallet/src/browser/index.ts +++ b/packages/wallet/src/browser/index.ts @@ -3,13 +3,10 @@ import * as TE from 'fp-ts/lib/TaskEither.js' import { pipe } from 'fp-ts/lib/function.js'; import * as A from 'fp-ts/lib/Array.js' -import { token } from '@marlowe.io/language-core-v1/token'; -import { TokenValue, lovelaceValue, tokenValue } from '@marlowe.io/language-core-v1/tokenValue'; - import { CIP30Network, WalletAPI } from '../api.js'; import { C,Core } from 'lucid-cardano'; import { hex, utf8 } from '@47ng/codec' -import { MarloweTxCBORHex, HexTransactionWitnessSet, AddressBech32, TxOutRef, addressBech32, txOutRef } from '@marlowe.io/runtime-core'; +import { MarloweTxCBORHex, HexTransactionWitnessSet, AddressBech32, TxOutRef, addressBech32, txOutRef, Token, token,lovelaces, assetId, mkPolicyId } from '@marlowe.io/runtime-core'; export const getExtensionInstance : (extensionName : string) => T.Task = (extensionName) => @@ -20,7 +17,8 @@ export const getExtensionInstance : (extensionName : string) => T.Task TE.TaskEither +const fetchTokens : (extensionCIP30Instance : BroswerExtensionCIP30Api) => TE.TaskEither = (extensionCIP30Instance) => pipe ( () => extensionCIP30Instance.getBalance() - , T.map((balances) => fromValue(deserializeValue(balances))) + , T.map((balances) => pipe(balances,deserializeValue,valueToTokens)) , TE.fromTask) const deserializeValue = (value: string) => C.Value.from_bytes(hex.decode(value)); -const fromValue = (value: Core.Value) => { - const tokenValues: TokenValue[] = [ lovelaceValue(BigInt(value.coin().to_str()).valueOf())] +const valueToTokens = (value: Core.Value) => { + const tokenValues: Token[] = [ lovelaces(valueToLovelaces(value))] const multiAsset = value.multiasset(); if (multiAsset !== undefined) { @@ -128,11 +126,11 @@ const fromValue = (value: Core.Value) => { const assetName = policyAssetNames.get(j); const quantity = policyAssets.get(assetName) ?? C.BigNum.from_str('0'); tokenValues.push( - tokenValue + token (BigInt(quantity.to_str()).valueOf()) - (token - (policyId.to_hex() - ,utf8.decode(assetName.to_bytes()).substring(1) // N.H : investigate why 1 aditional character is returned + (assetId + (mkPolicyId(policyId.to_hex())) + (utf8.decode(assetName.to_bytes()).substring(1) // N.H : investigate why 1 aditional character is returned ))); } } @@ -140,4 +138,15 @@ const fromValue = (value: Core.Value) => { } return tokenValues; -}; \ No newline at end of file +}; + +const fetchLovelaces : (extensionCIP30Instance : BroswerExtensionCIP30Api) => TE.TaskEither + = (extensionCIP30Instance) => + pipe + ( () => extensionCIP30Instance.getBalance() + , T.map((balances) => pipe(balances,deserializeValue,valueToLovelaces)) + , TE.fromTask) + +const valueToLovelaces = (value: Core.Value) => BigInt(value.coin().to_str()).valueOf() + + \ No newline at end of file diff --git a/packages/wallet/src/nodejs/index.ts b/packages/wallet/src/nodejs/index.ts index dc5519b6..e0dc744d 100644 --- a/packages/wallet/src/nodejs/index.ts +++ b/packages/wallet/src/nodejs/index.ts @@ -6,10 +6,9 @@ import * as O from 'fp-ts/lib/Option.js' import * as TE from 'fp-ts/lib/TaskEither.js' import * as T from 'fp-ts/lib/Task.js' -import { Token, token, TokenValue, adaValue, tokenValue } from '@marlowe.io/language-core-v1'; -import { AddressBech32, TxOutRef, addressBech32, unAddressBech32, MarloweTxCBORHex, HexTransactionWitnessSet } from '@marlowe.io/runtime-core'; +import { AddressBech32, TxOutRef, addressBech32, unAddressBech32, MarloweTxCBORHex, HexTransactionWitnessSet, Token, lovelaces, token, assetId, mkPolicyId, unPolicyId, AssetId } from '@marlowe.io/runtime-core'; import { WalletAPI, CIP30Network } from '../api.js'; - +import * as Codec from '@47ng/codec' const log = (message:string) => console.log(`\t## - ${message}`); @@ -71,20 +70,21 @@ export class SingleAddressWallet implements WalletAPI { { if (this.lucid.network === "Mainnet") { return Promise.resolve("Mainnet")} else {return Promise.resolve("Testnets")}} - public getTokenValues: TE.TaskEither + public getTokens: TE.TaskEither = pipe( TE.tryCatch( () => this.blockfrostApi.addresses(unAddressBech32(this.address)), (reason) => new Error(`Error while retrieving assetBalance : ${reason}`)) , TE.map( (content) => pipe(content.amount??[] - , A.map((amount) => amount.unit === "lovelace" ? - adaValue(BigInt(amount.quantity)) - : tokenValue - (BigInt(amount.quantity).valueOf()) - (token( fromUnit(amount.unit).policyId - , getAssetName(amount.unit))) )))) + , A.map((tokenBlockfrost) => tokenBlockfrost.unit === "lovelace" ? + lovelaces(BigInt(tokenBlockfrost.quantity)) + : token + (BigInt(tokenBlockfrost.quantity).valueOf()) + (assetId + (mkPolicyId(fromUnit(tokenBlockfrost.unit).policyId)) + (getAssetName(tokenBlockfrost.unit))) )))) - public adaBalance : TE.TaskEither + public getLovelaces : TE.TaskEither = pipe( TE.tryCatch( () => this.blockfrostApi.addresses(unAddressBech32(this.address)), (reason) => new Error(`Error while retrieving assetBalance : ${reason}`)) @@ -95,13 +95,13 @@ export class SingleAddressWallet implements WalletAPI { , O.getOrElse(() => 0n)))) - public tokenBalance : (token : Token) => TE.TaskEither - = (token) => + public tokenBalance : (assetId : AssetId) => TE.TaskEither + = (assetId) => pipe(TE.tryCatch( () => this.blockfrostApi.addresses(unAddressBech32(this.address)), (reason) => new Error(`Error while retrieving assetBalance : ${reason}`)) , TE.map( (content) => pipe(content.amount??[] - , A.filter((amount) => amount.unit === toUnit(token.currency_symbol, fromText(token.token_name))) + , A.filter((amount) => amount.unit === toUnit(unPolicyId(assetId.policyId), fromText(assetId.assetName))) , A.map((amount) => BigInt(amount.quantity)) , A.head , O.getOrElse(() => 0n)))) @@ -132,19 +132,22 @@ export class SingleAddressWallet implements WalletAPI { return [script,policyId]; } - public mintRandomTokens(tokenName:string, amount: bigint) : TE.TaskEither { + public mintRandomTokens(assetName:string, amount: bigint) : TE.TaskEither { const policyRefs = this.randomPolicyId (); const [mintingPolicy,policyId] = policyRefs return pipe( this.lucid.newTx() - .mintAssets({[toUnit(policyId, fromText(tokenName))]: amount.valueOf()}) + .mintAssets({[toUnit(policyId, fromText(assetName))]: amount.valueOf()}) .validTo(Date.now() + 100000) .attachMintingPolicy(mintingPolicy) , build , TE.chain(this.signSubmitAndWaitConfirmation) , TE.map(() => - ( { amount:amount - , token:token(policyRefs[1],tokenName) - }))) + ( token + (amount) + (assetId + (mkPolicyId(policyRefs[1])) + (assetName))) + )) } @@ -199,8 +202,7 @@ const build : (tx : Tx ) => TE.TaskEither (reason) => new Error(`Error while building Tx : ${reason}`)); - const getAssetName : (unit : Unit) => string = (unit) => { const assetName = fromUnit(unit).assetName - return assetName ? assetName : '' + return assetName ? Codec.hexToUTF8(assetName) : '' } \ No newline at end of file diff --git a/rollup/runtime-esm.config.mjs b/rollup/runtime-esm.config.mjs index 7d3e8a09..a5e99375 100644 --- a/rollup/runtime-esm.config.mjs +++ b/rollup/runtime-esm.config.mjs @@ -19,7 +19,7 @@ const nodePlugin = nodeResolve( export default { // input: 'packages/language/core/v1/dist/semantics/contract/index.js', // input: 'packages/adapter/dist/index.js', - input: 'packages/runtime/api/dist/index.js', + input: 'packages/runtime/lifecycle/dist/index.js', output: { dir: outputDir, format: 'esm', diff --git a/tsconfig.json b/tsconfig.json index 9726e96d..1af7e11c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,6 @@ { "path": "./packages/wallet/src" }, { "path": "./packages/runtime/client/rest/src" }, { "path": "./packages/runtime/core/src" }, - { "path": "./packages/runtime/api/src" } + { "path": "./packages/runtime/lifecycle/src" } ] } \ No newline at end of file