Snippets

chromawallet rnzgr9: Untitled snippet

Created by Riccardo Sibani

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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.