Type
TRANSFER
Validation date
2024-02-28 17:15:18 UTC
Fee
0 UCO

Code (3.27 KB)

@version 1

condition triggered_by: transaction, on: add_liquidity(token1_min_amount, token2_min_amount), as: [
  token_transfers: (
    valid_amounts? = false
    valid_liquidity? = false

    user_amounts = get_user_transfers_amount(transaction)

    if user_amounts.token1 > 0 && user_amounts.token2 > 0
      && user_amounts.token1 >= token1_min_amount && user_amounts.token2 >= token2_min_amount do
      lp_token_supply = State.get("lp_token_supply", 0)
      reserves = State.get("reserves", [token1: 0, token2: 0])

      final_amounts = nil
      if lp_token_supply != 0 do
        # Returns final_amounts.token1 == 0 in case of insufficient funds
        final_amounts = get_final_amounts(user_amounts, reserves, token1_min_amount, token2_min_amount)
      else
        final_amounts = [token1: user_amounts.token1, token2: user_amounts.token2]
      end

      if final_amounts.token1 > 0 && final_amounts.token2 > 0 do
        valid_amounts? = true

        pool_balances = get_pool_balances()
        # Amount = final amounts + potential current balance over reserve
        token1_amount = final_amounts.token1 + (pool_balances.token1 - reserves.token1)
        token2_amount = final_amounts.token2 + (pool_balances.token2 - reserves.token2)

        lp_token_to_mint = get_lp_token_to_mint(token1_amount, token2_amount)

        valid_liquidity? = lp_token_to_mint > 0
      end
    end

    valid_amounts? && valid_liquidity?
  )
]

actions triggered_by: transaction, on: add_liquidity(token1_min_amount, token2_min_amount) do
  pool_balances = get_pool_balances()
  user_amounts = get_user_transfers_amount(transaction)

  lp_token_supply = State.get("lp_token_supply", 0)
  reserves = State.get("reserves", [token1: 0, token2: 0])

  final_amounts = get_final_amounts(user_amounts, reserves, token1_min_amount, token2_min_amount)
  token1_to_refund = user_amounts.token1 - final_amounts.token1
  token2_to_refund = user_amounts.token2 - final_amounts.token2

  token1_amount = pool_balances.token1 - reserves.token1 - token1_to_refund
  token2_amount = pool_balances.token2 - reserves.token2 - token2_to_refund

  lp_token_to_mint = get_lp_token_to_mint(token1_amount, token2_amount)
  lp_token_to_mint_bigint = Math.trunc(lp_token_to_mint * 100_000_000)

  # Remove minimum liquidity if this is the first liquidity if the pool
  # First liquidity minted and burned on pool creation
  if lp_token_supply == 0 do
    lp_token_to_mint_bigint = lp_token_to_mint_bigint - 10
  end

  token_specification = [
    aeip: [8, 18, 19],
    supply: lp_token_to_mint_bigint,
    token_reference: 0x00006394EF24DFDC6FDFC3642FDC83827591A485704BB997221C0B9F313A468BDEAF,
    recipients: [
      [to: transaction.address, amount: lp_token_to_mint_bigint]
    ]
  ]

  new_token1_reserve = pool_balances.token1 - token1_to_refund
  new_token2_reserve = pool_balances.token2 - token2_to_refund

  State.set("lp_token_supply", lp_token_supply + lp_token_to_mint)
  State.set("reserves", [token1: new_token1_reserve, token2: new_token2_reserve])

  if token1_to_refund > 0 do
    Contract.add_token_transfer(to: transaction.address, amount: token1_to_refund, token_address: "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4")
  end

  if token2_to_refund > 0 do
    if "UCO" == "UCO" do
      Contract.add_uco_transfer(to: transaction.address, amount: token2_to_refund)
    else
      Contract.add_token_transfer(to: transaction.address, amount: token2_to_refund, token_address: "UCO")
    end
  end

  Contract.set_type("token")
  Contract.set_content(Json.to_string(token_specification))
