;; multiway-swap
;; Contract to facilitate a multiway NFT swap.
;; Each of the parties involved has to call confirm-and-escrow to escrow their dues.
;; Once all parties confirmed, finalize has to be called to redistribute escrowed items.
;; At any point before the swap is finalized it can be canceled by a party involved, 
;; regardles of whether they already called confirm-and-escrow or not.

;; traders
(define-constant TRADER-1 'SP12VXAS7H2MT7R8116F4RA1FCS21DZ86B4DTN80J)
(define-constant TRADER-2 'SP14NSM2BAB9MGMYNXJB93NY4EF4NFRW3G3EFBZDX)
(define-constant TRADER-3 'SP14W78Q821B3HQ3ED30624Z1F13X4JMFZY3N5SK4)
(define-constant TRADER-4 'SP194MH71GY297FZS03ECBKKK375KAH57FFTFYGNR)
(define-constant TRADER-5 'SP1FW0F2ZYZHXT1BVV8HX8ZXG3MRM0ZVH73QE9VSV)
(define-constant TRADER-6 'SP1G9PZMQSFRQ7Q98XP7JMNE2C22RXK1W61RXTNKP)
(define-constant TRADER-7 'SP1HHSDYJ0SGAM6K2W01ZF5K7AJFKWMJNH365ZWS9)
(define-constant TRADER-8 'SP1JCPNPAMAQJ364AFHPTW3HY7X0HYZ3TJ0ZDGWZH)
(define-constant TRADER-9 'SP1JF9VSNJBP4YZVC7AJ9CE6CXBD2ZV0W67T0E4T0)
(define-constant TRADER-10 'SP1KBVBP3AZP7YA968Y3G14A17P9XXFPBPEVF5EG9)
(define-constant TRADER-11 'SP1N1F89ADJRPY293K0D9HCREYDHGQNVEC6EQNHPJ)
(define-constant TRADER-12 'SP1N4052NVKW5DSNW7Z1G4PKEQ53ZH9T15A9NCJ3M)
(define-constant TRADER-13 'SP1Q6N226KFMA496MVWSB0VZC0T0R9FSKTYYYZ403)
(define-constant TRADER-14 'SP1T07GK9H4M0WP4N1DSSA7NJ7GNTQZ0GBZM0GAR2)
(define-constant TRADER-15 'SP1T7VV0H6TWC3B834XG62VAPXHP3245VJSCDGX0K)
(define-constant TRADER-16 'SP1XY24C7AJ8XZ2QQ5BMD43YWJBYAM388G00P354V)
(define-constant TRADER-17 'SP228WEAEMYX21RW0TT5T38THPNDYPPGGVW2RP570)
(define-constant TRADER-18 'SP23DZ9XYT3YNF70MHAV9Y3622H8B3DX10WMWT2T9)
(define-constant TRADER-19 'SP24ZBZ8ZE6F48JE9G3F3HRTG9FK7E2H6K2QZ3Q1K)
(define-constant TRADER-20 'SP27E3TDKYNH3C11RBDFPD5WGR6FV0VN08RKX4D2N)
(define-constant TRADER-21 'SP28NCDY6V4T7NJBMYGTJ55NHMXMC0GG806JW1ZTB)
(define-constant TRADER-22 'SP2H6HVZK6X3Z8F4PKF284AZJR6FH4H9J4W6KVV8T)
(define-constant TRADER-23 'SP2NM9ZX3A1NWJN5Q8X97RTC0AMG4FHBWSCZSYRPV)
(define-constant TRADER-24 'SP2PT6ZNH3E1FY91DCBCCQWP9YAFPX3V66VQRNSRT)
(define-constant TRADER-25 'SP2QAG09GBM8Y9HK05MSC30X0A09VW11T23ES27GX)
(define-constant TRADER-26 'SP2SFZX1WJSKT1GA2STDT6E5NWDX44GW4BB8DW4DJ)
(define-constant TRADER-27 'SP2TW1D8YF5CE0NDP5VCR5NMTPHQ4PQR1KBB4NQ5Q)
(define-constant TRADER-28 'SP2XHJBS68TS7SQN41XAVYAJVWYW47Y1DG3WCVGVD)
(define-constant TRADER-29 'SP31EEHQHNCGNEQ24RK06S3J2VNR6SBXET4AESXAM)
(define-constant TRADER-30 'SP31SJ2X5683KDX8P58HWRA2DXY3ED4WZ6Z3DM0A9)
(define-constant TRADER-31 'SP3273YEPG4QZWX0ENQ98FBT1N2Y06XW820STP7NN)
(define-constant TRADER-32 'SP33N5R751MG99QAM4CN6HQ3MDTYBR71SB4NXVGT1)
(define-constant TRADER-33 'SP35Q9M893AEGZWBRWWBNXWWH7RTPC6WKD6XP5HAJ)
(define-constant TRADER-34 'SP364J7EDJXRE1FPDZDABP9M58HPY4G88BFCP2HD0)
(define-constant TRADER-35 'SP38WCGSSQJBFAKH77R93AMTHBBEF83DQ6EJ358F2)
(define-constant TRADER-36 'SP3BTPH354JEM3E8AYAHQS9SWJ591TJQYD9QK0MCF)
(define-constant TRADER-37 'SP3C5JYPB8YE5H9WC2SM196RVJ0JXN2GHCWXJWSES)
(define-constant TRADER-38 'SP3MCMBPX5YSB59J5E1HEKYMKVAJEYX5Y5EWE37KK)
(define-constant TRADER-39 'SP3P4E5DQBJXMQ6MY5CR67G8RT9C5E8D3JK80MMKH)
(define-constant TRADER-40 'SP3R91G719XMPED4S1ZP37YHF30CAV1KGEKB8YXDT)
(define-constant TRADER-41 'SP3VCX5NFQ8VCHFS9M6N40ZJNVTRT4HZ62WFH5C4Q)
(define-constant TRADER-42 'SPCV4DY5FJZCY5TYX3EQ61HDR9MVH9R39YJ35CH7)
(define-constant TRADER-43 'SPHECV0WMKDY9CQHMXR5PEZESQ2PGY8Q8PTBRWD2)
(define-constant TRADER-44 'SPHK8A7P61C6ASWKYDX1PCDX9YA54DKVJN49EXGJ)
(define-constant TRADER-45 'SPM1Q7YG18378H6W254YN8PABEVRPT38ZCY01SJD)
(define-constant TRADER-46 'SPMDGP7AP5JQTDYY83V0Q7JD3CM7YQRXQYWW3E54)
(define-constant TRADER-47 'SPN3Y24JD5B17DN9Y8AEQGQV4VVWA644ACXBE3XE)
(define-constant TRADER-48 'SPQD0TF4NWKM05C4S04VPGFTAY1GRTC7TW7K6WA1)
(define-constant TRADER-49 'SPQE3J7XMMK0DN0BWJZHGE6B05VDYQRXRMDV734D)
(define-constant TRADER-50 'SPSR8THZK8YM777MBAKQHQTQAP96V2XAR1H5W6FK)
(define-constant TRADER-51 'SPV48Q8E5WP4TCQ63E9TV6KF9R4HP01Z8WS3FBTG)

;; receivers


;; constants
(define-constant ERR_ESCROW_NOT_FILLED u401)
(define-constant ERR_SWAP_FINALIZED u402)
(define-constant ERR_RELEASING_ESCROW_FAILED u491)
(define-constant ERR_SWAP_CANCELED u499)

(define-constant ERR_IS_NOT_TRADER u410)
(define-constant ERR_CALLER_ALREADY_ESCROWED u411)
(define-constant ERR_FAILED_TO_ESCROW_STX u412)
(define-constant ERR_FAILED_TO_ESCROW_NFT u413)

(define-constant SWAP_STATE_ACTIVE u100)
(define-constant SWAP_STATE_READY_TO_FINALIZE u101)
(define-constant SWAP_STATE_FINALIZED u102)
(define-constant SWAP_STATE_CANCELED u109)

(define-constant TRADER_STATE_ACTIVE u110)
(define-constant TRADER_STATE_CONFIRMED u111)
(define-constant TRADER_STATE_CANCELED u119)

(define-constant NUM_TRADERS u51)

;; data maps and vars
(define-data-var swapState uint SWAP_STATE_ACTIVE)
(define-data-var confirmCount uint u0)

(define-map TraderState principal uint)

;; Set TraderState of each trader to TRADER_STATE_ACTIVE.
(map-set TraderState TRADER-1 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-2 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-3 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-4 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-5 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-6 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-7 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-8 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-9 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-10 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-11 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-12 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-13 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-14 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-15 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-16 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-17 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-18 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-19 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-20 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-21 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-22 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-23 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-24 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-25 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-26 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-27 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-28 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-29 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-30 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-31 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-32 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-33 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-34 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-35 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-36 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-37 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-38 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-39 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-40 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-41 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-42 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-43 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-44 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-45 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-46 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-47 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-48 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-49 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-50 TRADER_STATE_ACTIVE)
(map-set TraderState TRADER-51 TRADER_STATE_ACTIVE)

;; private functions
(define-private (deposit-escrow) 
    (begin
	(if (is-eq tx-sender TRADER-1)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-2)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-3)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-4)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-5)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-6)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-7)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-8)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-9)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-10)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-11)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-12)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-13)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-14)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-15)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-16)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-17)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-18)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-19)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-20)
            (begin
		(unwrap! (contract-call? 'SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173.the-explorer-guild transfer u78 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
		(unwrap! (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.venice-visuals transfer u46 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
		(unwrap! (contract-call? 'SP22GHCK5W3EK5VC1PEB3VVCBVFRQNTQXV9306QA0.bear-market-buddies transfer u647 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
		(unwrap! (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.stacks-parrots-3d transfer u6 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
		(unwrap! (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.byzantion-stacks-parrots transfer u38 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
		(unwrap! (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.phases-of-satoshi transfer u114 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
		(unwrap! (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.virtual-tulips transfer u192 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
		(unwrap! (contract-call? 'SPNWZ5V2TPWGQGVDR6T7B6RQ4XMGZ4PXTEE0VQ0S.guests-hosted-stacks-parrots transfer u58 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
		(unwrap! (contract-call? 'SP3252T1HMQHZTA9S22WZ2HZMKC4CVH965SHSERTH.the-guests-woymuls transfer u50 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
		(unwrap! (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.venice-visuals transfer u25 tx-sender (as-contract tx-sender)) (err ERR_FAILED_TO_ESCROW_NFT))
	    )
            true
        )
	(if (is-eq tx-sender TRADER-21)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-22)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-23)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-24)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-25)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-26)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-27)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-28)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-29)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-30)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-31)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-32)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-33)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-34)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-35)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-36)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-37)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-38)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-39)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-40)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-41)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-42)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-43)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-44)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-45)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-46)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-47)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-48)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-49)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-50)
            (begin
	    )
            true
        )
	(if (is-eq tx-sender TRADER-51)
            (begin
	    )
            true
        )

        (map-set TraderState tx-sender TRADER_STATE_CONFIRMED)
        (unwrap-panic (update-swap-state))
        (ok true)
    )
)

(define-private (release-escrow) 
    (begin
	(unwrap-panic (as-contract (contract-call? 'SP3252T1HMQHZTA9S22WZ2HZMKC4CVH965SHSERTH.the-guests-woymuls transfer u50 tx-sender TRADER-1)))
	(unwrap-panic (as-contract (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.phases-of-satoshi transfer u114 tx-sender TRADER-14)))
	(unwrap-panic (as-contract (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.virtual-tulips transfer u192 tx-sender TRADER-14)))
	(unwrap-panic (as-contract (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.byzantion-stacks-parrots transfer u38 tx-sender TRADER-22)))
	(unwrap-panic (as-contract (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.venice-visuals transfer u25 tx-sender TRADER-34)))
	(unwrap-panic (as-contract (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.venice-visuals transfer u46 tx-sender TRADER-34)))
	(unwrap-panic (as-contract (contract-call? 'SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173.the-explorer-guild transfer u78 tx-sender TRADER-39)))
	(unwrap-panic (as-contract (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.stacks-parrots-3d transfer u6 tx-sender TRADER-39)))
	(unwrap-panic (as-contract (contract-call? 'SP22GHCK5W3EK5VC1PEB3VVCBVFRQNTQXV9306QA0.bear-market-buddies transfer u647 tx-sender TRADER-40)))
	(unwrap-panic (as-contract (contract-call? 'SPNWZ5V2TPWGQGVDR6T7B6RQ4XMGZ4PXTEE0VQ0S.guests-hosted-stacks-parrots transfer u58 tx-sender TRADER-40)))


        (var-set swapState SWAP_STATE_FINALIZED)
        (ok true)
    )
)

(define-private (return-escrow) 
    (begin 
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-1)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-2)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-3)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-4)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-5)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-6)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-7)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-8)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-9)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-10)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-11)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-12)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-13)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-14)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-15)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-16)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-17)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-18)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-19)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-20)) TRADER_STATE_CONFIRMED)
            (begin
		(unwrap-panic (as-contract (contract-call? 'SP2X0TZ59D5SZ8ACQ6YMCHHNR2ZN51Z32E2CJ173.the-explorer-guild transfer u78 tx-sender TRADER-20)))
		(unwrap-panic (as-contract (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.venice-visuals transfer u46 tx-sender TRADER-20)))
		(unwrap-panic (as-contract (contract-call? 'SP22GHCK5W3EK5VC1PEB3VVCBVFRQNTQXV9306QA0.bear-market-buddies transfer u647 tx-sender TRADER-20)))
		(unwrap-panic (as-contract (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.stacks-parrots-3d transfer u6 tx-sender TRADER-20)))
		(unwrap-panic (as-contract (contract-call? 'SP2KAF9RF86PVX3NEE27DFV1CQX0T4WGR41X3S45C.byzantion-stacks-parrots transfer u38 tx-sender TRADER-20)))
		(unwrap-panic (as-contract (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.phases-of-satoshi transfer u114 tx-sender TRADER-20)))
		(unwrap-panic (as-contract (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.virtual-tulips transfer u192 tx-sender TRADER-20)))
		(unwrap-panic (as-contract (contract-call? 'SPNWZ5V2TPWGQGVDR6T7B6RQ4XMGZ4PXTEE0VQ0S.guests-hosted-stacks-parrots transfer u58 tx-sender TRADER-20)))
		(unwrap-panic (as-contract (contract-call? 'SP3252T1HMQHZTA9S22WZ2HZMKC4CVH965SHSERTH.the-guests-woymuls transfer u50 tx-sender TRADER-20)))
		(unwrap-panic (as-contract (contract-call? 'SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.venice-visuals transfer u25 tx-sender TRADER-20)))
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-21)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-22)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-23)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-24)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-25)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-26)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-27)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-28)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-29)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-30)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-31)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-32)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-33)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-34)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-35)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-36)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-37)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-38)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-39)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-40)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-41)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-42)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-43)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-44)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-45)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-46)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-47)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-48)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-49)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-50)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )
	(if (is-eq (default-to ERR_IS_NOT_TRADER (map-get? TraderState TRADER-51)) TRADER_STATE_CONFIRMED)
            (begin
	    )
            true
        )

        (map-set TraderState tx-sender TRADER_STATE_CANCELED)
        (var-set swapState SWAP_STATE_CANCELED)
        (ok true)
    )
)

