Skip to content

Commit

Permalink
shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ridev6 committed Mar 22, 2024
1 parent 4d1ad02 commit 752b49f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 38 deletions.
49 changes: 27 additions & 22 deletions ride/futures_account.ride
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ let contractFilename = "futures_account.ride"
let kMultisig = "%s__multisig"
func kStatus(dapp: String, txId: String) = ["%s__status", dapp, txId].makeString(separator)

let kShutdown = "%s__shutdown"

func wrapErr(s: String) = {
contractFilename + ": " + s
}
Expand Down Expand Up @@ -52,7 +54,13 @@ func kRequestPriceAssetId(requestId: String) = {
["%s%s", requestId, "priceAssetId"].makeString(separator)
}

let factoryAddress = addressFromPublicKey(this.getBinaryValue(kFactoryPublicKey))
let factoryAddress = addressFromPublicKey(
this.getBinary(kFactoryPublicKey).valueOrErrorMessage(
throwErr("factory public key is not defined")
)
)

let shutdown = factoryAddress.getBoolean(kShutdown).valueOrElse(false)

let kCalculatorAddress = "%s__calculatorAddress"
let calculatorAddressOption = match factoryAddress.getString(kCalculatorAddress) {
Expand Down Expand Up @@ -88,39 +96,39 @@ func mustOwner(i: Invocation) = {

@Callable(i)
func stringEntry(key: String, val: String) =
if (i.mustCalculator()) then ([StringEntry(key, val)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([StringEntry(key, val)], key) else ([], unit)

@Callable(i)
func integerEntry(key: String, val: Int) =
if (i.mustCalculator()) then ([IntegerEntry(key, val)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([IntegerEntry(key, val)], key) else ([], unit)

@Callable(i)
func booleanEntry(key: String, val: Boolean) =
if (i.mustCalculator()) then ([BooleanEntry(key, val)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([BooleanEntry(key, val)], key) else ([], unit)

@Callable(i)
func binaryEntry(key: String, val: ByteVector) =
if (i.mustCalculator()) then ([BinaryEntry(key, val)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([BinaryEntry(key, val)], key) else ([], unit)

@Callable(i)
func deleteEntry(key: String) =
if (i.mustCalculator()) then ([DeleteEntry(key)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([DeleteEntry(key)], key) else ([], unit)

@Callable(i)
func reissue(assetId: ByteVector, amount: Int, reissuable: Boolean) =
if (i.mustCalculator()) then ([Reissue(assetId, amount, reissuable)], amount) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([Reissue(assetId, amount, reissuable)], amount) else ([], unit)

@Callable(i)
func burn(assetId: ByteVector, amount: Int) =
if (i.mustCalculator()) then ([Burn(assetId, amount)], amount) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([Burn(assetId, amount)], amount) else ([], unit)

@Callable(i)
func transferAsset(recipientBytes: ByteVector, amount: Int, assetId: ByteVector) =
if (i.mustCalculator()) then ([ScriptTransfer(Address(recipientBytes), amount, assetId)], amount) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([ScriptTransfer(Address(recipientBytes), amount, assetId)], amount) else ([], unit)

@Callable(i)
func transferWaves(recipientBytes: ByteVector, amount: Int) =
if (i.mustCalculator()) then ([ScriptTransfer(Address(recipientBytes), amount, unit)], amount) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([ScriptTransfer(Address(recipientBytes), amount, unit)], amount) else ([], unit)

@Callable(i)
func init(factoryPublicKey: ByteVector, creatorPublicKey: ByteVector) = {
Expand All @@ -142,6 +150,7 @@ func verify() = {
match tx {
case o: Order => {
strict checks = [
!shutdown || throwErr("not allowed"),
o.matcherPublicKey == matcherPublicKey || throwErr(
"matcher public key must be " + matcherPublicKey.toBase58String()
),
Expand All @@ -158,19 +167,15 @@ func verify() = {

true
}
case _ => if (chainId == chainIdW) then {
if (factoryPublicKeyOption.isDefined()) then {
match factoryAddress.getString(kMultisig) {
case multisig: String => {
let statusKey = kStatus(this.toString(), tx.id.toBase58String())
let status = multisig.addressFromStringValue().getBoolean(statusKey).valueOrElse(false)

status
}
case _ => false
case _ => if (factoryPublicKeyOption.isDefined()) then {
match factoryAddress.getString(kMultisig) {
case multisig: String => {
let statusKey = kStatus(this.toString(), tx.id.toBase58String())
let status = multisig.addressFromStringValue().getBoolean(statusKey).valueOrElse(false)

status
}
} else {
sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
case _ => false
}
} else {
sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
Expand Down
30 changes: 24 additions & 6 deletions ride/futures_calculator.ride
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ let contractFilename = "futures_calculator.ride"
let kMultisig = "%s__multisig"
func kStatus(dapp: String, txId: String) = ["%s__status", dapp, txId].makeString(separator)

let kShutdown = "%s__shutdown"

func validateAddress(address: String) = {
addressFromString(address).isDefined()
}


func wrapErr(s: String) = {
contractFilename + ": " + s
}
Expand All @@ -30,6 +31,8 @@ let factoryAddressOption = match this.getString(kFactoryAddress) {
}
let factoryAddress = factoryAddressOption.valueOrErrorMessage(wrapErr("invalid factory address"))

let shutdown = factoryAddress.getBoolean(kShutdown).valueOrElse(false)

func mustAddress(i: Invocation, address: Address) = {
i.caller == address || throwErr("permission denied")
}
Expand Down Expand Up @@ -136,7 +139,10 @@ func init(
# args: [amountAssetId, priceAssetId]
@Callable(i)
func requestAccount(callerPublicKey: ByteVector, args: List[String]) = {
strict checkCaller = i.mustFactory()
strict ckecks = [
!shutdown || throwErr("not allowed"),
i.mustFactory()
]
let amountAssetIdStr = args[0]
let priceAssetIdStr = args[1]
let userAddress = addressFromPublicKey(callerPublicKey)
Expand Down Expand Up @@ -198,7 +204,10 @@ func requestAccount(callerPublicKey: ByteVector, args: List[String]) = {
# args: [creatorPublicKey]
@Callable(i)
func addAccount(callerPublicKey: ByteVector, args: List[String]) = {
strict checkCaller = i.mustFactory()
strict ckecks = [
!shutdown || throwErr("not allowed"),
i.mustFactory()
]
let creatorPublicKey = args[0].fromBase58String()
let accountPublicKey = callerPublicKey
let accountAddress = addressFromPublicKey(callerPublicKey)
Expand Down Expand Up @@ -244,7 +253,10 @@ func addAccount(callerPublicKey: ByteVector, args: List[String]) = {
# args: [accountAddress]
@Callable(i)
func deposit(callerPublicKey: ByteVector, args: List[String]) = {
strict checkCaller = i.mustFactory()
strict ckecks = [
!shutdown || throwErr("not allowed"),
i.mustFactory()
]
let accountAddress = addressFromString(args[0]).valueOrErrorMessage(
wrapErr("invalid account address")
)
Expand All @@ -265,7 +277,10 @@ func deposit(callerPublicKey: ByteVector, args: List[String]) = {
# args: [accountAddress, amount, assetId]
@Callable(i)
func withdraw(callerPublicKey: ByteVector, args: List[String]) = {
strict checkCaller = i.mustFactory()
strict ckecks = [
!shutdown || throwErr("not allowed"),
i.mustFactory()
]
let userAddress = addressFromPublicKey(callerPublicKey)
let accountAddress = addressFromString(args[0]).valueOrErrorMessage(
wrapErr("invalid account address")
Expand All @@ -286,7 +301,10 @@ func withdraw(callerPublicKey: ByteVector, args: List[String]) = {
# args: [amountAssetId, priceAssetId, allow]
@Callable(i)
func pairAllowance(callerPublicKey: ByteVector, args: List[String]) = {
strict checkCaller = i.mustFactory()
strict ckecks = [
!shutdown || throwErr("not allowed"),
i.mustFactory()
]
let amountAssetIdStr = args[0]
let priceAssetIdStr = args[1]
let allowStr = args[2]
Expand Down
56 changes: 46 additions & 10 deletions ride/futures_factory.ride
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ let contractFilename = "futures_factory.ride"
let kMultisig = "%s__multisig"
func kStatus(dapp: String, txId: String) = ["%s__status", dapp, txId].makeString(separator)

let kShutdown = "%s__shutdown"
let kPublicKeys = "%s__publicKeys"

let shutdown = getBoolean(kShutdown).valueOrElse(false)

func validateAddress(address: String) = {
addressFromString(address).isDefined()
}
Expand Down Expand Up @@ -75,39 +80,39 @@ func init(

@Callable(i)
func stringEntry(key: String, val: String) =
if (i.mustCalculator()) then ([StringEntry(key, val)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([StringEntry(key, val)], key) else ([], unit)

@Callable(i)
func integerEntry(key: String, val: Int) =
if (i.mustCalculator()) then ([IntegerEntry(key, val)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([IntegerEntry(key, val)], key) else ([], unit)

@Callable(i)
func booleanEntry(key: String, val: Boolean) =
if (i.mustCalculator()) then ([BooleanEntry(key, val)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([BooleanEntry(key, val)], key) else ([], unit)

@Callable(i)
func binaryEntry(key: String, val: ByteVector) =
if (i.mustCalculator()) then ([BinaryEntry(key, val)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([BinaryEntry(key, val)], key) else ([], unit)

@Callable(i)
func deleteEntry(key: String) =
if (i.mustCalculator()) then ([DeleteEntry(key)], key) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([DeleteEntry(key)], key) else ([], unit)

@Callable(i)
func reissue(assetId: ByteVector, amount: Int, reissuable: Boolean) =
if (i.mustCalculator()) then ([Reissue(assetId, amount, reissuable)], amount) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([Reissue(assetId, amount, reissuable)], amount) else ([], unit)

@Callable(i)
func burn(assetId: ByteVector, amount: Int) =
if (i.mustCalculator()) then ([Burn(assetId, amount)], amount) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([Burn(assetId, amount)], amount) else ([], unit)

@Callable(i)
func transferAsset(recipientBytes: ByteVector, amount: Int, assetId: ByteVector) =
if (i.mustCalculator()) then ([ScriptTransfer(Address(recipientBytes), amount, assetId)], amount) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([ScriptTransfer(Address(recipientBytes), amount, assetId)], amount) else ([], unit)

@Callable(i)
func transferAssets(recipientBytes: ByteVector, assetsList: List[ByteVector], amountsList: List[Int]) = {
if (i.mustCalculator()) then {
if (!shutdown && i.mustCalculator()) then {
func addNewTransfer(acc: (List[ScriptTransfer], Int), nextAssetId: ByteVector) = {
let (transfers, j) = acc

Expand All @@ -129,15 +134,46 @@ func transferAssets(recipientBytes: ByteVector, assetsList: List[ByteVector], am

@Callable(i)
func transferWaves(recipientBytes: ByteVector, amount: Int) =
if (i.mustCalculator()) then ([ScriptTransfer(Address(recipientBytes), amount, unit)], amount) else ([], unit)
if (!shutdown && i.mustCalculator()) then ([ScriptTransfer(Address(recipientBytes), amount, unit)], amount) else ([], unit)

@Callable(i)
func call(function: String, args: List[String]) = {
strict checks = [
!shutdown || throwErr("not allowed")
]

let result = calculatorAddress.reentrantInvoke(function, [i.callerPublicKey, args], i.payments)

(nil, result)
}

@Callable(i)
func doShutdown() = {
let multisig = getStringValue(kMultisig).addressFromStringValue()
let publicKeysList = multisig.getStringValue(kPublicKeys).split(separator)

strict checks = [
containsElement(
publicKeysList, i.callerPublicKey.toBase58String()
) || throwErr("shutdown: not allowed")
]

([
BooleanEntry(kShutdown, true)
], unit)
}

@Callable(i)
func cancelShutdown() = {
strict checks = [
i.mustThis()
]

([
DeleteEntry(kShutdown)
], unit)
}

@Callable(i)
func setMultisig(multisig: String) = {
strict checks = [
Expand Down

0 comments on commit 752b49f

Please sign in to comment.