| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "bounce_fees": { |
| 5 | "base": 10000 |
| 6 | }, |
| 7 | "init": "{ |
| 8 | |
| 9 | $this = { |
| 10 | name: "swapbook", |
| 11 | use_case_1: "Declare your intention by locking money", |
| 12 | dust_collector:"O7NYCFUL5XIJTYE3O4MKGMGMTN6ATQAJ", |
| 13 | default_duration : 10*24*60*60, |
| 14 | number_of_offers : json_parse(var["number of offers"] otherwise 0), |
| 15 | htlc_creator_aa : "PVMCXUZBEHCFWOLXUDQVNCQZ476LNEW4", |
| 16 | }; |
| 17 | }", |
| 18 | "messages": { |
| 19 | "cases": [ |
| 20 | { |
| 21 | "if": "{ !!trigger.data.accept_offer }", |
| 22 | "init": "{ |
| 23 | $hash = trigger.data.accept_offer; |
| 24 | if (length($hash) != 44) bounce ("initiation hash must be 44 bytes long"); |
| 25 | |
| 26 | $offer = json_parse(var[$hash] otherwise bounce ("unknown hash/offer")); |
| 27 | |
| 28 | if (timestamp > $offer.deadline) bounce ("Too late"); |
| 29 | }", |
| 30 | "messages": [ |
| 31 | { |
| 32 | "app": "data", |
| 33 | "payload": { |
| 34 | "owner": "{$offer.owner}", |
| 35 | "destination_address": "{trigger.data.accept_with_destination_address_optional otherwise trigger.initial_address}", |
| 36 | "hash_of_secret": "{$offer.secret_hash}", |
| 37 | "amount": "{$offer.swap_amount}", |
| 38 | "asset_id": "{$offer.swap_asset}", |
| 39 | "deadline": "{$offer.deadline}" |
| 40 | } |
| 41 | }, |
| 42 | { |
| 43 | "if": "{ $offer.swap_asset != "base" }", |
| 44 | "app": "payment", |
| 45 | "payload": { |
| 46 | "asset": "{$offer.reward_asset}", |
| 47 | "outputs": [ |
| 48 | { |
| 49 | "address": "{ $this.htlc_creator_aa }", |
| 50 | "amount": "{ $offer.swap_amount }" |
| 51 | } |
| 52 | ] |
| 53 | } |
| 54 | }, |
| 55 | { |
| 56 | "app": "payment", |
| 57 | "payload": { |
| 58 | "asset": "base", |
| 59 | "outputs": [ |
| 60 | { |
| 61 | "address": "{ $offer.owner }", |
| 62 | "amount": "{ 1 }" |
| 63 | }, |
| 64 | { |
| 65 | "if": "{ $offer.swap_asset == "base" }", |
| 66 | "address": "{ $this.htlc_creator_aa }", |
| 67 | "amount": "{ $offer.swap_amount }" |
| 68 | } |
| 69 | ] |
| 70 | } |
| 71 | }, |
| 72 | { |
| 73 | "app": "state", |
| 74 | "state": "{ |
| 75 | var[$hash] = false; |
| 76 | var["number of offers"] = $this.number_of_offers -1; |
| 77 | response['message'] = "swap initiated"; |
| 78 | }" |
| 79 | } |
| 80 | ] |
| 81 | }, |
| 82 | { |
| 83 | "if": "{ !!trigger.data.cancel_offer }", |
| 84 | "init": "{ |
| 85 | $hash = trigger.data.cancel_offer; |
| 86 | if (length($hash) != 44) bounce ("cancel hash must be 44 bytes long, and finish with '='"); |
| 87 | |
| 88 | $offer = json_parse(var[$hash] otherwise bounce ("unknown hash/offer")); |
| 89 | if (trigger.initial_address != $offer.owner) bounce ("You are not the owner"); |
| 90 | }", |
| 91 | "messages": [ |
| 92 | { |
| 93 | "if": "{ $offer.swap_asset != "base" }", |
| 94 | "app": "payment", |
| 95 | "payload": { |
| 96 | "asset": "{$offer.reward_asset}", |
| 97 | "outputs": [ |
| 98 | { |
| 99 | "address": "{ $offer.owner }", |
| 100 | "amount": "{ $offer.swap_amount }" |
| 101 | } |
| 102 | ] |
| 103 | } |
| 104 | }, |
| 105 | { |
| 106 | "app": "payment", |
| 107 | "payload": { |
| 108 | "asset": "base", |
| 109 | "outputs": [ |
| 110 | { |
| 111 | "if": "{ $offer.swap_asset == "base" }", |
| 112 | "address": "{ $offer.owner }", |
| 113 | "amount": "{ $offer.swap_amount }" |
| 114 | } |
| 115 | ] |
| 116 | } |
| 117 | }, |
| 118 | { |
| 119 | "app": "state", |
| 120 | "state": "{ |
| 121 | var[$hash] = false; |
| 122 | var["number of offers"] = $this.number_of_offers -1; |
| 123 | response['message'] = "offer cancelled"; |
| 124 | }" |
| 125 | } |
| 126 | ] |
| 127 | }, |
| 128 | { |
| 129 | "if": "{ $this.number_of_offers < 1 and trigger.address == $this.dust_collector }", |
| 130 | "messages": [ |
| 131 | { |
| 132 | "app": "payment", |
| 133 | "payload": { |
| 134 | "asset": "base", |
| 135 | "outputs": [ |
| 136 | { |
| 137 | "address": "{ $this.owner }" |
| 138 | } |
| 139 | ] |
| 140 | } |
| 141 | } |
| 142 | ] |
| 143 | }, |
| 144 | { |
| 145 | "if": "{ !!trigger.data.offer_secret_hash }", |
| 146 | "init": "{ |
| 147 | $hash = trigger.data.offer_secret_hash; |
| 148 | if (length($hash) != 44) bounce ("hash must be 44 bytes long and finish with '='"); |
| 149 | if (!!var[$hash]) bounce ("this secret is already in use"); |
| 150 | }", |
| 151 | "messages": [ |
| 152 | { |
| 153 | "app": "state", |
| 154 | "state": "{ |
| 155 | $offer = { |
| 156 | owner: trigger.data.offer_owner otherwise trigger.address, |
| 157 | deadline: trigger.data.offer_deadline_optional otherwise timestamp + $this.default_duration, |
| 158 | secret_hash: $hash, |
| 159 | swap_amount: trigger.output[[asset!=base]].amount otherwise trigger.output[[asset=base]].amount-2000, |
| 160 | swap_asset: trigger.output[[asset!=base]].asset != "none"? trigger.output[[asset!=base]].asset: "base", |
| 161 | htlc_id_on_other_chain: trigger.data.offer_htlc_id_on_other_chain, |
| 162 | }; |
| 163 | var[$hash] = json_stringify($offer); |
| 164 | var["number of offers"] = $this.number_of_offers + 1; |
| 165 | response['message'] = "Offer recorded"; |
| 166 | }" |
| 167 | } |
| 168 | ] |
| 169 | } |
| 170 | ] |
| 171 | } |
| 172 | } |
| 173 | ] |