(define-private (update-swap-state) 
    (let 
        ((cfCount (+ (var-get confirmCount) u1)))

        (var-set confirmCount cfCount)
        (if 
            (is-eq cfCount NUM_TRADERS)
            (var-set swapState SWAP_STATE_READY_TO_FINALIZE)
            true
        )
        (ok true)
    )
)

;; public functions
(define-read-only (get-swap-state) 
  (ok (var-get swapState))
)

(define-read-only (get-trader-state (trader principal)) 
  (unwrap! (map-get? TraderState trader) ERR_IS_NOT_TRADER)
)

(define-public (confirm-and-escrow) 
    (let 
        ((trState (unwrap! (map-get? TraderState tx-sender) (err ERR_IS_NOT_TRADER))))

        (asserts! (not (is-eq (var-get swapState) SWAP_STATE_FINALIZED)) (err ERR_SWAP_FINALIZED))
        (asserts! (not (is-eq (var-get swapState) SWAP_STATE_CANCELED)) (err ERR_SWAP_CANCELED))
        (asserts! (not (is-eq trState TRADER_STATE_CONFIRMED)) (err ERR_CALLER_ALREADY_ESCROWED))
        (try! (deposit-escrow))
        (ok true)
    )
)

(define-public (cancel) 
    (begin
        (unwrap! (map-get? TraderState tx-sender) (err ERR_IS_NOT_TRADER))
        (asserts! (not (is-eq (var-get swapState) SWAP_STATE_FINALIZED)) (err ERR_SWAP_FINALIZED))
        (asserts! (not (is-eq (var-get swapState) SWAP_STATE_CANCELED)) (err ERR_SWAP_CANCELED))
        (unwrap! (return-escrow) (err ERR_RELEASING_ESCROW_FAILED))
        (ok true)
    )
)

(define-public (finalize) 
    (begin
        (asserts! (not (is-eq (var-get swapState) SWAP_STATE_ACTIVE)) (err ERR_ESCROW_NOT_FILLED))
        (asserts! (not (is-eq (var-get swapState) SWAP_STATE_FINALIZED)) (err ERR_SWAP_FINALIZED))
        (asserts! (not (is-eq (var-get swapState) SWAP_STATE_CANCELED)) (err ERR_SWAP_CANCELED))
        (unwrap! (release-escrow) (err ERR_RELEASING_ESCROW_FAILED))
        (ok true)
    )
)
