| 1 | [ |
| 2 | "autonomous agent", |
| 3 | { |
| 4 | "getters": "{ |
| 5 | |
| 6 | $locked_reward_share = 0.02; |
| 7 | $liquid_reward_share = 0.002; |
| 8 | |
| 9 | $deposit_asset_reducer = 0.5; |
| 10 | $bytes_reducer = 0.75; |
| 11 | |
| 12 | $new_user_reward = 10e9; |
| 13 | $referral_reward = 10e9; |
| 14 | |
| 15 | $balance_cap = 200e9; |
| 16 | |
| 17 | $in_friend_price = 2e9; |
| 18 | $min_deposit_today_for_infriends = 10e9; |
| 19 | |
| 20 | |
| 21 | $year = 31536000; |
| 22 | |
| 23 | |
| 24 | $get_period = ($days_old, $fu_addition_days) => { |
| 25 | $res = {period: 1}; |
| 26 | foreach($fu_addition_days, 7, ($index, $day) => { |
| 27 | if ($days_old > +$day) |
| 28 | $res.period = $index + 2; |
| 29 | }); |
| 30 | $res.period |
| 31 | }; |
| 32 | |
| 33 | $is_eligible_for_infriends = ($address, $user, $ceiling_price, $followup_reward_days, $received_amount) => { |
| 34 | $days_old = floor((timestamp - parse_date($user.reg_date))/24/3600); |
| 35 | $fu_addition_days = keys($followup_reward_days); |
| 36 | $period = $get_period($days_old, $fu_addition_days); |
| 37 | $days_old % $period == 0 |
| 38 | OR $in_friend_price AND $received_amount >= $in_friend_price |
| 39 | OR $user.last_deposit_date == timestamp_to_string(timestamp, 'date') AND $user.last_day_deposits >= $min_deposit_today_for_infriends |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | $are_eligible = ($address1, $address2, $user1, $user2, $ceiling_price, $followup_reward_days, $received_amount) => { |
| 44 | $bNewUser = !$user1.last_date OR !$user2.last_date; |
| 45 | { |
| 46 | user1_eligible: $bNewUser OR $is_eligible_for_infriends($address1, $user1, $ceiling_price, $followup_reward_days, $received_amount), |
| 47 | user2_eligible: $bNewUser OR $is_eligible_for_infriends($address2, $user2, $ceiling_price, $followup_reward_days, $in_friend_price), |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | $get_deposit_asset_exchange_rate = ($friends_aa, $asset) => { |
| 52 | $aa = var[$friends_aa]['deposit_asset_'||$asset]; |
| 53 | require($aa, "unknown deposit asset"); |
| 54 | $params = definition[$aa][1].params; |
| 55 | $bX = $params.x_asset == $asset AND $params.y_asset == 'base'; |
| 56 | $bY = $params.x_asset == 'base' AND $params.y_asset == $asset; |
| 57 | require($bX OR $bY, "deposit asset must be one of the pool's assets and the other asset must be GBYTE"); |
| 58 | $recent = var[$aa]['recent']; |
| 59 | require($recent AND $recent.current AND $recent.prev, "no recent state of the pool"); |
| 60 | $pmax = max($recent.current.pmax, $recent.prev.pmax); |
| 61 | $pmin = min($recent.current.pmin, $recent.prev.pmin); |
| 62 | require($pmin > 0 AND $pmax > 0, "pmin and pmax must be > 0"); |
| 63 | $bX ? $pmin : 1/$pmax |
| 64 | }; |
| 65 | |
| 66 | $get_total_balance = ($balances, $ceiling_price, $friends_aa) => { |
| 67 | $totals = {in_bytes: $balances.base * $bytes_reducer}; |
| 68 | $asset_balances = filter($balances, 5, ($a, $balance) => $a != 'base' AND $a != 'frd'); |
| 69 | foreach($asset_balances, 3, ($a, $balance) => { |
| 70 | $totals.in_bytes = $totals.in_bytes + $balance * $get_deposit_asset_exchange_rate($friends_aa, $a) * $deposit_asset_reducer; |
| 71 | }); |
| 72 | $balances.frd + $totals.in_bytes / $ceiling_price |
| 73 | }; |
| 74 | |
| 75 | $get_rewards = ($address1, $address2, $user1, $user2, $user1_ref, $user2_ref, $ceiling_price, $friends_aa, $bFollowup) => { |
| 76 | $total_balance1 = $get_total_balance($user1.balances, $ceiling_price, $friends_aa); |
| 77 | $total_balance2 = $get_total_balance($user2.balances, $ceiling_price, $friends_aa); |
| 78 | $bNewUser = !$user1.last_date OR !$user2.last_date; |
| 79 | $bNewUserOrFromNewUser = $bNewUser OR $bFollowup AND ($user1.first_friend == $address2 OR $user2.first_friend == $address1); |
| 80 | $capped_total_balance1 = $bNewUserOrFromNewUser ? $total_balance1 : min($total_balance1, $balance_cap); |
| 81 | $capped_total_balance2 = $bNewUserOrFromNewUser ? $total_balance2 : min($total_balance2, $balance_cap); |
| 82 | $rewards = { |
| 83 | user1: { |
| 84 | locked: floor($capped_total_balance1 * $locked_reward_share), |
| 85 | liquid: floor($capped_total_balance1 * $liquid_reward_share), |
| 86 | }, |
| 87 | user2: { |
| 88 | locked: floor($capped_total_balance2 * $locked_reward_share), |
| 89 | liquid: floor($capped_total_balance2 * $liquid_reward_share), |
| 90 | }, |
| 91 | }; |
| 92 | |
| 93 | if (!$user1.last_date) |
| 94 | $rewards.user1.is_new = true; |
| 95 | if (!$user2.last_date) |
| 96 | $rewards.user2.is_new = true; |
| 97 | |
| 98 | if ($bNewUser){ |
| 99 | $capped_new_user_reward = floor(min($new_user_reward, $total_balance1, $total_balance2)); |
| 100 | $rewards.user1.locked = $rewards.user1.locked + $capped_new_user_reward; |
| 101 | $rewards.user1.new_user_reward = $capped_new_user_reward; |
| 102 | $rewards.user2.locked = $rewards.user2.locked + $capped_new_user_reward; |
| 103 | $rewards.user2.new_user_reward = $capped_new_user_reward; |
| 104 | } |
| 105 | |
| 106 | $min_unlock_date = timestamp_to_string(timestamp + $year, 'date'); |
| 107 | if (!$user1.last_date AND $user1.ref AND $user1_ref.unlock_date >= $min_unlock_date){ |
| 108 | $capped_referral_reward1 = min($referral_reward, floor($total_balance1)); |
| 109 | $rewards.user1.referred_user_reward = $capped_referral_reward1; |
| 110 | $rewards.user1.locked = $rewards.user1.locked + $capped_referral_reward1; |
| 111 | |
| 112 | $rewards.referrers[$user1.ref] = $capped_referral_reward1; |
| 113 | } |
| 114 | if (!$user2.last_date AND $user2.ref AND $user2_ref.unlock_date >= $min_unlock_date){ |
| 115 | $capped_referral_reward2 = min($referral_reward, floor($total_balance2)); |
| 116 | $rewards.user2.referred_user_reward = $capped_referral_reward2; |
| 117 | $rewards.user2.locked = $rewards.user2.locked + $capped_referral_reward2; |
| 118 | |
| 119 | $rewards.referrers[$user2.ref] = $capped_referral_reward2 + $rewards.referrers[$user2.ref]; |
| 120 | } |
| 121 | |
| 122 | $rewards |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | }", |
| 127 | "messages": [ |
| 128 | { |
| 129 | "app": "state", |
| 130 | "state": "{ |
| 131 | $get_rewards(); |
| 132 | bounce("library only"); |
| 133 | }" |
| 134 | } |
| 135 | ] |
| 136 | } |
| 137 | ] |