Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adhere DEX orders to "limit order" behavior (single counter order) #13

Draft
wants to merge 25 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0626d30
start drafting buy order contract;
greenhat Apr 30, 2020
0aa01a1
move replication check (SELF.id) to R6;
greenhat May 3, 2020
b555470
finish buyer contract with single spending sell order;
greenhat May 3, 2020
2343110
draft buy order;
greenhat May 5, 2020
0fc52d1
draft sell order contract;
greenhat May 5, 2020
aebab81
add contract parameters in registers for order boxes;
greenhat May 5, 2020
de3d69d
guarded counter order presence in buyer contract;
greenhat May 6, 2020
9f9835c
comments;
greenhat May 7, 2020
845bda8
get the token id from return box via getOrElse in buyer contract and
greenhat May 7, 2020
cefa7b4
remove R4 in return box on buy order cancellation;
greenhat May 7, 2020
d1d11fd
remove R4 in sell order cancellation box;
greenhat May 7, 2020
f145b10
Merge branch 'dex-limit-order' into dex-limit-order-multiple-counter
greenhat May 7, 2020
ca88e94
calc full spread instead of spread per token;
greenhat May 7, 2020
fd95cc3
draft buyer contract for multiple counter orders;
greenhat May 7, 2020
be35c61
rewrite buy order contract full spread accumulation;
greenhat May 7, 2020
495c5ec
remove unused import;
greenhat May 9, 2020
f9fd75c
implement boxesAreSortedByTokenPrice;
greenhat May 10, 2020
65db8a8
draft fullSpread for seller contract;
greenhat May 10, 2020
43095cb
move TODOs to PR desc;
greenhat May 10, 2020
e9e9505
formatting;
greenhat May 11, 2020
e479862
add missing new(residual) order box requirements(registers)
greenhat May 18, 2020
31b6bac
fix missing checks for residual sell order box;
greenhat May 18, 2020
e42b5f1
for sell order contract require spread only if strictly "younger" than
greenhat May 18, 2020
152ede5
update Scastie link;
greenhat Jun 2, 2020
487cdeb
Merge pull request #14 from ergoplatform/dex-limit-order-multiple-cou…
greenhat Sep 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Enjoy:

### DEX with partial filling contracts