end

condition triggered_by: transaction, on: remove_liquidity(), as: [
  token_transfers: (
    valid? = false

    user_amount = get_user_lp_amount(transaction.token_transfers)
    lp_token_supply = State.get("lp_token_supply", 0)

    if user_amount > 0 && lp_token_supply > 0 do
      pool_balances = get_pool_balances()

      token1_to_remove = (user_amount * pool_balances.token1) / lp_token_supply
      token2_to_remove = (user_amount * pool_balances.token2) / lp_token_supply

      valid? = token1_to_remove > 0 && token2_to_remove > 0
    end

    valid?
  )
]

actions triggered_by: transaction, on: remove_liquidity() do
  user_amount = get_user_lp_amount(transaction.token_transfers)
  pool_balances = get_pool_balances()

  lp_token_supply = State.get("lp_token_supply")

  token1_to_remove = (user_amount * pool_balances.token1) / lp_token_supply
  token2_to_remove = (user_amount * pool_balances.token2) / lp_token_supply

  new_token1_reserve = pool_balances.token1 - token1_to_remove
  new_token2_reserve = pool_balances.token2 - token2_to_remove

  State.set("lp_token_supply", lp_token_supply - user_amount)
  State.set("reserves", [token1: new_token1_reserve, token2: new_token2_reserve])

  Contract.set_type("transfer")
  Contract.add_token_transfer(to: transaction.address, amount: token1_to_remove, token_address: "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4")
  if "UCO" == "UCO" do
    Contract.add_uco_transfer(to: transaction.address, amount: token2_to_remove)
  else
    Contract.add_token_transfer(to: transaction.address, amount: token2_to_remove, token_address: "UCO")
  end
end

condition triggered_by: transaction, on: swap(min_to_receive), as: [
  token_transfers: (
    valid? = false

    transfer = get_user_transfer(transaction)
    if transfer != nil do
        swap = get_swap_infos(transfer.token_address, transfer.amount)

        valid? = swap.output_amount > 0 && swap.output_amount >= min_to_receive
    end

    valid?
  )
]

actions triggered_by: transaction, on: swap(_min_to_receive) do
  transfer = get_user_transfer(transaction)

  swap = get_swap_infos(transfer.token_address, transfer.amount)

  pool_balances = get_pool_balances()
  token_to_send = nil
  token1_volume = 0
  token2_volume = 0
  token1_fee = 0
  token2_fee = 0
  token1_protocol_fee = 0
  token2_protocol_fee = 0
  if transfer.token_address == "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4" do
    pool_balances = Map.set(pool_balances, "token2", pool_balances.token2 - swap.output_amount)
    token_to_send = "UCO"
    token1_volume = transfer.amount
    token1_fee = swap.fee
    token1_protocol_fee = swap.protocol_fee
  else
    pool_balances = Map.set(pool_balances, "token1", pool_balances.token1 - swap.output_amount)
    token_to_send = "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4"
    token2_volume = transfer.amount
    token2_fee = swap.fee
    token2_protocol_fee = swap.protocol_fee
  end

  State.set("reserves", [token1: pool_balances.token1, token2: pool_balances.token2])

  stats = State.get("stats", [
    token1_total_fee: 0,
    token2_total_fee: 0,
    token1_total_volume: 0,
    token2_total_volume: 0,
    token1_total_protocol_fee: 0,
    token2_total_protocol_fee: 0,
  ])

  token1_total_fee = Map.get(stats, "token1_total_fee") + token1_fee
  token2_total_fee = Map.get(stats, "token2_total_fee") + token2_fee
  token1_total_volume = Map.get(stats, "token1_total_volume") + token1_volume
  token2_total_volume = Map.get(stats, "token2_total_volume") + token2_volume
  token1_total_protocol_fee = Map.get(stats, "token1_total_protocol_fee") + token1_protocol_fee
  token2_total_protocol_fee = Map.get(stats, "token2_total_protocol_fee") + token2_protocol_fee

  stats = Map.set(stats, "token1_total_fee", token1_total_fee)
  stats = Map.set(stats, "token2_total_fee", token2_total_fee)
  stats = Map.set(stats, "token1_total_volume", token1_total_volume)
  stats = Map.set(stats, "token2_total_volume", token2_total_volume)
  stats = Map.set(stats, "token1_total_protocol_fee", token1_total_protocol_fee)
  stats = Map.set(stats, "token2_total_protocol_fee", token2_total_protocol_fee)

  State.set("stats", stats)

  Contract.set_type("transfer")
  if token_to_send == "UCO" do
    Contract.add_uco_transfer(to: transaction.address, amount: swap.output_amount)
  else
    Contract.add_token_transfer(to: transaction.address, amount: swap.output_amount, token_address: token_to_send)
  end

  if swap.protocol_fee > 0 do
    if transfer.token_address == "UCO" do
      Contract.add_uco_transfer(to: 0x0000CFC185E5836BDC967B1DF7E8C0D3D61A0F7B1101AA077A7B8E307C33310D0D2B, amount: swap.protocol_fee)
    else
      Contract.add_token_transfer(to: 0x0000CFC185E5836BDC967B1DF7E8C0D3D61A0F7B1101AA077A7B8E307C33310D0D2B, amount: swap.protocol_fee, token_address: transfer.token_address)
    end
  end

