Skip to content

exchanger was added #40

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
49 changes: 49 additions & 0 deletions ride4dapps/exchanger/ride/exchanger.ride
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# STDLIB_VERSION 3 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}

let IdTokenA = base58'BaBy2y5tGsuQZPMM7JHU89zmahR7yybXaVMXpnZ589An'
let IdTokenB = unit

@Callable(contextObj)
func exchange() = {
let amountA = getIntegerValue(this, "amountA")
let amountB = getIntegerValue(this, "amountB")
# check and extract info about payment
let payment = match(contextObj.payment) {
case p:AttachedPayment => p
case _ => throw("Payment not attached")
}
let assetIdReceived = payment.assetId
let tokenReceiveAmount = payment.amount
# the amount of sent assets are determined in the next block
if (assetIdReceived == IdTokenB)
then
(
let tokenSendAmount = (fraction(amountB,tokenReceiveAmount,tokenReceiveAmount + amountA))
let assetIdSent = IdTokenA
# successful execution result is updating information about actual balance and supply into the state and transfer tokens to the caller
ScriptResult(
WriteSet([DataEntry("amountB", amountB + tokenReceiveAmount ),DataEntry("amountA", amountA - tokenSendAmount )]),
TransferSet([ScriptTransfer(contextObj.caller, tokenSendAmount, assetIdSent)])
)
)
else ( if (assetIdReceived == IdTokenA)
then
(
let tokenSendAmount = (fraction(amountA,tokenReceiveAmount,tokenReceiveAmount + amountB))
let assetIdSent = unit
# successful execution result is updating information about actual balance and supply into the state and transfer tokens to the caller
ScriptResult(
WriteSet([DataEntry("amountB", amountB - tokenSendAmount ),DataEntry("amountA", amountA + tokenReceiveAmount )]),
TransferSet([ScriptTransfer(contextObj.caller, tokenSendAmount, assetIdSent)])
)
)
else throw("Asset is not allowed"))
}