---
# System prepended metadata

title: Đáp án

---

# Đáp án

### Question 158160
```
use aiken/dict
use aiken/interval
use aiken/list
use aiken/transaction.{
  Mint, NoDatum, Output, ScriptContext, Transaction, TransactionId,
}
use aiken/transaction/credential.{Address}
use aiken/transaction/value

pub type Config {
  allowed_name: ByteArray,
  target_address: Address,
  minimum_ada: Int,
  minting_amount: Int,
}

fn certificate(config: Config, _redeemer: Data, ctx: ScriptContext) -> Bool {
  expect Mint(pid) = ctx.purpose
  let txn = ctx.transaction
  let address_ada =
    list.foldl(
      txn.outputs,
      0,
      fn(o, result) {
        if o.address == config.target_address {
          value.lovelace_of(o.value) + result
        } else {
          result
        }
      },
    )
  expect [(token_name, qty)] =
    txn.mint |> value.from_minted_value() |> value.tokens(pid) |> dict.to_list()
  (address_ada >= config.minimum_ada)? && //
  (token_name == config.allowed_name)? && //
  (qty == config.minting_amount)?
}
```


### Question 158163
```
use aiken/list
use aiken/transaction.{Transaction, OutputReference, Input, TransactionId, ScriptContext, Output, NoDatum, InlineDatum, Spend}
use aiken/hash.{Blake2b_224, Hash}
use aiken/transaction/credential.{VerificationKey, from_verification_key, from_script, VerificationKeyCredential}
use aiken/transaction/value.{Value}
use aiken/dict
use aiken/interval

pub type FundraisingCampaignDatum {
  beneficiary: Hash<Blake2b_224, VerificationKey>,
  target_amount: Int, // lovelace
  current_amount: Int, // lovelace
}

pub type Redeemer {
  // create fund raising
  FundRasingCampaign
  // add money to fund
  FundAddingMoney
}

pub fn find_own_input(tx: Transaction, out_ref: OutputReference) -> Option<Input> {
  list.find(tx.inputs, fn(input) { input.output_reference == out_ref })
}

fn raise_fund_campaign(datum: FundraisingCampaignDatum, redeemer: Redeemer, context: ScriptContext) -> Bool {
  let ScriptContext { purpose, transaction } = context
  expect Spend(out_ref) = purpose
  expect Some(own_input) = find_own_input(transaction, out_ref)
  when redeemer is {
    FundRasingCampaign -> {
      list.has(transaction.extra_signatories, datum.beneficiary)? && //
      datum.target_amount > 0 && //
      datum.current_amount == 0
    }
    FundAddingMoney -> {
      expect [treasury] =
        list.filter(
          transaction.outputs,
          fn(output) { output.address == own_input.output.address },
        )
      expect InlineDatum(inl_dtm) = treasury.datum
      expect fund_raising_datum: FundraisingCampaignDatum = inl_dtm

      let total_money_added = 
        list.foldl(
          transaction.outputs,
          0,
          fn(output, total) {
            if output.address.payment_credential == VerificationKeyCredential(datum.beneficiary) {
              total + value.lovelace_of(output.value)
            } else {
              total
            }
          }
        )
      
      and {
        (datum.current_amount < datum.target_amount)?, //
        (fund_raising_datum.beneficiary == datum.beneficiary)?, //
        (fund_raising_datum.target_amount == datum.target_amount)?, //
        (fund_raising_datum.current_amount == datum.current_amount + total_money_added)? //
      }
    }
  }
}
```

### Question 4 (DeleteDigit)
```
use aiken/bytearray
use aiken/list
use aiken/int

fn deleteDigit(n: Int) {
    let n_str = string.to_bytearray(string.from_int(n))
    let length = bytearray.length(n_str)
    let numbers = list.repeat(n_str, length)
    let deleted_numbers = list.indexed_map(numbers, fn(i, num) {
        let deleted_num = bytearray.concat(bytearray.take(num, i), bytearray.drop(num, i))
        let new_num = int.from_utf8(deleted_num)
        when new_num is {
            Some(a) -> a
            None -> None
        }
    })
    list.max(deleted_numbers)
}
```
