Skip to content

Commit

Permalink
Start guapswap spectrum processor.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldgaetano committed Nov 6, 2023
1 parent 3a5aa08 commit d2d8ec1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package org.guapswap.commons.dex.spectrum.spectrum_builders.contract_builders

import org.ergoplatform.appkit.scalaapi.ErgoValueBuilder
import org.ergoplatform.appkit.{Address, BlockchainContext, ConstantsBuilder, ErgoContract, ErgoType, ErgoValue, JavaHelpers}
import org.ergoplatform.appkit.{Address, BlockchainContext, ConstantsBuilder, ErgoContract, ErgoValue, JavaHelpers}
import org.guapswap.commons.dex.spectrum.{SpectrumDex, SpectrumErgoPool}
import org.guapswap.commons.ergo.ErgoBlockchainAssets
import sigmastate.{SType, Values}
import sigmastate.Values.ErgoTree
import sigmastate.basics.DLogProtocol.ProveDlog
import special.collection.Coll
import special.sigma.SigmaProp

case class SpectrumERG2TokenContractBuilder(
exFeePerTokenDenom: Long,
delta: Long,
baseAmount: Long,
feeNum: Int,
refundProp: ErgoValue[ProveDlog],
refundProp: ErgoValue[SigmaProp],
spectrumIsQuote: Boolean,
maxExFee: Long,
poolNFT: ErgoValue[Coll[java.lang.Byte]],
Expand Down Expand Up @@ -76,23 +76,26 @@ object SpectrumERG2TokenContractBuilder {
*/
def apply(apiUrl: String,
swapAssetTicker: String,
baseAmount: Long,
baseAmountIncludingFees: Long,
slippageTolerancePercentage: Double,
nitro: Double,
spectrumMinerFee: Long,
redeemerAddress: String,
refundAddress: String
)(implicit ctx: BlockchainContext): SpectrumERG2TokenContractBuilder = {
): SpectrumERG2TokenContractBuilder = {

val redeemerPropBytes: ErgoValue[Coll[java.lang.Byte]] = ErgoValue.of(Address.create(redeemerAddress).toPropositionBytes)
val refundPropBytes: ErgoValue[ProveDlog] = ErgoValue.of(ProveDlog(Address.create(refundAddress).getPublicKey.value))
val refundPropBytes: ErgoValue[SigmaProp] = ErgoValue.of(ProveDlog(Address.create(refundAddress).getPublicKey.value))

val swapBuyContract: ErgoTree = JavaHelpers.decodeStringToErgoTree(n2t_v3_swapsell_ergotree)
val swapBuyConstants: IndexedSeq[Values.Constant[SType]] = swapBuyContract.constants

val assetPool: SpectrumErgoPool = SpectrumDex.getPoolFromAssetTicker(swapAssetTicker).asInstanceOf[SpectrumErgoPool]
val assetAmounts: (Long, Long) = assetPool.getPoolInfo(apiUrl)
val minQuoteAmount: Long = AbstractSpectrumSwapContractBuilder.calcMinOutputAmount(baseAmount, slippageTolerancePercentage, assetAmounts._1, assetAmounts._2, assetPool.feeNum, SpectrumDex.SPECTRUM_ERGO_POOL_FEE_DENOM)
val minExecutionFee: Long = AbstractSpectrumSwapContractBuilder.calcMinExecutionFee(spectrumMinerFee)
val maxExecutionFee: Long = AbstractSpectrumSwapContractBuilder.calcMaxExecutionFee(minExecutionFee, nitro).toLong
val baseAmount: Long = baseAmountIncludingFees - maxExecutionFee
val minQuoteAmount: Long = AbstractSpectrumSwapContractBuilder.calcMinOutputAmount(baseAmount, slippageTolerancePercentage, assetAmounts._1, assetAmounts._2, assetPool.feeNum, SpectrumDex.SPECTRUM_ERGO_POOL_FEE_DENOM)
val swapExtremums: Option[(Double, (Long, Long, Long, Long))] = AbstractSpectrumSwapContractBuilder.calcSwapExtremums(minExecutionFee, nitro, minQuoteAmount)
//val dexFeePerTokenFraction: (BigInt, BigInt) = GuapSwapUtils.decimalToFraction(BigDecimal(swapExtremums.get._1))
val exFeePerTokenDenom: Long = AbstractSpectrumSwapContractBuilder.isoSConstantToErgoValue.to(swapBuyConstants(1)).getValue.asInstanceOf[Long]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ case class NodeConfig(
nodeApiKey: String,
explorerUrl: String,
mnemonic: String,
mnemonicIndex: String
mnemonicIndex: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package org.guapswap.guapswap.configs.setup_config.settings.mining_portfolio_set
case class GuapSwapSpectrumDexSettingsConfig (
slippageTolerancePercentage: Double,
nitro: Double,
spectrumMinerFeeInNanoERG: Long
spectrumMinerFeeInNanoERG: Long,
redeemerAddress: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,53 @@ package org.guapswap.guapswap.processors

import org.guapswap.guapswap.configs.setup_config.GuapSwapSetupConfig
import org.guapswap.commons.dex.spectrum.spectrum_builders.contract_builders.SpectrumERG2TokenContractBuilder


import org.ergoplatform.appkit.{OutBox, UnsignedTransactionBuilder}
import org.ergoplatform.appkit.{Address, BlockchainContext, Mnemonic, NetworkType, OutBox, OutBoxBuilder, SecretString, UnsignedTransactionBuilder}
import org.guapswap.commons.dex.spectrum.spectrum_builders.box_builders.SpectrumERG2TokenSwapBoxBuilder
import org.scalatest.tagobjects.Network


case class GuapSwapProcessor(
config: GuapSwapSetupConfig,
totalProxyBoxesValue: Long
)(implicit txBuilder: UnsignedTransactionBuilder)extends {
)(implicit ctx: BlockchainContext, txBuilder: UnsignedTransactionBuilder) extends {

def getOutputs(): Array[OutBox] = {
def getOutputs: Array[OutBox] = {

val outputs: Array[OutBox] = config.guapswapSettings.miningPortfolioSettings.guapswaps.map(guapswap => {

val dexChoice: String = guapswap.dexSettings.dexChoice

dexChoice match {

case "SpectrumDex" => {
case "spectrum" => {

val payoutVal: Long = (guapswap.percentageOfPayout * totalProxyBoxesValue).toLong
val userAddress: Address = Address.createEip3Address(config.node.mnemonicIndex, NetworkType.fromValue(config.node.networkType), SecretString.create(config.node.mnemonic), SecretString.empty(), false)

implicit val outBoxBuilder: OutBoxBuilder = txBuilder.outBoxBuilder()

val outBoxBuilder = txBuilder.outBoxBuilder()
val contract = SpectrumERG2TokenContractBuilder(
config.node.nodeApiUrl,
guapswap.tokenTicker,
payoutVal,
guapswap.dexSettings.spectrumDexSettings.slippageTolerancePercentage,
guapswap.dexSettings.spectrumDexSettings.nitro,
guapswap.dexSettings.spectrumDexSettings.spectrumMinerFeeInNanoERG,
guapswap.dexSettings.spectrumDexSettings.redeemerAddress,
userAddress.asP2PK().toString()
).toErgoContract

)
val spectrumSwapBox = SpectrumERG2TokenSwapBoxBuilder(payoutVal, contract).toOutBox
spectrumSwapBox

}

}

})

outputs

}

}

0 comments on commit d2d8ec1

Please sign in to comment.