Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dc9a56b

Browse files
committedDec 5, 2023
update runtime.lifecycle createContract API
1 parent 226c785 commit dc9a56b

File tree

20 files changed

+387
-201
lines changed

20 files changed

+387
-201
lines changed
 

‎examples/get-my-contract-ids-flow/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
window.createContract = async () => {
4242
const address = await window.runtimeLifeCycle.wallet.getChangeAddress();
4343
return runtimeLifeCycle.contracts.createContract({
44-
contract: mkContract(address, Date.now() + 1000 * 60 * 10),
44+
contractOrSourceId: mkContract(address, Date.now() + 1000 * 60 * 10),
4545
});
4646
};
4747
</script>

‎examples/run-lite/index.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ <h2>Console</h2>
108108
walletName,
109109
});
110110

111-
const contractId = await runtime.contracts.createContract({ contract });
111+
const contractId = await runtime.contracts.createContract({
112+
contractOrSourceId: contract,
113+
});
112114
log("Contract created with id: " + contractId);
113115
setContractIdIndicator(contractId);
114116
}

‎examples/survey-workshop/participant/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async function createContract(
124124

125125
const lifecycle = await H.getLifecycle();
126126
const [contractId] = await lifecycle.contracts.createContract({
127-
contract,
127+
contractOrSourceId: contract,
128128
tags: { MarloweSurvey: "test 1" },
129129
});
130130

‎examples/vesting-flow/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ <h2>Console</h2>
116116

117117
const [contractId, txIdCreated] =
118118
await runtimeLifeCycle.contracts.createContract({
119-
contract: vestingContract,
119+
contractOrSourceId: vestingContract,
120120
tags: { [dappId]: { scheme: request.scheme } },
121121
});
122122
log(

‎flake.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎jsdelivr-npm-importmap.js

+40-70
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/runtime/client/rest/src/contract/details.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const Payout = t.type({
3232
*/
3333
payoutId: TxOutRef,
3434
/**
35-
* The {@link RoleName} of the participant that has the unclaimed Payout.
35+
* The {@link @marlowe.io/language-core-v1!index.RoleName | Role Name} of the participant that has the unclaimed Payout.
3636
*/
3737
role: G.RoleName,
3838
});

‎packages/runtime/client/rest/src/contract/endpoints/collection.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,8 @@ export interface BuildCreateContractTxRequest {
370370
* - **Quantities to create(Mint)** : When asking to mint the tokens within the Runtime, quantities can defined as well.
371371
*
372372
* Smart Constructors are available to ease these configuration:
373-
* - {@link @marlowe.io/runtime-rest-client!contract.mkUseMintedRoleTokens}
374-
* - {@link @marlowe.io/runtime-rest-client!contract.mkMintOpenRoleToken}
375-
* - {@link @marlowe.io/runtime-rest-client!contract.mkMintClosedRoleToken}
373+
* - {@link @marlowe.io/runtime-rest-client!contract.useMintedRoles}
374+
* - {@link @marlowe.io/runtime-rest-client!contract.mintRole}
376375
*
377376
* @remarks
378377
* - The Distribution can be a mix of Closed and Open Role Tokens configuration. See examples below.
@@ -382,13 +381,13 @@ export interface BuildCreateContractTxRequest {
382381
*
383382
* ```ts
384383
* //////////////
385-
* // #1 - Mint within the Runtime
384+
* // #1 - Mint Role Tokens
386385
* //////////////
387386
* const anAddressBech32 = "addr_test1qqe342swyfn75mp2anj45f8ythjyxg6m7pu0pznptl6f2d84kwuzrh8c83gzhrq5zcw7ytmqc863z5rhhwst3w4x87eq0td9ja"
388387
* const aMintingConfiguration =
389-
* { "closed_Role_A_NFT" : mkMintClosedRoleToken(anAddressBech32)
388+
* { "closed_Role_A_NFT" : mintRole(anAddressBech32)
390389
* , "closed_Role_B_FT" :
391-
* mkMintClosedRoleToken(
390+
* mintRole(
392391
* anAddressBech32,
393392
* 5, // Quantities
394393
* { "name": "closed_Role_B_FT Marlowe Role Token",
@@ -403,11 +402,12 @@ export interface BuildCreateContractTxRequest {
403402
* }
404403
* ]
405404
* })
406-
* , "open_Role_C" : mkMintOpenRoleToken()
407-
* , "open_Role_D" : mkMintOpenRoleToken(
405+
* , "open_Role_C" : mintRole(openRole)
406+
* , "open_Role_D" : mintRole(
407+
* openRole,
408408
* 2, // Quantities
409409
* { "name": "open_Role_D Marlowe Role Token",
410-
"description": "These are metadata for closedRoleB",
410+
* "description": "These are metadata for closedRoleB",
411411
* image": "ipfs://QmaQMH7ybS9KmdYQpa4FMtAhwJH5cNaacpg4fTwhfPvcwj",
412412
* "mediaType": "image/png",
413413
* "files": [
@@ -423,16 +423,15 @@ export interface BuildCreateContractTxRequest {
423423
* //////////////
424424
* // #2 Use Minted Roles Tokens
425425
* const aUseMintedRoleTokensConfiguration =
426-
* mkUseMintedRoleTokens(
426+
* useMintedRoles(
427427
* "e68f1cea19752d1292b4be71b7f5d2b3219a15859c028f7454f66cdf",
428428
* ["role_A","role_C"]
429429
* )
430430
* ```
431431
*
432432
* @see
433-
* - {@link @marlowe.io/runtime-rest-client!contract.mkUseMintedRoleTokens}
434-
* - {@link @marlowe.io/runtime-rest-client!contract.mkMintOpenRoleToken}
435-
* - {@link @marlowe.io/runtime-rest-client!contract.mkMintClosedRoleToken}
433+
* - {@link @marlowe.io/runtime-rest-client!contract.useMintedRoles}
434+
* - {@link @marlowe.io/runtime-rest-client!contract.mintRole}
436435
* - Open Roles Runtime Implementation : https://github.com/input-output-hk/marlowe-cardano/blob/main/marlowe-runtime/doc/open-roles.md
437436
*/
438437
rolesConfiguration?: RolesConfiguration;

‎packages/runtime/client/rest/src/contract/index.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
export { ContractHeader } from "./header.js";
1515
export { ContractDetails } from "./details.js";
1616
export {
17-
mkUseMintedRoleTokens,
18-
mkMintClosedRoleToken,
19-
mkMintOpenRoleToken,
17+
useMintedRoles,
18+
mintRole,
2019
AddressBech32Brand,
2120
AddressBech32,
22-
mkOpenRole,
21+
openRole,
2322
ClosedRole,
2423
OpenRole,
25-
Openess,
24+
Openness,
2625
UsePolicyWithClosedRoleTokens,
2726
UsePolicyWithOpenRoleTokens,
2827
MintRolesTokens,

‎packages/runtime/client/rest/src/contract/rolesConfigurations.ts

+21-31
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ export const OpenRoleGuard = t.literal("OpenRole");
4343
* Construction of an Open Role Token
4444
* @category Roles Configuration
4545
*/
46-
export const mkOpenRole = "OpenRole";
46+
export const openRole = "OpenRole";
4747

4848
/**
4949
* Definition of the Openness of a Role Token
5050
* @category Roles Configuration
5151
*/
52-
export type Openess = ClosedRole | OpenRole;
53-
export const OpenessGuard: t.Type<Openess, string> = t.union([
52+
export type Openness = ClosedRole | OpenRole;
53+
export const OpenessGuard: t.Type<Openness, string> = t.union([
5454
ClosedRoleGuard,
5555
OpenRoleGuard,
5656
]);
@@ -129,7 +129,7 @@ export interface ClosedNFTWithMetadata {
129129
/**
130130
* @category Roles Configuration
131131
*/
132-
export type Recipient = Openess;
132+
export type Recipient = Openness;
133133

134134
export const RecipientGuard: t.Type<Recipient, string> = t.union([
135135
OpenRoleGuard,
@@ -178,9 +178,8 @@ export const MintRolesTokensGuard: t.Type<MintRolesTokens> = t.record(
178178
* Defines how to configure Roles over Cardano at the creation of a Marlowe Contract.
179179
* @see
180180
* Smart Constructors are available to ease the configuration:
181-
* - {@link @marlowe.io/runtime-rest-client!contract.mkUseMintedRoleTokens}
182-
* - {@link @marlowe.io/runtime-rest-client!contract.mkMintOpenRoleToken}
183-
* - {@link @marlowe.io/runtime-rest-client!contract.mkMintClosedRoleToken}
181+
* - {@link @marlowe.io/runtime-rest-client!contract.useMintedRoles}
182+
* - {@link @marlowe.io/runtime-rest-client!contract.mintRole}
184183
* @category Endpoint : Build Create Contract Tx
185184
* @category Roles Configuration
186185
*/
@@ -209,42 +208,33 @@ export const RolesConfigurationGuard = t.union([
209208
* @category Endpoint : Build Create Contract Tx
210209
* @category Roles Configuration
211210
*/
212-
export const mkUseMintedRoleTokens = (
211+
export const useMintedRoles = (
213212
policyId: PolicyId,
214213
openRoleNames?: RoleName[]
215214
): RolesConfiguration =>
216215
openRoleNames
217-
? { script: mkOpenRole, policyId: policyId, openRoleNames: openRoleNames }
216+
? { script: openRole, policyId: policyId, openRoleNames: openRoleNames }
218217
: (policyId as UsePolicyWithClosedRoleTokens);
219218

220219
/**
221220
* Configure the minting of a Closed Role Token.
222-
* @param address where to distribute the token that will be mint
221+
* @param openness where to distribute the token (Either openly or closedly)
223222
* @param quantity Quantity of the Closed Role Token (by Default an NFT (==1))
224223
* @param metadata Token Metadata of the Token
225224
* @category Endpoint : Build Create Contract Tx
226225
* @category Roles Configuration
227226
*/
228-
export const mkMintClosedRoleToken: (
229-
address: AddressBech32,
227+
export const mintRole = (
228+
openness: Openness,
230229
quantity?: TokenQuantity,
231230
metadata?: TokenMetadata
232-
) => RoleTokenConfiguration = (address, quantity, metadata) => ({
233-
recipients: { [address]: quantity ? quantity : 1n },
234-
metadata: metadata,
235-
});
236-
237-
/**
238-
* Configure the minting of an Open Role Token.
239-
* @param quantity Quantity of the Closed Role Token (by Default an NFT (==1))
240-
* @param metadata Token Metadata of the Token
241-
* @category Endpoint : Build Create Contract Tx
242-
* @category Roles Configuration
243-
*/
244-
export const mkMintOpenRoleToken: (
245-
quantity?: TokenQuantity,
246-
metadata?: TokenMetadata
247-
) => RoleTokenConfiguration = (quantity, metadata) => ({
248-
recipients: { [mkOpenRole]: quantity ? quantity : 1n },
249-
metadata: metadata,
250-
});
231+
): RoleTokenConfiguration =>
232+
OpenRoleGuard.is(openness)
233+
? {
234+
recipients: { [openRole]: quantity ? quantity : 1n },
235+
metadata: metadata,
236+
}
237+
: {
238+
recipients: { [openness]: quantity ? quantity : 1n },
239+
metadata: metadata,
240+
};

‎packages/runtime/client/rest/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ export interface ContractsAPI {
448448
* @description Dependency Injection for the Rest Client API
449449
* @hidden
450450
*/
451-
export type RestDI = { rest: FPTSRestAPI };
451+
export type RestDI = { deprecatedRestAPI: FPTSRestAPI; restClient: RestClient };
452452

453453
/**
454454
* @hidden

‎packages/runtime/lifecycle/src/api.ts

+178-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@ import {
66
PayoutAvailable,
77
PayoutId,
88
PayoutWithdrawn,
9+
StakeAddressBech32,
910
Tags,
1011
TxId,
1112
} from "@marlowe.io/runtime-core";
1213
import { RestDI } from "@marlowe.io/runtime-rest-client";
13-
import { RolesConfiguration } from "@marlowe.io/runtime-rest-client/contract";
14+
import {
15+
ContractOrSourceId,
16+
RolesConfiguration,
17+
} from "@marlowe.io/runtime-rest-client/contract";
1418
import { ISO8601 } from "@marlowe.io/adapter/time";
15-
import { Contract, Environment, Input } from "@marlowe.io/language-core-v1";
19+
import {
20+
Contract,
21+
Environment,
22+
Input,
23+
RoleName,
24+
} from "@marlowe.io/language-core-v1";
1625
import { Next } from "@marlowe.io/language-core-v1/next";
1726

1827
export type RuntimeLifecycle = {
@@ -29,10 +38,175 @@ export type RuntimeLifecycle = {
2938
export type ContractsDI = WalletDI & RestDI;
3039

3140
export type CreateContractRequest = {
32-
contract: Contract;
33-
roles?: RolesConfiguration;
41+
/**
42+
* A Marlowe Contract or a Merkleized One (referred by its source Id) to create over Cardano
43+
* @see Large/Deep Contracts Support (Contract Merkleization) and `@marlowe.io/language-core`
44+
*/
45+
contractOrSourceId: ContractOrSourceId;
46+
47+
/**
48+
* The Marlowe Runtime utilizes this Optional field to set a stake address
49+
* where to send staking rewards for the Marlowe script outputs of this contract.
50+
*/
51+
stakeAddress?: StakeAddressBech32;
52+
/**
53+
* @experimental
54+
* Thread Roles are a details of implementation within the runtime. It allows provide a custom name
55+
* if the thread role name is conflicting with other role names used.
56+
* @default
57+
* - the Thread Role Name is "" by default.
58+
*/
59+
threadRoleName?: RoleName;
60+
61+
/**
62+
* Role Token Configuration for the contract passed in the `contractOrSourceId` field.
63+
*
64+
* <h4>Prerequisite</h4>
65+
* <p>
66+
* Participants ({@link @marlowe.io/language-core-v1!index.Party | Party}) in a Marlowe Contract can be expressed in 2 ways:
67+
*
68+
* 1. **By Adressses** : Addresses are directly defined within the Marlowe Contract and no configuration are necessary in that context.
69+
* 2. **By Roles** : Defined by {@link @marlowe.io/language-core-v1!index.RoleName | RoleNames} within the Marlowe Contract, they have to match a Token Name when created in Cardano. This field `rolesConfiguration` is about configuring this use case
70+
* </p>
71+
*
72+
* <h4>Configuration Options</h4>
73+
* <p>
74+
*
75+
* - **When to create (mint)**
76+
* - **Within the Runtime** : At the contrat creation, these defined Roles Tokens will be minted "on the fly" by the runtime.
77+
* - **Without the Runtime** : before the creation, these Role Tokens are already defined (via an NFT platform, `cardano-cli`, another Marlowe Contract Created, etc.. )
78+
* - **How to distribute**
79+
* - **Closedly** (Closed Roles) : At the creation of contract or before, the Role Tokens are released to the participants. All the participants are knowned at the creation and therfore we consider the participation as being closed.
80+
* - **Openly** (Open Roles) : Whoever applies an input (IDeposit or IChoice) on the contract `contractOrSourceId` first will be identified as a participant by receiving the Role Token in their wallet. In that case, participants are unknown at the creation and the participation is open to any meeting the criteria.
81+
* - **With or without Metadata**
82+
* - **Quantities to create(Mint)** : When asking to mint the tokens within the Runtime, quantities can defined as well.
83+
*
84+
* Smart Constructors are available to ease these configuration:
85+
* - {@link @marlowe.io/runtime-rest-client!contract.useMintedRoles}
86+
* - {@link @marlowe.io/runtime-rest-client!contract.mintRole}
87+
*
88+
* @remarks
89+
* - The Distribution can be a mix of Closed and Open Role Tokens configuration. See examples below.
90+
* </p>
91+
*
92+
* @example
93+
*
94+
* ```ts
95+
* //////////////
96+
* // #1 - Mint Role Tokens
97+
* //////////////
98+
* const anAddressBech32 = "addr_test1qqe342swyfn75mp2anj45f8ythjyxg6m7pu0pznptl6f2d84kwuzrh8c83gzhrq5zcw7ytmqc863z5rhhwst3w4x87eq0td9ja"
99+
* const aMintingConfiguration =
100+
* { "closed_Role_A_NFT" : mintRole(anAddressBech32)
101+
* , "closed_Role_B_FT" :
102+
* mintRole(
103+
* anAddressBech32,
104+
* 5, // Quantities
105+
* { "name": "closed_Role_B_FT Marlowe Role Token",
106+
"description": "These are metadata for closedRoleB",
107+
* image": "ipfs://QmaQMH7ybS9KmdYQpa4FMtAhwJH5cNaacpg4fTwhfPvcwj",
108+
* "mediaType": "image/png",
109+
* "files": [
110+
* {
111+
* "name": "icon-1000",
112+
* "mediaType": "image/webp",
113+
* "src": "ipfs://QmUbvavFxGSSEo3ipQf7rjrELDvXHDshWkHZSpV8CVdSE5"
114+
* }
115+
* ]
116+
* })
117+
* , "open_Role_C" : mkMintOpenRoleToken()
118+
* , "open_Role_D" : mkMintOpenRoleToken(
119+
* 2, // Quantities
120+
* { "name": "open_Role_D Marlowe Role Token",
121+
"description": "These are metadata for closedRoleB",
122+
* image": "ipfs://QmaQMH7ybS9KmdYQpa4FMtAhwJH5cNaacpg4fTwhfPvcwj",
123+
* "mediaType": "image/png",
124+
* "files": [
125+
* {
126+
* "name": "icon-1000",
127+
* "mediaType": "image/webp",
128+
* "src": "ipfs://QmUbvavFxGSSEo3ipQf7rjrELDvXHDshWkHZSpV8CVdSE5"
129+
* }
130+
* ]
131+
* })
132+
* }
133+
*
134+
* //////////////
135+
* // #2 Use Minted Roles Tokens
136+
* const aUseMintedRoleTokensConfiguration =
137+
* useMintedRoles(
138+
* "e68f1cea19752d1292b4be71b7f5d2b3219a15859c028f7454f66cdf",
139+
* ["role_A","role_C"]
140+
* )
141+
* ```
142+
*
143+
* @see
144+
* - {@link @marlowe.io/runtime-rest-client!contract.useMintedRoles}
145+
* - {@link @marlowe.io/runtime-rest-client!contract.mintRole}
146+
* - Open Roles Runtime Implementation : https://github.com/input-output-hk/marlowe-cardano/blob/main/marlowe-runtime/doc/open-roles.md
147+
*/
148+
rolesConfiguration?: RolesConfiguration;
149+
150+
/**
151+
* Marlowe Tags are stored as Metadata within the Transaction Metadata under the top-level Marlowe Reserved Key (`1564`).
152+
* Tags allows to Query created Marlowe Contracts via {@link @marlowe.io/runtime-rest-client!index.RestClient#getContracts | Get contracts }
153+
*
154+
* <h4>Properties</h4>
155+
*
156+
* 1. They aren't limited size-wise like regular metadata fields are over Cardano.
157+
* 2. Metadata can be associated under each tag
158+
*
159+
* @example
160+
* ```ts
161+
* const myTags : Tags = { "My Tag 1 That can be as long as I want": // Not limited to 64 bytes
162+
* { a: 0
163+
* , b : "Tag 1 content" // Limited to 64 bytes (Cardano Metadata constraint)
164+
* },
165+
* "MyTag2": { c: 0, d : "Tag 2 content"}};
166+
* ```
167+
*/
34168
tags?: Tags;
169+
/**
170+
* Cardano Metadata about the contract creation.
171+
* <h4>Properties</h4>
172+
* <p>
173+
* Metadata can be expressed as a JSON object with some restrictions:
174+
* - All top-level keys must be integers between 0 and 2^64 - 1.
175+
* - Each metadata value is tagged with its type.
176+
* - Strings must be at most 64 bytes when UTF-8 is encoded.
177+
* - Bytestrings are hex-encoded, with a maximum length of 64 bytes.
178+
*
179+
* Metadata aren't stored as JSON on the Cardano blockchain but are instead stored using a compact binary encoding (CBOR).
180+
* The binary encoding of metadata values supports three simple types:
181+
* - Integers in the range `-(2^64 - 1)` to `2^64 - 1`
182+
* - Strings (UTF-8 encoded)
183+
* - Bytestrings
184+
* - And two compound types:
185+
* - Lists of metadata values
186+
* - Mappings from metadata values to metadata values
187+
* </p>
188+
* It is possible to transform any JSON object into this schema (See https://developers.cardano.org/docs/transaction-metadata )
189+
* @see
190+
* https://developers.cardano.org/docs/transaction-metadata
191+
*/
35192
metadata?: Metadata;
193+
194+
/**
195+
* Minimum Lovelace value to add on the UTxO created (Representing the Marlowe Contract Created on the ledger).This value
196+
* is computed automatically within the Runtime, so this parameter is only necessary if you need some custom adjustment.
197+
*
198+
* <h4>Justification</h4>
199+
* <p>Creating a Marlowe Contracts over Cardano is about creating UTxO entries on the Ledger.
200+
*
201+
* To protect the ledger from growing beyond a certain size that will cost too much to maintain,
202+
* a constraint called "Minimum ada value requirement (mininmumLovelaceUTxODeposit)" that adjust
203+
* the value (in ADA) of each UTxO has been added.
204+
*
205+
* The more the UTxOs entries are big in size, the more the value of minimum
206+
* of ADAs needs to be contained.</p>
207+
* @see
208+
* https://docs.cardano.org/native-tokens/minimum-ada-value-requirement
209+
*/
36210
mininmumLovelaceUTxODeposit?: number;
37211
};
38212

‎packages/runtime/lifecycle/src/browser/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {
44
} from "@marlowe.io/wallet/browser";
55
import * as Generic from "../generic/runtime.js";
66

7-
import { mkFPTSRestClient } from "@marlowe.io/runtime-rest-client";
7+
import {
8+
mkFPTSRestClient,
9+
mkRestClient,
10+
} from "@marlowe.io/runtime-rest-client";
811

912
/**
1013
* Options for creating a RuntimeLifecycle instance using the browser wallet.
@@ -31,6 +34,7 @@ export async function mkRuntimeLifecycle({
3134
walletName,
3235
}: BrowserRuntimeLifecycleOptions) {
3336
const wallet = await mkBrowserWallet(walletName);
34-
const restClient = mkFPTSRestClient(runtimeURL);
35-
return Generic.mkRuntimeLifecycle(restClient, wallet);
37+
const deprecatedRestAPI = mkFPTSRestClient(runtimeURL);
38+
const restClient = mkRestClient(runtimeURL);
39+
return Generic.mkRuntimeLifecycle(deprecatedRestAPI, restClient, wallet);
3640
}

‎packages/runtime/lifecycle/src/generic/contracts.ts

+56-39
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ import {
1919
HexTransactionWitnessSet,
2020
unAddressBech32,
2121
unPolicyId,
22+
transactionWitnessSetTextEnvelope,
2223
} from "@marlowe.io/runtime-core";
2324

24-
import { FPTSRestAPI } from "@marlowe.io/runtime-rest-client";
25+
import { FPTSRestAPI, RestClient } from "@marlowe.io/runtime-rest-client";
2526
import { DecodingError } from "@marlowe.io/adapter/codec";
2627

2728
import { Next, noNext } from "@marlowe.io/language-core-v1/next";
2829
import { isNone, none, Option } from "fp-ts/lib/Option.js";
2930
import {
31+
BuildCreateContractTxResponse,
3032
ContractsRange,
3133
TransactionTextEnvelope,
3234
} from "@marlowe.io/runtime-rest-client/contract";
3335

3436
export function mkContractLifecycle(
3537
wallet: WalletAPI,
36-
rest: FPTSRestAPI
38+
deprecatedRestAPI: FPTSRestAPI,
39+
restClient: RestClient
3740
): ContractsAPI {
38-
const di = { wallet, rest };
41+
const di = { wallet, deprecatedRestAPI, restClient };
3942
return {
4043
createContract: submitCreateTx(di),
4144
applyInputs: submitApplyInputsTx(di),
@@ -45,31 +48,33 @@ export function mkContractLifecycle(
4548
}
4649

4750
const submitCreateTx =
48-
({ wallet, rest }: ContractsDI) =>
51+
({ wallet, restClient }: ContractsDI) =>
4952
(
5053
createContractRequest: CreateContractRequest
5154
): Promise<[ContractId, TxId]> => {
5255
return unsafeTaskEither(
53-
submitCreateTxFpTs(rest)(wallet)(createContractRequest)
56+
submitCreateTxFpTs(restClient)(wallet)(createContractRequest)
5457
);
5558
};
5659

5760
const submitApplyInputsTx =
58-
({ wallet, rest }: ContractsDI) =>
61+
({ wallet, deprecatedRestAPI }: ContractsDI) =>
5962
async (
6063
contractId: ContractId,
6164
applyInputsRequest: ApplyInputsRequest
6265
): Promise<TxId> => {
6366
return unsafeTaskEither(
64-
submitApplyInputsTxFpTs(rest)(wallet)(contractId)(applyInputsRequest)
67+
submitApplyInputsTxFpTs(deprecatedRestAPI)(wallet)(contractId)(
68+
applyInputsRequest
69+
)
6570
);
6671
};
6772

6873
const getApplicableInputs =
69-
({ wallet, rest }: ContractsDI) =>
74+
({ wallet, deprecatedRestAPI }: ContractsDI) =>
7075
async (contractId: ContractId, environement: Environment): Promise<Next> => {
7176
const contractDetails = await unsafeTaskEither(
72-
rest.contracts.contract.get(contractId)
77+
deprecatedRestAPI.contracts.contract.get(contractId)
7378
);
7479
if (isNone(contractDetails.currentContract)) {
7580
return noNext;
@@ -78,13 +83,15 @@ const getApplicableInputs =
7883
contractDetails.roleTokenMintingPolicyId
7984
);
8085
return await unsafeTaskEither(
81-
rest.contracts.contract.next(contractId)(environement)(parties)
86+
deprecatedRestAPI.contracts.contract.next(contractId)(environement)(
87+
parties
88+
)
8289
);
8390
}
8491
};
8592

8693
const getContractIds =
87-
({ rest, wallet }: ContractsDI) =>
94+
({ deprecatedRestAPI, wallet }: ContractsDI) =>
8895
async (): Promise<ContractId[]> => {
8996
const partyAddresses = [
9097
await wallet.getChangeAddress(),
@@ -95,7 +102,9 @@ const getContractIds =
95102
range: Option<ContractsRange>,
96103
acc: ContractId[]
97104
): Promise<ContractId[]> => {
98-
const result = await rest.contracts.getHeadersByRange(range)(kwargs)();
105+
const result = await deprecatedRestAPI.contracts.getHeadersByRange(range)(
106+
kwargs
107+
)();
99108
if (result._tag === "Left") throw result.left;
100109
const response = result.right;
101110
const contractIds = [
@@ -130,7 +139,7 @@ const getParties: (
130139
};
131140

132141
export const submitCreateTxFpTs: (
133-
client: FPTSRestAPI
142+
client: RestClient
134143
) => (
135144
wallet: WalletAPI
136145
) => (
@@ -140,40 +149,48 @@ export const submitCreateTxFpTs: (
140149
pipe(
141150
tryCatchDefault(() => getAddressesAndCollaterals(wallet)),
142151
TE.chain((addressesAndCollaterals) =>
143-
client.contracts.post(
144-
{
145-
contract: createContractRequest.contract,
152+
tryCatchDefault(() =>
153+
client.buildCreateContractTx({
146154
version: "v1",
147-
roles: createContractRequest.roles,
148-
tags: createContractRequest.tags ? createContractRequest.tags : {},
149-
metadata: createContractRequest.metadata
150-
? createContractRequest.metadata
151-
: {},
152-
...(createContractRequest.mininmumLovelaceUTxODeposit && {
153-
mininmumLovelaceUTxODeposit:
154-
createContractRequest.mininmumLovelaceUTxODeposit,
155-
}),
156-
},
157-
addressesAndCollaterals
155+
156+
changeAddress: addressesAndCollaterals.changeAddress,
157+
usedAddresses: addressesAndCollaterals.usedAddresses,
158+
collateralUTxOs: addressesAndCollaterals.collateralUTxOs,
159+
stakeAddress: createContractRequest.stakeAddress,
160+
161+
contractOrSourceId: createContractRequest.contractOrSourceId,
162+
threadRoleName: createContractRequest.threadRoleName,
163+
rolesConfiguration: createContractRequest.rolesConfiguration,
164+
165+
tags: createContractRequest.tags,
166+
metadata: createContractRequest.metadata,
167+
mininmumLovelaceUTxODeposit:
168+
createContractRequest.mininmumLovelaceUTxODeposit,
169+
})
158170
)
159171
),
160-
TE.chainW((contractTextEnvelope) =>
161-
pipe(
162-
tryCatchDefault(() => wallet.signTx(contractTextEnvelope.tx.cborHex)),
163-
TE.chain((hexTransactionWitnessSet) =>
164-
client.contracts.contract.put(
165-
contractTextEnvelope.contractId,
166-
hexTransactionWitnessSet
167-
)
168-
),
169-
TE.map(() => contractTextEnvelope.contractId)
170-
)
172+
TE.chainW(
173+
(buildCreateContractTxResponse: BuildCreateContractTxResponse) =>
174+
pipe(
175+
tryCatchDefault(() =>
176+
wallet.signTx(buildCreateContractTxResponse.tx.cborHex)
177+
),
178+
TE.chain((hexTransactionWitnessSet) =>
179+
tryCatchDefault(() =>
180+
client.submitContract(
181+
buildCreateContractTxResponse.contractId,
182+
transactionWitnessSetTextEnvelope(hexTransactionWitnessSet)
183+
)
184+
)
185+
),
186+
TE.map(() => buildCreateContractTxResponse.contractId)
187+
)
171188
),
172189
TE.map((contractId) => [contractId, contractIdToTxId(contractId)])
173190
);
174191

175192
export const createContractFpTs: (
176-
client: FPTSRestAPI
193+
client: RestClient
177194
) => (
178195
wallet: WalletAPI
179196
) => (

‎packages/runtime/lifecycle/src/generic/payouts.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
withdrawalIdToTxId,
1818
} from "@marlowe.io/runtime-core";
1919

20-
import { FPTSRestAPI } from "@marlowe.io/runtime-rest-client";
20+
import { FPTSRestAPI, RestClient } from "@marlowe.io/runtime-rest-client";
2121

2222
import * as RestPayout from "@marlowe.io/runtime-rest-client/payout";
2323

@@ -26,9 +26,10 @@ import { stringify } from "json-bigint";
2626

2727
export function mkPayoutLifecycle(
2828
wallet: WalletAPI,
29-
rest: FPTSRestAPI
29+
deprecatedRestAPI: FPTSRestAPI,
30+
restClient: RestClient
3031
): PayoutsAPI {
31-
const di = { wallet, rest };
32+
const di = { wallet, deprecatedRestAPI, restClient };
3233
return {
3334
available: fetchAvailablePayouts(di),
3435
withdraw: withdrawPayouts(di),
@@ -37,22 +38,28 @@ export function mkPayoutLifecycle(
3738
}
3839

3940
const fetchAvailablePayouts =
40-
({ wallet, rest }: PayoutsDI) =>
41+
({ wallet, deprecatedRestAPI }: PayoutsDI) =>
4142
(filters?: Filters): Promise<PayoutAvailable[]> => {
4243
return unsafeTaskEither(
43-
fetchAvailablePayoutsFpTs(rest)(wallet)(O.fromNullable(filters))
44+
fetchAvailablePayoutsFpTs(deprecatedRestAPI)(wallet)(
45+
O.fromNullable(filters)
46+
)
4447
);
4548
};
4649
const withdrawPayouts =
47-
({ wallet, rest }: PayoutsDI) =>
50+
({ wallet, deprecatedRestAPI }: PayoutsDI) =>
4851
(payoutIds: PayoutId[]): Promise<void> => {
49-
return unsafeTaskEither(withdrawPayoutsFpTs(rest)(wallet)(payoutIds));
52+
return unsafeTaskEither(
53+
withdrawPayoutsFpTs(deprecatedRestAPI)(wallet)(payoutIds)
54+
);
5055
};
5156
const fetchWithdrawnPayouts =
52-
({ wallet, rest }: PayoutsDI) =>
57+
({ wallet, deprecatedRestAPI }: PayoutsDI) =>
5358
(filters?: Filters): Promise<PayoutWithdrawn[]> => {
5459
return unsafeTaskEither(
55-
fetchWithdrawnPayoutsFpTs(rest)(wallet)(O.fromNullable(filters))
60+
fetchWithdrawnPayoutsFpTs(deprecatedRestAPI)(wallet)(
61+
O.fromNullable(filters)
62+
)
5663
);
5764
};
5865

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { RuntimeLifecycle } from "../api.js";
22
import { WalletAPI } from "@marlowe.io/wallet/api";
33

4-
import { FPTSRestAPI } from "@marlowe.io/runtime-rest-client";
4+
import { FPTSRestAPI, RestClient } from "@marlowe.io/runtime-rest-client";
55

66
import { mkPayoutLifecycle } from "./payouts.js";
77
import { mkContractLifecycle } from "./contracts.js";
88

99
export function mkRuntimeLifecycle(
10-
restAPI: FPTSRestAPI,
10+
deprecatedRestAPI: FPTSRestAPI,
11+
restClient: RestClient,
1112
wallet: WalletAPI
1213
): RuntimeLifecycle {
1314
return {
1415
wallet: wallet,
15-
contracts: mkContractLifecycle(wallet, restAPI),
16-
payouts: mkPayoutLifecycle(wallet, restAPI),
16+
contracts: mkContractLifecycle(wallet, deprecatedRestAPI, restClient),
17+
payouts: mkPayoutLifecycle(wallet, deprecatedRestAPI, restClient),
1718
};
1819
}

‎packages/runtime/lifecycle/src/nodejs/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { mkFPTSRestClient } from "@marlowe.io/runtime-rest-client";
1+
import {
2+
mkFPTSRestClient,
3+
mkRestClient,
4+
} from "@marlowe.io/runtime-rest-client";
25
import * as S from "@marlowe.io/wallet/nodejs";
36
import * as Generic from "../generic/runtime.js";
47

@@ -16,6 +19,7 @@ export async function mkRuntimeLifecycle({
1619
privateKeyBech32
1720
);
1821

19-
const restClient = mkFPTSRestClient(runtimeURL);
20-
return Generic.mkRuntimeLifecycle(restClient, wallet);
22+
const deprecatedRestAPI = mkFPTSRestClient(runtimeURL);
23+
const restClient = mkRestClient(runtimeURL);
24+
return Generic.mkRuntimeLifecycle(deprecatedRestAPI, restClient, wallet);
2125
}

‎packages/runtime/lifecycle/test/examples/swap.ada.token.e2e.spec.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@ import {
1313
} from "../context.js";
1414
import { provisionAnAdaAndTokenProvider } from "../provisionning.js";
1515
import console from "console";
16-
import { runtimeTokenToMarloweTokenValue } from "@marlowe.io/runtime-core";
16+
import {
17+
runtimeTokenToMarloweTokenValue,
18+
unAddressBech32,
19+
} from "@marlowe.io/runtime-core";
1720
import { onlyByContractIds } from "@marlowe.io/runtime-lifecycle/api";
1821
import { MINUTES } from "@marlowe.io/adapter/time";
22+
import { mintRole } from "@marlowe.io/runtime-rest-client/contract";
23+
import {
24+
AddressBech32,
25+
AddressBech32Guard,
26+
} from "@marlowe.io/runtime-rest-client/contract/rolesConfigurations.js";
27+
import { unsafeEither } from "@marlowe.io/adapter/fp-ts";
1928

2029
global.console = console;
2130

@@ -57,10 +66,14 @@ describe("swap", () => {
5766
const [contractId, txIdContractCreated] = await runtime(
5867
adaProvider
5968
).contracts.createContract({
60-
contract: swapContract,
61-
roles: {
62-
[swapRequest.provider.roleName]: adaProvider.address,
63-
[swapRequest.swapper.roleName]: tokenProvider.address,
69+
contractOrSourceId: swapContract,
70+
rolesConfiguration: {
71+
[swapRequest.provider.roleName]: mintRole(
72+
adaProvider.address as unknown as AddressBech32
73+
),
74+
[swapRequest.swapper.roleName]: mintRole(
75+
tokenProvider.address as unknown as AddressBech32
76+
),
6477
},
6578
});
6679
await runtime(adaProvider).wallet.waitConfirmation(txIdContractCreated);

‎packages/runtime/lifecycle/test/generic/contracts.e2e.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe("Runtime Contract Lifecycle ", () => {
3333
getBankPrivateKey()
3434
);
3535
const [contractId, txIdContractCreated] =
36-
await runtime.contracts.createContract({ contract: close });
36+
await runtime.contracts.createContract({ contractOrSourceId: close });
3737
await runtime.wallet.waitConfirmation(txIdContractCreated);
3838
console.log("contractID created", contractId);
3939
},
@@ -51,7 +51,7 @@ describe("Runtime Contract Lifecycle ", () => {
5151
const notifyTimeout = pipe(addDays(Date.now(), 1), datetoTimeout);
5252
const [contractId, txIdContractCreated] =
5353
await runtime.contracts.createContract({
54-
contract: oneNotifyTrue(notifyTimeout),
54+
contractOrSourceId: oneNotifyTrue(notifyTimeout),
5555
});
5656
await runtime.wallet.waitConfirmation(txIdContractCreated);
5757

‎packages/runtime/lifecycle/test/generic/payouts.e2e.spec.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import console from "console";
1616
import { runtimeTokenToMarloweTokenValue } from "@marlowe.io/runtime-core";
1717
import { onlyByContractIds } from "@marlowe.io/runtime-lifecycle/api";
1818
import { MINUTES } from "@marlowe.io/adapter/time";
19+
import { mintRole } from "@marlowe.io/runtime-rest-client/contract";
20+
import { AddressBech32 } from "@marlowe.io/runtime-rest-client/contract/rolesConfigurations.js";
1921

2022
global.console = console;
2123

@@ -50,10 +52,14 @@ describe("Payouts", () => {
5052
const [contractId, txCreatedContract] = await runtime(
5153
adaProvider
5254
).contracts.createContract({
53-
contract: swapContract,
54-
roles: {
55-
[swapRequest.provider.roleName]: adaProvider.address,
56-
[swapRequest.swapper.roleName]: tokenProvider.address,
55+
contractOrSourceId: swapContract,
56+
rolesConfiguration: {
57+
[swapRequest.provider.roleName]: mintRole(
58+
adaProvider.address as unknown as AddressBech32
59+
),
60+
[swapRequest.swapper.roleName]: mintRole(
61+
tokenProvider.address as unknown as AddressBech32
62+
),
5763
},
5864
});
5965
await runtime(adaProvider).wallet.waitConfirmation(txCreatedContract);

0 commit comments

Comments
 (0)
Please sign in to comment.