Skip to content

Commit

Permalink
feat: updating specs with emergency token owner guard
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Dec 13, 2023
1 parent 574ef47 commit 9e21800
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 23 deletions.
14 changes: 8 additions & 6 deletions lib/aiken-virtual-dex/types.ak
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,24 @@ pub type FeeInfoRedeemer {
}

// 5. TradeAccount
pub type TradeAddressDatum {
pub type TradeAccountDatum {
TradeNormalDatum
TradeEmergencyDatum { valid_since: Int }
TradeEmergencyDatum { valid_since: Int, minter: ByteArray }
EmergencyIncidentInitiation { owner: Address }
}

pub type TradeAddressRedeemer {
pub type TradeAccountRedeemer {
TradeNormalAction
TradeEmergencyAction { withdraw_output: OutputReference }
InitiateEmergencyIncident
}

// 6. ChangeAccount
pub type ChangeAddressDatum {
ChangeAddressDatum
pub type ChangeAccountDatum {
ChangeAccountDatum
}

pub type ChangeAddressRedeemer {
pub type ChangeAccountRedeemer {
OwnerWithdraw
AppRefill
MassRefill { refill_output: OutputReference }
Expand Down
6 changes: 3 additions & 3 deletions lib/aiken-virtual-dex/validators/change_account.ak
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use aiken_virtual_dex/common.{
redeemer_from, value_geq,
}
use aiken_virtual_dex/types.{
AppRefill, ChangeAddressDatum, ChangeAddressRedeemer, MassRefill, OracleDatum,
AppRefill, ChangeAccountDatum, ChangeAccountRedeemer, MassRefill, OracleDatum,
OwnerWithdraw,
}
use aiken_virtual_dex/utils.{get_account_token_name}

pub fn change_account_logic(
oracle_nft: PolicyId,
account_number: Int,
_datum: ChangeAddressDatum,
redeemer: ChangeAddressRedeemer,
_datum: ChangeAccountDatum,
redeemer: ChangeAccountRedeemer,
context: ScriptContext,
) -> Bool {
True
Expand Down
12 changes: 7 additions & 5 deletions lib/aiken-virtual-dex/validators/trade_account.ak
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use aiken/transaction.{InlineDatum, ScriptContext, Spend, find_input}
use aiken/transaction/value.{PolicyId}
use aiken_virtual_dex/common.{all_key_signed, key_signed, only_input_datum_with}
use aiken_virtual_dex/types.{
OracleDatum, TradeAddressDatum, TradeAddressRedeemer, TradeEmergencyAction,
TradeEmergencyDatum, TradeNormalAction,
InitiateEmergencyIncident, OracleDatum, TradeAccountDatum,
TradeAccountRedeemer, TradeEmergencyAction, TradeEmergencyDatum,
TradeNormalAction,
}

pub fn trade_account_logic(
oracle_nft: PolicyId,
owner: ByteArray,
_datum: TradeAddressDatum,
redeemer: TradeAddressRedeemer,
_datum: TradeAccountDatum,
redeemer: TradeAccountRedeemer,
context: ScriptContext,
) -> Bool {
let ScriptContext { purpose, transaction } = context
Expand All @@ -23,12 +24,13 @@ pub fn trade_account_logic(
TradeEmergencyAction { withdraw_output } -> {
expect Some(input) = find_input(transaction.inputs, withdraw_output)
expect InlineDatum(raw_datum) = input.output.datum
expect casted_datum: TradeAddressDatum = raw_datum
expect casted_datum: TradeAccountDatum = raw_datum
when casted_datum is {
TradeEmergencyDatum { .. } ->
key_signed(transaction.extra_signatories, owner)
_ -> False
}
}
InitiateEmergencyIncident -> True
}
}
10 changes: 10 additions & 0 deletions specs/5_trade_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

- Stating that the UTxO is attached with an emergency token, ready for user withdrawal without passing through application logic

3. InitiateEmergencyIncident {owner}

- Stating that the UTxO is ready to be the authorization input for minting the emergency token.

## User Action

1. Normal operation - Redeemer `TradeNormalAction`
Expand All @@ -27,3 +31,9 @@
- Signed by owner
- Validity range is after `valid_since`
- `EmergencyToken` with correct token name of hash of current address is burnt in current transaction

3. Emergency operation - Redeemer `TradeEmergencyAction {withdraw_output}`

- Signed by owner
- Validity range is after `valid_since`
- `EmergencyToken` with correct token name of hash of current address is burnt in current transaction
2 changes: 1 addition & 1 deletion specs/6_change_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## Datum

1. ChangeAddressDatum
1. ChangeAccountDatum

- Stating that the UTxO is ready for normal app operation, including placing orders, taking orders and authorized withdrawal

Expand Down
3 changes: 1 addition & 2 deletions specs/8_emergency_token.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

## Parameter

- `owner`: The pub key has of account owner

## User Action

1. Mint - Redeemer `EMint {current_timestamp, trade_account_address}`

- The `AssetName` must be in form of hash of `ValidatorHash` + `StakeCredentialHash`
- There must be 1 input from `trade_account_address` with datum information of account owner.
- There must be 1 output to `trade_account_address`
- The output datum to `trade_account_address` with `valid_since` after current signing interval
- Must be signed by owner
Expand Down
6 changes: 3 additions & 3 deletions validators/change_account.ak
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use aiken/transaction.{ScriptContext}
use aiken/transaction/value.{PolicyId}
use aiken_virtual_dex/types.{ChangeAddressDatum, ChangeAddressRedeemer}
use aiken_virtual_dex/types.{ChangeAccountDatum, ChangeAccountRedeemer}
use aiken_virtual_dex/validators/change_account.{change_account_logic}

validator(oracle_nft: PolicyId, account_number: Int) {
fn change_account(
datum: ChangeAddressDatum,
redeemer: ChangeAddressRedeemer,
datum: ChangeAccountDatum,
redeemer: ChangeAccountRedeemer,
context: ScriptContext,
) {
change_account_logic(oracle_nft, account_number, datum, redeemer, context)
Expand Down
6 changes: 3 additions & 3 deletions validators/trade_account.ak
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use aiken/transaction.{ScriptContext}
use aiken/transaction/value.{PolicyId}
use aiken_virtual_dex/types.{TradeAddressDatum, TradeAddressRedeemer}
use aiken_virtual_dex/types.{TradeAccountDatum, TradeAccountRedeemer}
use aiken_virtual_dex/validators/trade_account.{trade_account_logic}

validator(oracle_nft: PolicyId, owner: ByteArray) {
fn trade_account(
datum: TradeAddressDatum,
redeemer: TradeAddressRedeemer,
datum: TradeAccountDatum,
redeemer: TradeAccountRedeemer,
context: ScriptContext,
) {
trade_account_logic(oracle_nft, owner, datum, redeemer, context)
Expand Down

0 comments on commit 9e21800

Please sign in to comment.