end

condition triggered_by: transaction, on: update_code(), as: [
  previous_public_key: (
    # Pool code can only be updated from the router contract of the dex

    # Transaction is not yet validated so we need to use previous address
    # to get the genesis address
    previous_address = Chain.get_previous_address()
    Chain.get_genesis_address(previous_address) == 0x000066CD867DA536A73D39CF05174387923358DC0009A29CC7162D4AED00675DAB55
  )
]

actions triggered_by: transaction, on: update_code() do
  params = [
    "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4",
    "UCO",
    0x0000818EF23676779DAE1C97072BB99A3E0DD1C31BAD3787422798DBE3F777F74A43,
    0x00006394EF24DFDC6FDFC3642FDC83827591A485704BB997221C0B9F313A468BDEAF
  ]

  new_code = Contract.call_function(0x000086E60124C986EBCAA5AFFB7A3DB8213072A132233FE61CF45651FDCF3C4CECEA, "get_pool_code", params)

  if Code.is_valid?(new_code) && !Code.is_same?(new_code, contract.code) do
    Contract.set_type("contract")
    Contract.set_code(new_code)
  end
end

condition triggered_by: transaction, on: set_protocol_fee(new_protocol_fee), as: [
  content: new_protocol_fee <= 1 && new_protocol_fee >= 0,
  previous_public_key: (
    # Pool code can only be updated from the master contract of the dex

    # Transaction is not yet validated so we need to use previous address
    # to get the genesis address
    previous_address = Chain.get_previous_address()
    Chain.get_genesis_address(previous_address) == 0x0000E46F8E90074DDF1DFC46385E07D826D35251F3A7B7FF65AD6F7E4B138AFF7C10
  )
]

actions triggered_by: transaction, on: set_protocol_fee(new_protocol_fee) do
  State.set("protocol_fee", new_protocol_fee)
end

export fun get_ratio(token_address) do
  reserves = State.get("reserves", [token1: 0, token2: 0])
  ratio = 0

  token_address = String.to_uppercase(token_address)

  if reserves.token1 > 0 && reserves.token2 > 0 do
    if token_address == "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4" do
      ratio = reserves.token2 / reserves.token1
    else
      ratio = reserves.token1 / reserves.token2
    end
  end
  ratio
end

