-
Hi Team! I am using your ts sdk to create a dApp and I would like to know if there is a way to determine if a Marlowe contract X "belongs to me" or not, that is, if it complies with a certain contract structure or not. I am aware that there are guards that are useful to check whether if the X contract starts with a certain contract constructor, but that way would be tedious for what I need. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Trying to see if a Marlowe contract is "an instance of your contract" is a common problem that doesn't have a general solution yet. I see three ways of tackling this issue: 1. Recreate the contract and do a deep equality checkThe "easiest" way to see if a contract is an exact instance of a contract that you could create is to share the contract creation parameters/schema between the participants, use those to create an instance of the contract and then do a deep equality check. In other words, assuming that you have: type ContractSchema {
partyA: Address;
partyB: Address;
amount: bigint;
deadline: Timeout;
}
mkMyContract(schema: ContractSchema): Contract {
// ...
} When you create an instance of the contract in the blockchain, you can add the Then you can do: import { deepEqual } from "@marlowe.io/adapter/deep-equal";
assert(deepEqual(contractFromChain, mkMyContract(sharedSchema))) In this approach, the key part is that instead of extracting, you share the details and make sure the contract matches. 2. Use the @marlowe.io/language-core-v1/guards to manually check if a contract has the expected shapeYou can check the survey workshop dapp (web, code) to see how I use the current guards to manually ask questions about the shape of the contract and try to provide a useful hint when it doesn't match. At the same time I query the shape I extract meaningful data. This approach can be error-prone and difficult to achieve for complex contract. 3. I started an experiment package @marlowe.io/generic-guards to make the shape-matching more intuitiveI started an experiment branch of something I was thinking for some time. I call these "generic-guards", @nhenin suggested to call them "shallow-guards". And they are different to the second option as you define how to match using the actual shape of the contract, at least the parts you are interested. Take a look at this example to see how this package could be used to match an expected partial shape. Bare in mind that this is the start of an experiment, so I would have to check if this has a good DX for bigger patterns. |
Beta Was this translation helpful? Give feedback.
Trying to see if a Marlowe contract is "an instance of your contract" is a common problem that doesn't have a general solution yet.
I see three ways of tackling this issue:
1. Recreate the contract and do a deep equality check
The "easiest" way to see if a contract is an exact instance of a contract that you could create is to share the contract creation parameters/schema between the participants, use those to create an instance of the contract and then do a deep equality check.
In other words, assuming that you have:
When you cr…