Definition: [
"autonomous agent",
{
"init": "{
$BOUNCER = 'V23L3YLZVOLQ473QVRGODFRIDSBDOSUJ';
$attestor = trigger.data.attestor and is_valid_address(trigger.data.attestor) ? trigger.data.attestor : false;
$field = trigger.data.attestor and typeof(trigger.data.field) == 'string' ? trigger.data.field : false;
if(trigger.output[[asset!=base]].asset != 'none')
$asset = trigger.output[[asset!=base]].asset;
}",
"messages": {
"cases": [
{
"if": "{var['l']}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{var['l']}"
}
]
}
},
{
"app": "state",
"state": "{
var['l'] = false;
}"
}
]
},
{
"if": "{var[trigger.address]}",
"init": "{
$asset_wanted = trigger.data.want;
if(!$asset_wanted) bounce('want param missing, specify asset you want in exchange for yours');
if(typeof($asset_wanted) != 'string') bounce('want param invalid, must be an asset');
if(!var[$asset_wanted]) bounce('wanted asset does not have a registered default AA proxy, please register sending any amount of '||$asset_wanted||' to '||this_address);
if(!trigger.data.price) bounce('Price to exchange missing (price param), must be set in assets wanted per asset offered');
$asset_offered = var[trigger.address];
if(!trigger.data.price OR trigger.data.price <= 0) bounce('Price invalid, must be set in assets wanted per asset offered');
$price = trigger.data.price;
$address = trigger.data.address;
$price_fill = 1/trigger.data.price;
$ord_pair = sha256(var[trigger.address]||'_'||$asset_wanted);
$fill_pair = sha256($asset_wanted||'_'||var[trigger.address]);
$ord_key = $ord_pair ||'_'||$price;
$ord_addr_key = $ord_pair||'_'||$price||'_'||$address;
$orders_to_fill = $fill_pair||'_'||$price_fill;
$to_fill = trigger.data.to;
$fill_key = $fill_pair ||'_'||$price_fill||'_'||$to_fill;
}",
"messages": {
"cases": [
{
"if": "{trigger.data.cancel}",
"init": "{
if(!var[$ord_addr_key]) bounce('You have no funds deposited on this pair');
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{trigger.address}",
"amount": 1
},
{
"address": "{$address}"
}
]
}
},
{
"app": "data",
"payload": {
"transfer": "{$address}",
"value": "{var[$ord_addr_key]}"
}
},
{
"app": "state",
"state": "{
var[$ord_key] -= 1;if(var[$ord_key] == 0)var[$ord_key]=false;
$am = var[$ord_addr_key];
var[$ord_addr_key] = false;
response['message'] = 'Canceled order to sell '||$am||' '||$asset_offered||' at '||$price||' '||$asset_wanted||' per unit';
}"
}
]
},
{
"init": "{
if(var[$orders_to_fill]){
if(!$to_fill OR !is_valid_address($to_fill)) bounce('to param not valid, must be an address. There are '||var[$orders_to_fill]||' orders who match, please fill them, to know address to fill check AA state vars around '||$fill_pair);
if(!var[$fill_key]) bounce('Address has no created order on this pair');
$a_amount = var[$fill_key];
if(!var[trigger.address]['a'||$address] OR var[trigger.address]['a'||$address] < ($a_amount*$price_fill)) bounce('You have not enough funds to fill the order, need '||($a_amount*$price_fill));
$b_amount = $a_amount*$price_fill;
}
else{
if(!trigger.data.value) bounce('Value param missing');
if(var[$ord_addr_key]) bounce('You already sent to exchange for this pair, please cancel order first sending cancel = 1 if you want to change the offer');
}
}",
"messages": [
{
"if": "{var[$orders_to_fill]}",
"app": "data",
"payload": {
"a": {
"from": "{this_address}",
"to": "{$address}",
"value": "{$a_amount}",
"addr": "{var[$asset_wanted]}"
},
"b": {
"from": "{$address}",
"to": "{$to_fill}",
"value": "{$b_amount}",
"addr": "{trigger.address}"
}
}
},
{
"if": "{var[$orders_to_fill]}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{var[$asset_wanted]}",
"amount": 1
},
{
"address": "{trigger.address}",
"amount": 1
},
{
"address": "{$to_fill}",
"amount": 1
},
{
"address": "{$address}"
}
]
}
},
{
"if": "{!var[$orders_to_fill]}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$address}",
"amount": "{trigger.output[[asset=base]]-700}"
}
]
}
},
{
"app": "state",
"state": "{
if(var[$orders_to_fill]){
var[$orders_to_fill] -= 1;
if(var[$orders_to_fill] == 0) var[$orders_to_fill]=false;
response['message'] = 'Exchanged '||$a_amount||' '||$asset_wanted||' at '||$price||' '||$asset_wanted||' per unit';
var[$fill_key] = false;
}
else{
var[$ord_key] += 1;
var[$ord_addr_key] = trigger.data.value;
response['message'] = 'Order created to sell '||trigger.data.value||' '||$asset_offered||' at '||$price||' '||$asset_wanted||' per unit';
}
}"
}
]
}
]
}
},
{
"messages": [
{
"app": "payment",
"payload": {
"asset": "{$asset}",
"outputs": [
{
"address": "{trigger.address}",
"amount": "{trigger.output[[asset!=base]]}"
}
]
}
},
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{$BOUNCER}",
"amount": "{trigger.output[[asset=base]]-8000}"
}
]
}
},
{
"app": "definition",
"payload": {
"definition": [
"autonomous agent",
{
"{'init'}": "{
"{$ASSET = '"||$asset||"';"
||($attestor?("$ATTESTOR_ADDR = '"||$attestor||"';"):"")
||"$PATHER_ADDR = '"||this_address||"';$BYTE_INP = 10000;$FREE_LIMIT = 2000;$ADDR = 'a';$PERM = 'p';$BYTE = 'b';$TOTAL = 'total';$base = trigger.output[[asset=base]];$trigger_cost = unit[trigger.unit].headers_commission + unit[trigger.unit].payload_commission;$addr = trigger.address;$balance = balance[base] - $base - storage_size;$free_mode = ($balance - var[$TOTAL]) > $FREE_LIMIT;$addr_key = $ADDR || $addr;$addr_q = var[$addr_key];$byte_key = $BYTE || $addr;$base_inp = $base + ($free_mode?$trigger_cost:0);$value = trigger.data.value;if(trigger.output[[asset!=base]].asset != 'none')$asset = trigger.output[[asset!=base]].asset;if($ATTESTOR_ADDR AND !attestation[[attestors=$ATTESTOR_ADDR, address=trigger.address,ifnone=false]]"
||($field?("['"||$field||"']"):"")||") bounce('Not allowed to use, please attest your address with '||$ATTESTOR_ADDR);if(!trigger.data.approve AND $value AND (typeof($value) != 'number' OR $value <= 0)) bounce('Value param invalid');}"
}",
"messages": {
"{'cases'}": [
{
"{'if'}": "{
"{$PATHER_ADDR == $addr AND !trigger.data.transfer}"
}",
"messages": [
{
"app": "state",
"{'state'}": "{
"{if(trigger.data.a.addr == this_address){$from = trigger.data.a.from;$to = trigger.data.a.to;$value = trigger.data.a.value;}else{if(trigger.data.b.addr == this_address){$from = trigger.data.b.from;$to = trigger.data.b.to;$value = trigger.data.b.value;}}var[$ADDR|| $to] += $value;var[$ADDR|| $from] -= $value;if(var[$ADDR|| $from] == 0) var[$ADDR|| $from] = false;}"
}"
}
]
},
{
"{'if'}": "{
"{trigger.data.approve AND $base == $BYTE_INP}"
}",
"{'init'}": "{
"{$approve = trigger.data.approve;$perm_key = $PERM || $addr || $approve;if(!is_valid_address($approve) OR $approve == $addr) bounce('approve param invalid, specify address to approve instead of any other value');if(!$value) bounce('Value param missing');if(typeof($value) != 'number' OR $value < 0) bounce('Value param invalid');}"
}",
"messages": [
{
"app": "state",
"{'state'}": "{
"{var[$perm_key] = $value;if(var[$perm_key] == 0) var[$perm_key] = false;var[$byte_key] += $base_inp;var[$TOTAL] += $base_inp;response['message'] = 'Approved '||$approve||' to spend '||$value ||' from your account';}"
}"
}
]
},
{
"{'if'}": "{
"{trigger.data.transfer AND ($base == $BYTE_INP OR $addr == $PATHER_ADDR)}"
}",
"{'init'}": "{
"{$to = trigger.data.transfer == 'exchange' ? $PATHER_ADDR : trigger.data.transfer;if(!is_valid_address($to) OR $to == $addr) bounce('transfer param invalid, specify address to instead of any other value');if($to != $PATHER_ADDR AND !$addr_q) bounce('No funds found');if($to != $PATHER_ADDR AND !$value) bounce('Value param missing');if($value AND $value > $addr_q) bounce('You have max '||$addr_q);}"
}",
"messages": [
{
"{'if'}": "{"{$to == $PATHER_ADDR}"}",
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{"{$PATHER_ADDR}"}",
"amount": "{"{$base-5000}"}"
}
]
}
},
{
"{'if'}": "{"{$to == $PATHER_ADDR}"}",
"app": "data",
"payload": {
"want": "{"{trigger.data.want}"}",
"price": "{"{trigger.data.price}"}",
"address": "{"{trigger.address}"}",
"value": "{"{$value?$value:''}"}",
"to": "{"{trigger.data.to?trigger.data.to:''}"}",
"cancel": "{"{trigger.data.cancel?1:''}"}"
}
},
{
"app": "state",
"{'state'}": "{
"{var[$ADDR|| $to] += $value;var[$addr_key] -= $value;$fee = unit[response_unit].headers_commission + unit[response_unit].payload_commission;if(var[$addr_key] == 0) var[$addr_key] = false;if($addr != $PATHER_ADDR){var[$byte_key] += ($to == $PATHER_ADDR?(($free_mode?$trigger_cost:0)+5000 - $fee): $base_inp );var[$TOTAL] += ($to == $PATHER_ADDR?(($free_mode?$trigger_cost:0)+5000 - $fee): $base_inp);}response['message'] = 'Transferred '||$value||' to '||trigger.data.transfer;}"
}"
}
]
},
{
"{'if'}": "{
"{trigger.data.withdraw_bytes AND $base == $BYTE_INP}"
}",
"{'init'}": "{
"{$user_bal = var[$byte_key] otherwise 0;$w_amount = $balance < $user_bal ? $balance : $user_bal;$wt = ($w_amount / $BYTE_INP) + (1-(($w_amount / $BYTE_INP)%1));$w_fee = $ATTESTOR_ADDR AND $free_mode ?0 : (($wt > 2) ? 462+$wt*60 : 522);if($user_bal == 0) bounce('No bytes to withdraw');}"
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "base",
"outputs": [
{
"address": "{"{$addr}"}",
"amount": "{"{$w_amount + $base - $w_fee}"}"
}
]
}
},
{
"app": "state",
"{'state'}": "{
"{var[$byte_key] = false;var[$TOTAL] -= $w_amount;response['message'] = 'Withdrawn '||$w_amount||' bytes less '||$w_fee||' fee from your account';}"
}"
}
]
},
{
"{'if'}": "{
"{$asset AND $base == $BYTE_INP}"
}",
"{'init'}": "{
"{$asset_inp = trigger.output[[asset!=base]];if($asset == 'ambiguous') bounce('Only 1 asset per tx');if($asset != $ASSET) bounce('You must deposit '||$ASSET);}"
}",
"messages": [
{
"app": "state",
"{'state'}": "{
"{var[$addr_key] += $asset_inp;var[$byte_key] += $base_inp;var[$TOTAL] += $base_inp;response['message'] = 'Added '||$asset_inp||' to your account';}"
}"
}
]
},
{
"{'if'}": "{
"{trigger.data.withdraw AND $base == $BYTE_INP}"
}",
"{'init'}": "{
"{$max_w = $addr_q-($addr_q % 1);$w_amount = $value - ($value % 1);if(!$value) bounce('Value param missing, max to withdraw '||$max_w);if(!$addr_q) bounce('Nothing to withdraw');if($w_amount <= 0) bounce('Max to withdraw '||$max_w);if($w_amount > $max_w) bounce('Not enough asset, max to withdraw '||$max_w);}"
}",
"messages": [
{
"app": "payment",
"payload": {
"asset": "{"{$ASSET}"}",
"outputs": [
{
"address": "{"{$addr}"}",
"amount": "{"{$w_amount}"}"
}
]
}
},
{
"app": "state",
"{'state'}": "{
"{if($ATTESTOR_ADDR AND $free_mode)$w_fee = 0;else$w_fee = unit[response_unit].headers_commission + unit[response_unit].payload_commission;var[$addr_key] -= $w_amount;if(var[$addr_key] == 0) var[$addr_key]=false;var[$byte_key] += ($base_inp-$w_fee);var[$TOTAL] += ($base_inp-$w_fee);response['message'] = 'Withdrawn '||$w_amount||' from your account';}"
}"
}
]
},
{
"{'if'}": "{
"{trigger.data.transfer_from AND $base == $BYTE_INP}"
}",
"{'init'}": "{
"{$from = trigger.data.transfer_from;$to = trigger.data.to;$from_key = $ADDR|| $from;$perm_key = $PERM || $from || $addr;if(!is_valid_address($from)) bounce('transfer_from param invalid, specify from address instead of any other value');if(!$to) bounce('To param missing');if(!is_valid_address($to)) bounce('To param invalid');if(!var[$from_key]) bounce('Account have no funds');if(!$value) bounce('Value param missing');if(!var[$perm_key]) bounce('Not allowed to spend');if(var[$perm_key] < $value) bounce('You can spend max '||var[$perm_key]);if(var[$from_key] < $value) bounce('Account have '||var[$from_key]);}"
}",
"messages": [
{
"app": "state",
"{'state'}": "{
"{var[$ADDR|| $to] += $value;var[$from_key] -= $value;var[$perm_key] -= $value;if(var[$from_key] == 0) var[$from_key] = false;if(var[$perm_key] == 0) var[$perm_key] = false;var[$byte_key] += $base_inp;var[$TOTAL] += $base_inp;response['message'] = 'Transferred '||$value||' to '||$to;}"
}"
}
]
},
{
"messages": [
{
"app": "state",
"{'state'}": "{
"{if($asset) bounce('Allowed asset: '||$ASSET);response['accepted_params'] = 'withdraw, transfer, transfer_from, approve, withdraw_bytes';response['usage'] = 'Send '||$BYTE_INP||' bytes and param or deposit '||$ASSET||' = 1 for continue.';response['message'] = 'Received donation for funding AA operation, thaanks!';}"
}"
}
]
}
]
}
}
]
}
},
{
"app": "state",
"state": "{
if(!$asset) bounce('You must send an asset');
if($asset == 'ambiguous') bounce('Only 1 asset per tx');
response['message']='AA for '||$asset||'created';
$definition_address = unit[response_unit].messages[[.app='definition']].payload.address;
response['address'] = $definition_address;
var[$definition_address] = $asset;
var['l'] = $definition_address;
if($attestor){
if($field){
response['security'] = $attestor||' will have to give permission and will be able to block accounts';
}
else{
response['security'] = $attestor||' will have to give permission but will not be able to block accounts, specify an attestation field using field param in other case';
}
}
else{
if(var[$asset]) bounce('Proxy AA already registered: '||var[$asset]);
var[$asset] = $definition_address;
response['security'] = 'Free access, nobody will be able to block anyone. Use here attestor param to restrict AA to certain addresses';
}
}"
}
]
}
]
}
}
]