[Run in Scastie](https://scastie.scala-lang.org/YCzvl8NBQwa7R0pVI5mHnA)
[Run in Scastie](https://scastie.scala-lang.org/mh3h6SrESnKJwdqZjKnVkw)

[Source code](https://github.com/ergoplatform/ergo-playgrounds/blob/c91117ae0b1434b7a554028592e30a5bba15a14b/playgrounds/src/main/scala/org/ergoplatform/playgrounds/examples/AssetsAtomicExchangePlayground.scala#L1-L1)
[Source code](https://github.com/ergoplatform/ergo-playgrounds/blob/e42b5f14e9bfc50be27d69db44e40e17a3d7e58f/playgrounds/src/main/scala/org/ergoplatform/playgrounds/examples/DEXPlayground.scala#L3)

### Assets Atomic Exchange contracts

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dynverSeparator in ThisBuild := "-"
lazy val allConfigDependency = "compile->compile;test->test"

lazy val dependencies = Seq(
"org.ergoplatform" %% "ergo-scala-compiler" % "0.0.0-32-aaadbee1-SNAPSHOT",
"org.ergoplatform" %% "ergo-scala-compiler" % "0.0.0-41-13995374-SNAPSHOT",
"org.ergoplatform" %% "ergo-appkit" % "develop-d77acfb8-SNAPSHOT"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import org.ergoplatform.playgroundenv.dsl.{
TypesDsl
}
import sigmastate.Values.{SigmaPropConstant, SigmaPropValue}
import sigmastate.eval.CompiletimeIRContext

object playground extends GeneratorsDsl with TypesDsl with BoxDsl with TransactionDsl {

implicit override protected def IR = new CompiletimeIRContext()

val MinTxFee: Long = 1000 * 1000
val MinErg: Long = 1000 * 1000

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.ergoplatform.playgroundenv.models.TokenAmount
import org.ergoplatform.{ErgoBox, ErgoBoxCandidate}
import scorex.crypto.hash.Digest32
import sigmastate.SType
import sigmastate.SType.AnyOps
import sigmastate.Values.{ByteArrayConstant, EvaluatedValue}
import sigmastate.eval.Extensions._
import sigmastate.eval._
Expand Down Expand Up @@ -35,38 +36,50 @@ trait BoxDsl extends TypesDsl {
)
}

private def liftVal[T](v: T): EvaluatedValue[SType] = v match {
case ba: Array[Byte] => ByteArrayConstant(ba)
private def liftVal(v: Any): EvaluatedValue[SType] = {
val (tV, newV) = v match {
case a: Array[Byte] => (Evaluation.rtypeOf(a.toColl).get, a.toColl)
case _ => (Evaluation.rtypeOf(v).get, v)
}
val elemTpe = Evaluation.rtypeToSType(tV)
IR.builder.mkConstant[SType](newV.asWrappedType, elemTpe)
}

def Box[T](
private def liftRegVals(
regs: Seq[(NonMandatoryRegisterId, Any)]
): Map[NonMandatoryRegisterId, EvaluatedValue[SType]] =
regs.map { t =>
(t._1, liftVal(t._2))
}.toMap

def Box(
value: Long,
register: (NonMandatoryRegisterId, T),
script: ErgoContract
script: ErgoContract,
registers: (NonMandatoryRegisterId, Any)*
): ErgoBoxCandidate = {
require(value > 0, s"box value shoulde be > 0, got $value")
new ErgoBoxCandidate(
value,
script.ergoTree,
0,
Array[(TokenId, Long)]().toColl,
Map((register._1, liftVal(register._2)))
liftRegVals(registers)
)
}

def Box(
value: Long,
token: (TokenInfo, Long),
register: (NonMandatoryRegisterId, Any),
script: ErgoContract
script: ErgoContract,
registers: (NonMandatoryRegisterId, Any)*
): ErgoBoxCandidate = {
require(value > 0, s"box value shoulde be > 0, got $value")
new ErgoBoxCandidate(
value,
script.ergoTree,
0,
Array[(TokenId, Long)]((Digest32 @@ token._1.tokenId.toArray, token._2)).toColl,
Map((register._1, liftVal(register._2)))
liftRegVals(registers)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.ergoplatform.playgroundenv.dsl

import sigmastate.eval.CompiletimeIRContext

trait TypesDsl {

implicit protected def IR: CompiletimeIRContext;

type Coll[A] = special.collection.Coll[A]
type SigmaProp = special.sigma.SigmaProp
type ErgoContract = org.ergoplatform.compiler.ErgoContract
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ object AssetsAtomicExchangePlayground {

val sellerOutBox =
Box(
value = sellerAskNanoErgs,
register = (R4 -> sellOrderTransactionSigned.outputs(0).id),
script = contract(sellerParty.wallet.getAddress.pubKey)
value = sellerAskNanoErgs,
registers = (R4 -> sellOrderTransactionSigned.outputs(0).id),
script = contract(sellerParty.wallet.getAddress.pubKey)
)

val buyerOutBox = Box(
value = buyerSwapBoxValue,
token = (token -> buyerBidTokenAmount),
register = (R4 -> buyOrderTransactionSigned.outputs(0).id),
script = contract(buyerParty.wallet.getAddress.pubKey)
value = buyerSwapBoxValue,
token = (token -> buyerBidTokenAmount),
registers = (R4 -> buyOrderTransactionSigned.outputs(0).id),
script = contract(buyerParty.wallet.getAddress.pubKey)
)

val dexParty = blockchainSim.newParty("DEX")
Expand Down
Loading