Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/input-output-hk/plutus in…
Browse files Browse the repository at this point in the history
…to effectfully/builtins/remove-static-built-in-functions
  • Loading branch information
effectfully committed Oct 22, 2020
2 parents 1de2d23 + a2dae31 commit 0762ba7
Show file tree
Hide file tree
Showing 40 changed files with 1,047 additions and 176 deletions.
2 changes: 1 addition & 1 deletion marlowe-playground-client/README.md
Expand Up @@ -27,4 +27,4 @@ Then run `yarn run webpack:server` for an auto-reloading dev build on http://loc
* purescript dependencies are managed with psc-package so add them to [psc-package.json](./psc-package.json)
* purescript uses package sets managed by spago so if the package set doesn't contain a dependency you can add it to [packages.dhall](./packages.dhall)

Whenever you change any of these files you should run `$(nix-build -A dev.scripts.updateClientDeps ../default.nix)` to make sure they are available to things that build purescript (such as webpack). Additionally running this script will make changes to various files that will need to be committed for CI to work.
Whenever you change any of these files you should run `$(nix-build -A dev.scripts.updateClientDeps ../default.nix)/bin/update-client-deps` to make sure they are available to things that build purescript (such as webpack). Additionally running this script will make changes to various files that will need to be committed for CI to work.
2 changes: 1 addition & 1 deletion marlowe-playground-client/package.json
Expand Up @@ -23,7 +23,7 @@
"moo": "^0.5.1",
"nearley": "^2.19.1",
"node-blockly": "^1.0.36",
"notevil": "^1.3.3",
"safe-eval": "^0.4.0",
"popper.js": "^1.14.4",
"xhr2": "^0.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion marlowe-playground-client/spago-packages.nix
Expand Up @@ -1221,7 +1221,7 @@ let
name = "unsafe-reference";
version = "v3.0.1";
src = pkgs.fetchgit {
url = "https://github.com/purescript-contrib/purescript-unsafe-reference";
url = "https://github.com/purescript-contrib/purescript-unsafe-reference.git";
rev = "79d7de7b9351346a73e6c060d80532c95ba1c7c1";
sha256 = "0q758dz59qz0li4s3w1qcg921xp5i5rh6i1l611iv7rr8cbj11al";
};
Expand Down
274 changes: 208 additions & 66 deletions marlowe-playground-client/src/Examples/JS/Contracts.purs
Expand Up @@ -8,7 +8,7 @@ const bob : Party = Role("bob");
const carol : Party = Role("carol");
/* Value under escrow */
const price : SomeNumber = new bignumber.BigNumber(450);
const price : SomeNumber = 450n;
/* helper function to build Actions */
Expand All @@ -29,9 +29,9 @@ const choiceValueBy = function(party : Party) : Value {
/* Names for choices */
const pay : [Bound] = [Bound(0, 0)];
const refund : [Bound] = [Bound(1, 1)];
const both : [Bound] = [Bound(0, 1)];
const pay : [Bound] = [Bound(0n, 0n)];
const refund : [Bound] = [Bound(1n, 1n)];
const both : [Bound] = [Bound(0n, 1n)];
/* Name choices according to person making choice and choice made */
Expand All @@ -57,11 +57,11 @@ const bobChosen : Value = choiceValueBy(bob);
const arbitrate : Contract = When([Case(carolRefund, Close),
Case(carolPay, Pay(alice, Party(bob), ada, price, Close))],
100, Close);
100n, Close);
/* The contract to follow when Alice and Bob have made the same choice. */
const agreement : Contract = If(ValueEQ(aliceChosen, 0),
const agreement : Contract = If(ValueEQ(aliceChosen, 0n),
Pay(alice, Party(bob), ada, price, Close),
Close);
Expand All @@ -72,8 +72,8 @@ const inner : Contract = When([Case(aliceChoice,
If(ValueEQ(aliceChosen, bobChosen),
agreement,
arbitrate))],
60, arbitrate))],
40, Close);
60n, arbitrate))],
40n, Close);
/* What does the vanilla contract look like?
- if Alice and Bob choose
Expand All @@ -83,30 +83,26 @@ const inner : Contract = When([Case(aliceChoice,
- refund if no choices are made. */
const contract : Contract = When([Case(Deposit(alice, alice, ada, price), inner)],
10,
10n,
Close)
contract
"""

zeroCouponBond :: String
zeroCouponBond =
"""const investor : Party = Role("investor");
const issuer : Party = Role("issuer");
When([Case(
Deposit(investor, investor, ada, 850),
Pay(investor, Party(issuer), ada, 850,
When([ Case(Deposit(investor, issuer, ada, 1000),
Pay(investor, Party(investor), ada, 1000, Close))
],
20,
Close)
))],
10,
Close);
const contract : Contract = When([Case(
Deposit(investor, investor, ada, 850n),
Pay(investor, Party(issuer), ada, 850n,
When([ Case(Deposit(investor, issuer, ada, 1000n),
Pay(investor, Party(investor), ada, 1000n, Close))
],
20n,
Close)
))],
10n,
Close);
"""

couponBondGuaranteed :: String
Expand All @@ -115,47 +111,47 @@ couponBondGuaranteed =
const guarantor : Party = Role("guarantor");
const investor : Party = Role("investor");
When([
Case(Deposit(investor, guarantor, ada, 1030),
When([
Case(Deposit(investor, investor, ada, 1000),
Pay(investor, Party(issuer), ada, 1000,
When([
Case(Deposit(investor, issuer, ada, 10),
Pay(investor, Party(investor), ada, 10,
Pay(investor, Party(guarantor), ada, 10,
When([
Case(Deposit(investor, issuer, ada, 10),
Pay(investor, Party(investor), ada, 10,
Pay(investor, Party(guarantor), ada, 10,
When([
Case(Deposit(investor, issuer, ada, 1010),
Pay(investor, Party(investor), ada, 1010,
Pay(investor, Party(guarantor), ada, 1010, Close)
)
)
], 20, Close)
)
)
)
], 15, Close)
)
)
)
], 10, Close)
)
)
], 5, Close)
)
], 5, Close)
const contract : Contract = When([
Case(Deposit(investor, guarantor, ada, 1030n),
When([
Case(Deposit(investor, investor, ada, 1000n),
Pay(investor, Party(issuer), ada, 1000n,
When([
Case(Deposit(investor, issuer, ada, 10n),
Pay(investor, Party(investor), ada, 10n,
Pay(investor, Party(guarantor), ada, 10n,
When([
Case(Deposit(investor, issuer, ada, 10n),
Pay(investor, Party(investor), ada, 10n,
Pay(investor, Party(guarantor), ada, 10n,
When([
Case(Deposit(investor, issuer, ada, 1010n),
Pay(investor, Party(investor), ada, 1010n,
Pay(investor, Party(guarantor), ada, 1010n, Close)
)
)
], 20n, Close)
)
)
)
], 15n, Close)
)
)
)
], 10n, Close)
)
)
], 5n, Close)
)
], 5n, Close)
"""

