Snippets
Created by
Riccardo Sibani
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import exchange;
import ft3_core: rell_modules.ft3.core;
import ft3_acc: rell_modules.ft3.account;
enum option_type {
PUT,
CALL
}
enum payable_type {
CASH,
PHYSICAL
}
entity validator {
pubkey;
}
struct option_ft3_asset {
collateralized_asset: ft3_core.asset;
option_type;
payable_type;
strike_price: integer;
against_asset: ft3_core.asset;
maturity_date: timestamp;
}
entity option {
collateralized_asset: ft3_core.asset;
strike_price: integer;
against_asset: ft3_core.asset;
type: option_type;
payable_type;
maturity_date: timestamp;
owner_account: ft3_acc.account;
index option_token: ft3_core.asset;
}
function add_validator(pubkey) { // should be multisign account
require((validator@*{}).size() ==0);
create validator(pubkey);
}
function register_asset(name) {
require(is_signer(validator@{}.pubkey));
ft3_core.register_asset(name, "ETHEREUM".to_bytes());
}
function record_deposit(account_id: byte_array, mint: map<ft3_core.asset, integer>) {
require(is_signer(validator@{}.pubkey));
for((token, amount) in mint) {
ft3_core.ensure_balance( ft3_acc.account @ { account_id }, token ).amount += amount;
}
}
//function ensure_locked(account: ft3_acc.account, asset: ft3_core.asset): locked {
// val locked_entry = locked@?{account, asset};
// if(locked_entry !=null) {
// return locked_entry;
// }
// else return create locked(account, asset);
//}
function ensure_lock_account(account_: ft3_acc.account): ft3_acc.account {
val lock_account_id = ("LOCK_" + account_.id).hash();
val lock_account = ft3_acc.account@?{lock_account_id};
if(lock_account!=null) return lock_account;
else return create ft3_acc.account(lock_account_id);
}
function ensure_option(asset_name: text): ft3_core.asset {
val option_token = ft3_core.asset@?{.name == asset_name};
if(option_token!=null) return option_token;
else return ft3_core.register_asset(asset_name, chain_context.blockchain_rid);
}
function create_option(account_: ft3_acc.account, collateralized_asset: ft3_core.asset, type: option_type, payable_type, strike_price: integer, against_asset: ft3_core.asset, maturity_date: timestamp) {
val asset_name = option_ft3_asset(collateralized_asset, type, payable_type, strike_price, against_asset, maturity_date).hash();
val option_token = ensure_option(asset_name.to_hex());
val option_ = create option(account_, collateralized_asset, type, payable_type, strike_price, against_asset, maturity_date, option_token);
// give option_token to account
val balance = ft3_core.ensure_balance(account_, option_token);
balance.amount += 1;
}
function create_call(account_: ft3_acc.account, auth_desc_id: byte_array, collateralized_asset: ft3_core.asset, payable_type, strike_price: integer, against_asset: ft3_core.asset, maturity_date: timestamp) {
ft3_acc.require_auth(account_, auth_desc_id, list<text>());
// account needs to have the balance
require(ft3_core.balance@{account_, collateralized_asset}.amount > 1);
// locking the collateral
lock(account_, collateralized_asset, 1);
create_option(account_, collateralized_asset, option_type.CALL, payable_type, strike_price, against_asset, maturity_date);
}
function create_put(account_: ft3_acc.account, auth_desc_id: byte_array, collateralized_asset: ft3_core.asset, payable_type, strike_price: integer, against_asset: ft3_core.asset, maturity_date: timestamp) {
ft3_acc.require_auth(account_, auth_desc_id, list<text>());
require(ft3_core.balance@{account_, collateralized_asset}.amount > strike_price);
// locking the collateral (in this case the strike price since it is a put)
lock(account_, collateralized_asset, strike_price);
create_option(account_, collateralized_asset, option_type.PUT, payable_type, strike_price, against_asset, maturity_date);
}
function lock(account_: ft3_acc.account, asset_: ft3_core.asset, amount: integer) {
val lock_account = ensure_lock_account(account_);
// amount is removed from balance
update ft3_core.balance@{account_, asset_}(.amount -= 1);
update ft3_core.balance@{lock_account, asset_}(.amount += 1);
create ft3_core.payment_history_entry (
lock_account,
asset_,
.delta = amount,
.op_index = 500, //op_context ???
.is_input = true,
.entry_index = 0
);
create ft3_core.payment_history_entry (
account_,
asset_,
.delta = amount,
.op_index = 500, //op_context ???
.is_input = false,
.entry_index = 0
);
}
function execute_option(account: ft3_acc.account, auth_desc_id: byte_array, option_token: ft3_core.asset) {
// 1. Require owner
// check maturity and expiration
}
function free_option(account: ft3_acc.account, auth_desc_id: byte_array, option_token: ft3_core.asset) {
//1 require owner collateral
// check expiration date is old
// burn token
// free collateral
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.