export fun get_equivalent_amount(token_address, amount) do
  reserves = State.get("reserves", [token1: 0, token2: 0])
  ratio = 0

  token_address = String.to_uppercase(token_address)

  if reserves.token1 > 0 && reserves.token2 > 0 do
    if token_address == "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4" do
      ratio = reserves.token2 / reserves.token1
    else
      ratio = reserves.token1 / reserves.token2
    end
  end

  amount * ratio
end

export fun get_lp_token_to_mint(token1_amount, token2_amount) do
  lp_token_supply = State.get("lp_token_supply", 0)
  reserves = State.get("reserves", [token1: 0, token2: 0])

  if lp_token_supply == 0 || reserves.token1 == 0 || reserves.token2 == 0 do
    # First liquidity
    Math.sqrt(token1_amount * token2_amount)
  else
    mint_amount1 = (token1_amount * lp_token_supply) / reserves.token1
    mint_amount2 = (token2_amount * lp_token_supply) / reserves.token2

    if mint_amount1 < mint_amount2 do
      mint_amount1
    else
      mint_amount2
    end
  end
end

export fun get_swap_infos(token_address, amount) do
  output_amount = 0
  fee = 0
  protocol_fee = 0
  price_impact = 0

  reserves = State.get("reserves", [token1: 0, token2: 0])
  token_address = String.to_uppercase(token_address)

  if reserves.token1 > 0 && reserves.token2 > 0 do
    fee = amount * 0.0025
    protocol_fee = amount * State.get("protocol_fee", 0.25) / 100
    amount_with_fee = amount - fee - protocol_fee

    market_price = 0

    if token_address == "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4" do
      market_price = amount_with_fee * (reserves.token2 / reserves.token1)
      amount = (amount_with_fee * reserves.token2) / (amount_with_fee + reserves.token1)
      if amount < reserves.token2 do
        output_amount = amount
      end
    else
      market_price = amount_with_fee * (reserves.token1 / reserves.token2)
      amount = (amount_with_fee * reserves.token1) / (amount_with_fee + reserves.token2)
      if amount < reserves.token1 do
        output_amount = amount
      end
    end

    if output_amount > 0 do
      # This check is necessary as there might be some approximation in small decimal calculation
      if market_price > output_amount do
        price_impact = ((market_price / output_amount) - 1) * 100
      else
        price_impact = 0
      end
    end
  end

  [
    output_amount: output_amount,
    fee: fee,
    protocol_fee: protocol_fee,
    price_impact: price_impact
  ]
end

export fun get_remove_amounts(lp_token_amount) do
  reserves = State.get("reserves", [token1: 0, token2: 0])
  lp_token_supply = State.get("lp_token_supply", 0)

  token1_to_remove = 0
  token2_to_remove = 0

  if lp_token_supply > 0 && lp_token_amount < lp_token_supply do
    token1_to_remove = (lp_token_amount * reserves.token1) / lp_token_supply
    token2_to_remove = (lp_token_amount * reserves.token2) / lp_token_supply
  end

  [token1: token1_to_remove, token2: token2_to_remove]
end

export fun get_pool_infos() do
  reserves = State.get("reserves", [token1: 0, token2: 0])
  stats = State.get("stats", [
    token1_total_fee: 0,
    token2_total_fee: 0,
    token1_total_volume: 0,
    token2_total_volume: 0,
    token1_total_protocol_fee: 0,
    token2_total_protocol_fee: 0,
  ])

  [
    token1: [
      address: "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4",
      reserve: reserves.token1
    ],
    token2: [
      address: "UCO",
      reserve: reserves.token2
    ],
    lp_token: [
      address: 0x00006394EF24DFDC6FDFC3642FDC83827591A485704BB997221C0B9F313A468BDEAF,
      supply: State.get("lp_token_supply", 0)
    ],
    fee: 0.25,
    protocol_fee: State.get("protocol_fee", 0.25),
    stats: stats
  ]
end