swap :: String
swap =
"""const lovelacePerAda : SomeNumber = new bignumber.BigNumber("1000000");
const amountOfAda : SomeNumber = new bignumber.BigNumber("1000");
const amountOfLovelace : SomeNumber = lovelacePerAda.times(amountOfAda);
const amountOfDollars : SomeNumber = new bignumber.BigNumber("100");
"""const lovelacePerAda : SomeNumber = 1000000n;
const amountOfAda : SomeNumber = 1000n;
const amountOfLovelace : SomeNumber = lovelacePerAda * amountOfAda;
const amountOfDollars : SomeNumber = 100n;
const dollars : Token = Token("85bb65", "dollar")
Expand Down Expand Up @@ -193,9 +189,155 @@ const makePayment = function(src : SwapParty, dest : SwapParty,
continuation);
}
makeDeposit(alice, 10,
makeDeposit(bob, 20,
makePayment(alice, bob,
makePayment(bob, alice,
Close))))
const contract : Contract = makeDeposit(alice, 10n,
makeDeposit(bob, 20n,
makePayment(alice, bob,
makePayment(bob, alice,
Close))))
"""

cfd :: String
cfd =
"""
const party : Party = Role("party")
const counterParty = Role("counterparty")
const oracle: Party = Role("oracle")
function waitForEvent (action : Action, timeout : SomeNumber, alternative : Contract): (_: Contract) => Contract {
return cont => When([Case(action, cont)], timeout, alternative)
}
function before(time: SomeNumber): SomeNumber {
return time
}
function orElse(contract: Contract): Contract {
return contract
}
function receiveValue(val: string): Action {
return Choice(ChoiceId(val, oracle), [Bound(0, 100)])
}
function readValue(val : string): Value {
return ChoiceValue(ChoiceId(val, oracle))
}
function waitFor(delay: SomeNumber): (_: Contract) => Contract {
return cont => When([], delay, cont)
}
function checkIf(obs: Observation, c1: Contract, c2: Contract): Contract {
return If(obs, c1, c2)
}
function thenDo(c: Contract): Contract {
return c
}
function elseDo(c: Contract): Contract {
return c
}
function letValue(name: string, v: Value): (_: Contract) => Contract {
return cont => Let (ValueId(name), v, cont)
}
function useValue(name: string): Value {
return UseValue(name)
}
function value(v: SomeNumber): Value {
return Constant(v)
}
function fromParty(a: AccountId): AccountId {
return a
}
function toParty(p: Party): Payee {
return Party(p)
}
function withToken(tkn: Token): Token {
return tkn
}
const end: Contract = Close
function amountOf(amount: Value): Value {
return amount
}
function minus(v1: Value, v2: Value): Value {
return SubValue(v1, v2)
}
function sendPayment(fromParty: Party, toCounterParty: Payee, tkn: Token, payoff: Value): (_: Contract) => Contract {
return cont => Pay(fromParty, toCounterParty, tkn, payoff, cont)
}
function Do(actions: Array<(_: Contract) => Contract>, last: Contract): Contract {
const foldRight = <A, B>(xs: Array<A>, zero: B) => (f: (b: B, a: A) => B): B => {
const len = xs.length;
if (len === 0) return zero;
else {
const last = xs[len - 1];
const inits = xs.slice(0, len - 1);
return foldRight(inits, f(zero, last))(f);
}
}
return foldRight(actions, last)((b, a) => a(b))
}
const contract = (() => {
const partyCollateralToken = Token("", "")
const partyCollateralAmount = value(1000n)
const counterPartyCollateralToken = Token("", "")
const counterPartyCollateralAmount = value(1000n)
const endDate = 1000n
const partyCollateralDeposit = Deposit(party, party, partyCollateralToken, partyCollateralAmount)
const counterPartyCollateralDeposit = Deposit(counterParty, counterParty, counterPartyCollateralToken, counterPartyCollateralAmount)
const minValue = (val1: Value, val2: Value) => Cond(ValueGE(val1, val2), val2, val1)
return Do([
waitForEvent(partyCollateralDeposit, before(100n), orElse(end)),
waitForEvent(counterPartyCollateralDeposit, before(100n), orElse(end)),
waitForEvent(receiveValue("price1"), before(100n), orElse(end)),
waitFor(endDate),
waitForEvent(receiveValue("price2"), before(endDate + 100n), orElse(end)),
letValue("delta", minus(readValue("price1"), readValue("price2")))
], checkIf(ValueEQ(useValue("delta"), value(0)),
thenDo(end),
elseDo(
checkIf(ValueLT(useValue("delta"), value(0)),
thenDo(
Do([
letValue("absdelta", minus(value(0), useValue("delta"))),
(() => {
const payoff = minValue(useValue("absdelta"), partyCollateralAmount)
return sendPayment(fromParty(party), toParty(counterParty), withToken(partyCollateralToken), amountOf(payoff))
})()
], end)
),
elseDo(
Do([
(() => {
const payoff = minValue(useValue("delta"), partyCollateralAmount)
return sendPayment(fromParty(party), toParty(counterParty), withToken(partyCollateralToken), amountOf(payoff))
})()
], end)
)
)
)
)
)
})()
"""
9 changes: 9 additions & 0 deletions marlowe-playground-client/src/Halogen/Monaco.purs
Expand Up @@ -68,6 +68,7 @@ data Query a
= SetText String a
| GetText (String -> a)
| GetModel (Monaco.ITextModel -> a)
| GetModelMarkers (Array IMarker -> a)
| SetPosition IPosition a
| Resize a
| SetTheme String a
Expand Down Expand Up @@ -192,6 +193,14 @@ handleQuery (GetModel f) = do
m <- liftEffect $ Monaco.getModel editor
pure $ f m

handleQuery (GetModelMarkers f) = do
withEditor \editor ->
liftEffect do
monaco <- Monaco.getMonaco
model <- Monaco.getModel editor
markers <- Monaco.getModelMarkers monaco model
pure $ f markers

handleQuery (SetPosition position next) = do
withEditor \editor -> do
liftEffect $ Monaco.setPosition editor position
Expand Down

0 comments on commit 0762ba7

Please sign in to comment.