Definition: [
"autonomous agent",
{
"messages": {
"cases": [
{
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$addr}",
"amount": "{$withdraw_asset ? $base_inp : ($withdraw_amount + $base_inp)}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$asset_ct}",
"outputs": [
{
"address": "{$addr}",
"amount": "{$withdraw_amount}"
}
]
},
"if": "{$withdraw_asset}"
},
{
"app": "state",
"state": "{var[$w_total_key] -= $withdraw_amount;if(var[$w_total_key] == 0) var[$w_total_key] = false;var[$withdraw_key] -= $withdraw_amount;if(var[$withdraw_key] == 0) var[$withdraw_key] = false;var[$key] -= $withdraw_asset ? ($withdraw_amount * $ratio) : ($withdraw_amount / $ratio);if(var[$key] == 0) var[$key] = false;response['message'] = 'withdrawn exchanged amount ('||($withdraw_asset ? 'assets': 'bytes')||') at '||$ratio||' bytes per token';}"
}
],
"if": "{$withdraw_asset = var[$exch_asset_key] and var[$base_key] and var[$exch_asset_key] > 0 and var[$base_key] > 0;$withdraw_base = var[$exch_key] and var[$asset_key] and var[$exch_key] > 0 and var[$asset_key] > 0;$withdraw_asset or $withdraw_base}",
"init": "{if($asset) bounce('You must withdraw, send byte only transaction please');$withdraw_key = $withdraw_asset ? $exch_asset_key : $exch_key;$w_total_key = $withdraw_asset ? $total_asset_key : $total_key;$max_to_withdraw = $withdraw_asset ? (var[$base_key] / $ratio) : (var[$asset_key] * $ratio);$key = $withdraw_asset ? $base_key : $asset_key;$withdraw_amount = (var[$withdraw_key] <= $max_to_withdraw) ? var[$withdraw_key] : $max_to_withdraw;}"
},
{
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$addr}",
"amount": "{$exchange_asset ? ($byte_amount + $base_inp) : $byte_amount}"
}
]
},
"if": "{$exchange_asset or $byte_amount > 0}"
},
{
"app": "payment",
"payload": {
"asset": "{$asset_ct}",
"outputs": [
{
"address": "{$addr}",
"amount": "{$asset_amount}"
}
]
},
"if": "{$exchange_base or $asset_amount > 0}"
},
{
"app": "state",
"state": "{var['_ Last price'] = $ratio;if($exchange_asset){var[$total_asset_key] += $asset_inp - $asset_amount;var[$exch_asset_key] += $asset_inp - $asset_amount;var[$total_key] -= $byte_amount;if(var[$total_key] == 0) var[$total_key] = false;response['message'] = 'sold '||($asset_inp - $asset_amount)||' assets for '||($byte_amount)||' bytes';}else{var[$total_key] += $base_inp_round - $byte_amount;var[$exch_key] += $base_inp_round - $byte_amount;var[$total_asset_key] -= $asset_amount;if(var[$total_asset_key] == 0) var[$total_asset_key] = false;response['message'] = 'bought '||($asset_amount)||' assets for '||($base_inp_round - $byte_amount)||' bytes';}}"
}
],
"if": "{$exchange_asset = $asset and $max_b_to_exchange > 0;$exchange_base = !$asset and $max_a_to_exchange > 0;!trigger.data.cancel and ($exchange_asset or $exchange_base)}",
"init": "{$max_to_ex = $asset ? $max_b_to_exchange : $max_a_to_exchange;$inp_in_other = $asset ? $asset_inp*$ratio : $base_inp_round/$ratio;if($inp_in_other > $max_to_ex){$byte_amount = $asset ? $max_to_ex : (($inp_in_other - $max_to_ex) * $ratio);$asset_amount = $asset ? ((($inp_in_other - $max_to_ex) - (($inp_in_other - $max_to_ex) % $ratio)) / $ratio) : $max_to_ex;}else{if($inp_in_other == 0) bounce('Not enough funds for minimum');$byte_amount = $asset ? $inp_in_other : 0;$asset_amount = $asset ? 0 : $inp_in_other;}}"
},
{
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$addr}",
"amount": "{$cancel_asset ? $base_inp : ($cancel_amount + $base_inp)}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "{$asset_ct}",
"outputs": [
{
"address": "{$addr}",
"amount": "{$cancel_amount}"
}
]
},
"if": "{$cancel_asset}"
},
{
"app": "state",
"state": "{var[$cancel_tot_key] -= $cancel_amount;if(var[$cancel_tot_key] == 0)var[$cancel_tot_key] = false;var[$key] -= $cancel_amount;if(var[$key] == 0)var[$key] = false;response['message'] = 'canceled order ('||($cancel_asset?'sell':'buy')||' at '||$ratio||' bytes per token)';}"
}
],
"if": "{$cancel_asset = var[$asset_key] and var[$asset_key] > 0;$cancel_base = var[$base_key] and var[$base_key] > 0;trigger.data.cancel and ($cancel_asset or $cancel_base)}",
"init": "{$max_to_cancel = $cancel_asset ? $max_a_to_exchange : $max_b_to_exchange;$key = $cancel_asset ? $asset_key : $base_key;$cancel_tot_key = $cancel_asset ? $total_asset_key : $total_key;if($max_to_cancel <= 0) bounce('Not allowed');$cancel_amount = ($max_to_cancel <= var[$key]) ? $max_to_cancel : var[$key];}"
},
{
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$addr}",
"amount": "{$base_inp}"
}
]
},
"if": "{$asset}"
},
{
"app": "state",
"state": "{if($asset){var[$total_asset_key] += $asset_inp;var[$asset_key] += $asset_inp;response['message'] = 'deposited '||$asset_inp||' assets to sell at '||$ratio||' bytes per token';}else{var[$total_key] += $base_inp_round;var[$base_key] += $base_inp_round;response['message'] = 'deposited '||$base_inp_round||' bytes to buy at '||$ratio||' bytes per token';}}"
}
]
}
]
},
"init": "{$asset_ct = 'U0rLnB4lXupOc1u1woZzzrr0xscmOc50wvrpXAMWq3Q=';if(!var['_ Last price'] and !trigger.data.price) bounce('Price must be defined before first exchange');if(trigger.data.price){$ratio = round(trigger.data.price);if($ratio <= 0) bounce('Price not allowed');}else $ratio = var['_ Last price'];if(trigger.output[[asset!=base]].asset != 'none')$asset = trigger.output[[asset!=base]].asset;if($asset){if($asset == 'ambiguous') bounce('Only 1 asset per tx');if($asset != $asset_ct) bounce('Asset not allowed, please send ' || $asset_ct);$asset_inp = (trigger.output[[asset!=base]] != 0) ? trigger.output[[asset!=base]] : bounce('Zero asset tx');}$addr = trigger.address;$base_inp = trigger.output[[asset=base]] - 700;$base_key = '_' || $addr || $ratio || 'b';$asset_key = '_' || $addr || $ratio;$total_key = 'b' || $ratio;$total_asset_key = 'a' || $ratio;$exch_key = 'b_' || $ratio ;$exch_asset_key = 'a_' || $ratio ;$max_b_to_exchange = var[$total_key] - var[$exch_key];$max_a_to_exchange = var[$total_asset_key] - var[$exch_asset_key];$base_inp_round = ($base_inp - ($base_inp % $ratio));}"
}
]