| 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_htlc_duration : 2*24*60*60, |
| 14 | default_duration : 10*24*60*60, |
| 15 | number_of_offers : json_parse(var["number of offers"] otherwise 0), |
| 16 | htlc_creator_aa : "GALNCFV3O34RAEMI3YCKNCGQKJWDZ6L5", |
| 17 | }; |
| 18 | }", |
| 19 | "messages": { |
| 20 | "cases": [ |
| 21 | { |
| 22 | "if": "{ !!trigger.data.accept_offer }", |
| 23 | "init": "{ |
| 24 | $hash = trigger.data.accept_offer; |
| 25 | if (length($hash) != 44) bounce ("initiation hash must be 44 bytes long"); |
| 26 | |
| 27 | $offer = json_parse(var[$hash] otherwise bounce ("unknown hash/offer")); |
| 28 | $destination_address = trigger.data.accept_with_destination_address_optional otherwise trigger.initial_address; |
| 29 | if (timestamp > $offer.deadline) bounce ("Too late"); |
| 30 | |
| 31 | |
| 32 | $aa_definition = ['autonomous agent', { |
| 33 | base_aa: $this.htlc_base_aa , |
| 34 | params: { |
| 35 | destination_address: $destination_address, |
| 36 | hash_of_secret: $offer.secret_hash, |
| 37 | amount: $offer.swap_amount, |
| 38 | asset_id: $offer.swap_asset, |
| 39 | duration: $offer.htlc_duration, |
| 40 | } |
| 41 | } |
| 42 | ]; |
| 43 | }", |
| 44 | "messages": [ |
| 45 | { |
| 46 | "app": "data", |
| 47 | "payload": { |
| 48 | "owner": "{$offer.owner}", |
| 49 | "destination_address": "{$destination_address}", |
| 50 | "hash_of_secret": "{$offer.secret_hash}", |
| 51 | "amount": "{$offer.swap_amount}", |
| 52 | "asset_id": "{$offer.swap_asset}", |
| 53 | "duration": "{$offer.duration}" |
| 54 | } |
| 55 | }, |
| 56 | { |
| 57 | "if": "{ $offer.swap_asset != "base" }", |
| 58 | "app": "payment", |
| 59 | "payload": { |
| 60 | "asset": "{$offer.reward_asset}", |
| 61 | "outputs": [ |
| 62 | { |
| 63 | "address": "{ $this.htlc_creator_aa }", |
| 64 | "amount": "{ $offer.swap_amount }" |
| 65 | } |
| 66 | ] |
| 67 | } |
| 68 | }, |
| 69 | { |
| 70 | "app": "payment", |
| 71 | "payload": { |
| 72 | "asset": "base", |
| 73 | "outputs": [ |
| 74 | { |
| 75 | "address": "{ $offer.owner }", |
| 76 | "amount": "{ 1 }" |
| 77 | }, |
| 78 | { |
| 79 | "if": "{ $offer.swap_asset == "base" }", |
| 80 | "address": "{ $this.htlc_creator_aa }", |
| 81 | "amount": "{ $offer.swap_amount }" |
| 82 | } |
| 83 | ] |
| 84 | } |
| 85 | }, |
| 86 | { |
| 87 | "app": "state", |
| 88 | "state": "{ |
| 89 | var[$hash] = false; |
| 90 | var["number of offers"] = $this.number_of_offers -1; |
| 91 | response['message'] = "swap initiated"; |
| 92 | response['HTLC_address'] = chash160($aa_definition); |
| 93 | }" |
| 94 | } |
| 95 | ] |
| 96 | }, |
| 97 | { |
| 98 | "if": "{ !!trigger.data.cancel_offer }", |
| 99 | "init": "{ |
| 100 | $hash = trigger.data.cancel_offer; |
| 101 | if (length($hash) != 44) bounce ("cancel hash must be 44 bytes long, and finish with '='"); |
| 102 | |
| 103 | $offer = json_parse(var[$hash] otherwise bounce ("unknown hash/offer")); |
| 104 | if (trigger.initial_address != $offer.owner) bounce ("You are not the owner"); |
| 105 | }", |
| 106 | "messages": [ |
| 107 | { |
| 108 | "if": "{ $offer.swap_asset != "base" }", |
| 109 | "app": "payment", |
| 110 | "payload": { |
| 111 | "asset": "{$offer.reward_asset}", |
| 112 | "outputs": [ |
| 113 | { |
| 114 | "address": "{ $offer.owner }", |
| 115 | "amount": "{ $offer.swap_amount }" |
| 116 | } |
| 117 | ] |
| 118 | } |
| 119 | }, |
| 120 | { |
| 121 | "app": "payment", |
| 122 | "payload": { |
| 123 | "asset": "base", |
| 124 | "outputs": [ |
| 125 | { |
| 126 | "if": "{ $offer.swap_asset == "base" }", |
| 127 | "address": "{ $offer.owner }", |
| 128 | "amount": "{ $offer.swap_amount }" |
| 129 | } |
| 130 | ] |
| 131 | } |
| 132 | }, |
| 133 | { |
| 134 | "app": "state", |
| 135 | "state": "{ |
| 136 | var[$hash] = false; |
| 137 | var["number of offers"] = $this.number_of_offers -1; |
| 138 | response['message'] = "offer cancelled"; |
| 139 | }" |
| 140 | } |
| 141 | ] |
| 142 | }, |
| 143 | { |
| 144 | "if": "{ $this.number_of_offers < 1 and trigger.address == $this.dust_collector }", |
| 145 | "messages": [ |
| 146 | { |
| 147 | "app": "payment", |
| 148 | "payload": { |
| 149 | "asset": "base", |
| 150 | "outputs": [ |
| 151 | { |
| 152 | "address": "{ $this.owner }" |
| 153 | } |
| 154 | ] |
| 155 | } |
| 156 | } |
| 157 | ] |
| 158 | }, |
| 159 | { |
| 160 | "if": "{ !!trigger.data.offer_secret_hash }", |
| 161 | "init": "{ |
| 162 | $hash = trigger.data.offer_secret_hash; |
| 163 | if (length($hash) != 44) bounce ("hash must be 44 bytes long and finish with '='"); |
| 164 | if (!!var[$hash]) bounce ("this secret is already in use"); |
| 165 | }", |
| 166 | "messages": [ |
| 167 | { |
| 168 | "app": "state", |
| 169 | "state": "{ |
| 170 | $offer = { |
| 171 | owner: trigger.data.offer_owner otherwise trigger.address, |
| 172 | htlc_duration: trigger.data.offer_htlc_duration_optional otherwise $this.default_htlc_duration, |
| 173 | secret_hash: $hash, |
| 174 | swap_amount: trigger.output[[asset!=base]].amount otherwise trigger.output[[asset=base]].amount-2000, |
| 175 | swap_asset: trigger.output[[asset!=base]].asset != "none"? trigger.output[[asset!=base]].asset: "base", |
| 176 | deadline: timestamp + (trigger.data.offer_duration_optional otherwise $this.default_duration), |
| 177 | }; |
| 178 | var[$hash] = json_stringify($offer); |
| 179 | var["number of offers"] = $this.number_of_offers + 1; |
| 180 | response['message'] = "Offer recorded"; |
| 181 | }" |
| 182 | } |
| 183 | ] |
| 184 | }, |
| 185 | { |
| 186 | "messages": [ |
| 187 | { |
| 188 | "app": "state", |
| 189 | "state": "{ |
| 190 | response['message'] = "No case true"; |
| 191 | }" |
| 192 | } |
| 193 | ] |
| 194 | } |
| 195 | ] |
| 196 | } |
| 197 | } |
| 198 | ] |