fun get_final_amounts(user_amounts, reserves, token1_min_amount, token2_min_amount) do
  final_token1_amount = 0
  final_token2_amount = 0

  if reserves.token1 > 0 && reserves.token2 > 0 do
    token2_ratio = reserves.token2 / reserves.token1
    token2_equivalent_amount = user_amounts.token1 * token2_ratio

    if token2_equivalent_amount <= user_amounts.token2 && token2_equivalent_amount >= token2_min_amount do
      final_token1_amount = user_amounts.token1
      final_token2_amount = token2_equivalent_amount
    else
      token1_ratio = reserves.token1 / reserves.token2
      token1_equivalent_amount = user_amounts.token2 * token1_ratio

      if token1_equivalent_amount <= user_amounts.token1 && token1_equivalent_amount >= token1_min_amount do
        final_token1_amount = token1_equivalent_amount
        final_token2_amount = user_amounts.token2
      end
    end
  else
    # No reserve
    final_token1_amount = user_amounts.token1
    final_token2_amount = user_amounts.token2
  end

  [token1: final_token1_amount, token2: final_token2_amount]
end

fun get_user_transfers_amount(tx) do
  contract_address = 0x0000818EF23676779DAE1C97072BB99A3E0DD1C31BAD3787422798DBE3F777F74A43

  token1_amount = 0
  token2_amount = 0
  transfers = Map.get(tx.token_transfers, contract_address, [])

  uco_amount = Map.get(tx.uco_transfers, contract_address)
  if uco_amount != nil do
    transfers = List.prepend(transfers, [token_address: "UCO", amount: uco_amount])
  end

  if List.size(transfers) == 2 do
    for transfer in transfers do
      if transfer.token_address == "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4" do
        token1_amount = transfer.amount
      end
      if transfer.token_address == "UCO" do
        token2_amount = transfer.amount
      end
    end
  end

  [token1: token1_amount, token2: token2_amount]
end

fun get_user_transfer(tx) do
  contract_address = 0x0000818EF23676779DAE1C97072BB99A3E0DD1C31BAD3787422798DBE3F777F74A43

  token_transfer = nil
  transfers = Map.get(tx.token_transfers, contract_address, [])

  uco_amount = Map.get(tx.uco_transfers, contract_address)
  if uco_amount != nil do
    transfers = List.prepend(transfers, [token_address: "UCO", amount: uco_amount])
  end

  transfer = List.at(transfers, 0)

  tokens = [
    "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4",
    "UCO"
  ]

  if List.size(transfers) == 1 && List.in?(tokens, transfer.token_address) do
    token_transfer = transfer
  end

  token_transfer
end

fun get_user_lp_amount(token_transfers) do
  lp_token = 0x00006394EF24DFDC6FDFC3642FDC83827591A485704BB997221C0B9F313A468BDEAF

  lp_amount = 0
  transfers = Map.get(token_transfers, Chain.get_burn_address(), [])

  for transfer in transfers do
    if transfer.token_address == lp_token do
      lp_amount = transfer.amount
    end
  end

  lp_amount
end

fun get_pool_balances() do
  balances = Chain.get_balance(contract.address)

  token2_balance = 0
  if "UCO" == "UCO" do
    token2_balance = balances.uco
  else
    token2_id = [token_address: "UCO", token_id: 0]
    token2_balance = Map.get(balances.tokens, token2_id, 0)
  end

  token1_id = [token_address: "00003DF600E329199BF3EE8FBE2B8223413D70BCDD97E15089E6A74D94DE3F1173B4", token_id: 0]
  [
    token1: Map.get(balances.tokens, token1_id, 0),
    token2: token2_balance
  ]
end

Content (0 B)

State (264 B)

{
  "lp_token_supply": 9276.39381855,
  "reserves": {
    "token1": 64.44767156,
    "token2": 1022844.4579521
  },
  "stats": {
    "token1_total_fee": 0.07893545,
    "token1_total_protocol_fee": 0.07893545,
    "token1_total_volume": 31.57428792,
    "token2_total_fee": 980.02575747,
    "token2_total_protocol_fee": 980.02575747,
    "token2_total_volume": 392010.30317095
  }
}
                  
Movements (0)

Ownerships (1)

  • Secret shared with 1 key

    Encoded secret

    91499184159E0B7E1B73560F7D584F82277900C7671FA7DFC16F786A49C62D913BEC4C844369F8E5CA0AE98520616E674CBFAD94D9F6BB687A2688C4

    Authorized keys

    • 00017877BCF4122095926A49489009649603AB129822A19EF9D573B8FD714911ED7F

Contract recipients (0)

Inputs (0)

Contract inputs (0)

Proofs and signatures

Previous public key

0001E57C2C28468D560BA95557DBE48471F06450DBE6835DA94D7BFA705A7A2FFB77

Previous signature

B26BE60CE323F4E7C0CC890B49B7866394A5846DB123BEDC8A8A0964F8DEE4737ABEE9CE0F89B5C0C04654AE29C5057C7E67519FA86A535C6CBC1D7E88019B02

Origin signature

3045022100F67B4B18EE705E542ED8B9C1848601E8BFDC6AAEA650681FD7BA4F514B39E17F0220172F0A07CB21828585D367E411BB215F3598C7353C51BF4011641E92C693E5F6

Proof of work

010204892A04DBE05192BCB662689E7E19700F3F7202028EF0A9F20635C4948436ED85A43BA3017281EE97781342DD8F24ED6F583B5FE542830FDB5191A11843A72A61

Proof of integrity

006F5F453B790077EC1E0175FFCB2D9DADCA9674E7F6A473EC236F5EC2997BE68A

Coordinator signature

F265969578CD9D9284B41251E9F06B891425399C383590D9AAAE9FB411192A32D8098F8A187F12CE8B987EE4436E19365FC404073D0737EE2B1DD559596CDA03

Validator #1 public key

0001B01EEF96BA7E95FC844D456CE8868F18864519FC9532E1751C2035FD044DD5D0

Validator #1 signature

DC70055A6F575D0731091BA2D7CE781E32C8ECD622D7BD289BE3526816F45CF8B7DD19B818CB711C5FD9920903E650BAD347377925C588080C7A67C47013780C

Validator #2 public key

00010F2A0E4C424582A94BD90E05FE6931628F91988ABBE387D365994F1F3FCF5A12

Validator #2 signature

7B43C48EC0B10B6D615A65B57C6AAB5AFBDFBE80B9650E5D0741269E43C0869973C56ADB4CA5229BB08502031DADCB85F34B242A88FFD20A9F90F296A6947C0D

Validator #3 public key

000151C124A6211CD402FD1CFE560C5DB51ED0CBEF44B09B21A41206028E7E5942BF

Validator #3 signature

2EB5330C8B1E3E0A5F4A77801576664698E614FE2F788811DBA30B2A2F726C0D06C83E4B58DFC5F56BF8701782D6BE2395CF2A13AD5839A6BF7B9EEA2EB6A600

Validator #4 public key

00011B58ED42235461734EAF253BD97A80B92899ABCC3BE680D44B6825DD2A88A947

Validator #4 signature

DEA33442023DD5685623DB3BAF584CA483821A09E937F074B8FCA41A7CF1CCD5609BEC6C252240AFDF123E7DF0B4A8F77EC579B764F30631EB6CF0A8B8F2570F

Validator #5 public key

000103E30584AD8DE66F9E29419D5D0ABEE5A76722C9FD0D012BDDE3A6E2B149C48D

Validator #5 signature

B632D75270A1A54EF965DB07749E6354674D6E9ECDCB038298790C5499616D705AF1A88D16710639ACE6E7619643B9BDA7CB7E8875226C44F4745B8F6BCB860A