diff --git a/.gitignore b/.gitignore index ac6649a..4da64f6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ node_modules **/*.pyc **/*.egg-info single_asset/lorentz/morley-ledgers/ +**/bisect*.coverage diff --git a/fractional/README.md b/fractional/README.md index d0f9ab9..6592082 100644 --- a/fractional/README.md +++ b/fractional/README.md @@ -27,8 +27,6 @@ Fraction of ownership is represented by the balance of the linked ownership toke allocated to the fractional owner. Fractional owner can vote to transfer an NFT to some other address. -**DAO admin** - a special privileged address which can setup initial fractional -NFT ownership and change some parameters of the ownership DAO. ## Operations diff --git a/generic_fractional_dao/README.md b/generic_fractional_dao/README.md new file mode 100644 index 0000000..e1f940f --- /dev/null +++ b/generic_fractional_dao/README.md @@ -0,0 +1,88 @@ +# Generic Fractional Ownership DAO + +Existing [fractional ownership DAO](../fractional/README.md) controls FA2 token +transfer operation using fractional ownership voting. However, token management +may involve other operations with the token (like putting it for sale on some +marketplace or auction contract). The set of operations, that can be performed +with the NFT and require fractional ownership control, is not predefined and can +be extended in the future. + +The proposed generic DAO uses fractional voting to control any generic operation +represented by a lambda function. Such lambda can transfer tokens, buy/sell tokens +on market place or auction or perform any other generic operation. Strictly speaking, +generic fractional ownership DAO is not explicitly tied to any NFTs. Fractional +owners can vote on any lambda representing the operation; if they try to transfer +some token which is not owned by the DAO, such operation will fail, when the +transfer is validated by FA2 contract. + +## Entities + +**NFT token** - any generic implementation of an FA2 NFT token that can be +transferred between any addresses. + +**Ownership DAO** - a contract that can own any generic NFT token and control +any operation with owned NFTs using fractional ownership logic. + +**Ownership token** - a fungible FA2 token embedded within the ownership DAO. +Token balances are allocated to fractional owners. Such allocations can be changed +by transferring ownership tokens. + +**Fractional owner** - an address which owns a fraction of all NFTs managed by +the DAO. Fraction of ownership is represented by the balance of the ownership token +allocated to the fractional owner. Fractional owner can vote on any operation lambda +that manipulates managed NFTs. + +**Operation lambda** - any operation that manipulates owned NFT tokens or DAO itself. +Fractional owners can vote on execution of the operation lambda. + +## DAO operations + +**Transfer ownership tokens** - Since the ownership token managed by the DAO is +a regular FA2 fungible token, fractional owners can transfer it using standard +FA2 transfer. + +**Vote on operation lambda** - any fractional owner can submit a vote consisting +of a lambda (`unit -> operation list`) and a nonce. +The vote weight is proportional to a balance of the ownership token allocated +to a fractional owner. Once predefined voting threshold is met, DAO executes the +lambda and returns produced operation. + +## DAO Entry Points + +### Standard FA2 entry points for ownership token + +`%transfer` + +`balance_of` + +`update_operators` + +### `%vote` + +```ocaml +%vote { + lambda: unit -> operation list; + nonce: nat; +} +``` + +## Miscellaneous + +Providing lambda "templates" or some high-level client API to create and sign +lambda functions for generic operations should be considered. + +1. NFT `transfer` and `update_operators`. +2. Interaction with market place and auction contracts. +3. DAO administration. + +## What's Next + +- Create tests for the fractional DAO contract +- Create helper Typescript API to generate DAO lambdas for the most common operations + + - Transfer governed token(s) + - Update operators for governed token(s) + - Change DAO voting threshold + - Change DAO voting period + +- Migrate DAO contract to minter-sdk diff --git a/generic_fractional_dao/flextesa b/generic_fractional_dao/flextesa new file mode 120000 index 0000000..e976725 --- /dev/null +++ b/generic_fractional_dao/flextesa @@ -0,0 +1 @@ +../shared/flextesa/ \ No newline at end of file diff --git a/generic_fractional_dao/ligo/fa2 b/generic_fractional_dao/ligo/fa2 new file mode 120000 index 0000000..6cb348d --- /dev/null +++ b/generic_fractional_dao/ligo/fa2 @@ -0,0 +1 @@ +../../shared/fa2 \ No newline at end of file diff --git a/generic_fractional_dao/ligo/fa2_modules b/generic_fractional_dao/ligo/fa2_modules new file mode 120000 index 0000000..147c835 --- /dev/null +++ b/generic_fractional_dao/ligo/fa2_modules @@ -0,0 +1 @@ +../../shared/fa2_modules/ \ No newline at end of file diff --git a/generic_fractional_dao/ligo/src/fa2_single_token.mligo b/generic_fractional_dao/ligo/src/fa2_single_token.mligo new file mode 120000 index 0000000..12ea74c --- /dev/null +++ b/generic_fractional_dao/ligo/src/fa2_single_token.mligo @@ -0,0 +1 @@ +../../../single_asset/ligo/src/fa2_single_token.mligo \ No newline at end of file diff --git a/generic_fractional_dao/ligo/src/fractional_dao.mligo b/generic_fractional_dao/ligo/src/fractional_dao.mligo new file mode 100644 index 0000000..9bd7655 --- /dev/null +++ b/generic_fractional_dao/ligo/src/fractional_dao.mligo @@ -0,0 +1,215 @@ +#if !FRACTIONAL_DAO +#define FRACTIONAL_DAO + +#include "fa2_single_token.mligo" + +type permit = +[@layout:comb] +{ + key : key; (* user's key *) + signature : signature; (*signature of packed lambda + permit context *) +} + +type proposal_info = { + vote_amount : nat; + voters : address set; + timestamp : timestamp; +} + +type dao_lambda = unit -> operation list + +type vote = +[@layout:comb] +{ + lambda : dao_lambda; + permit : permit option; +} + +type set_voting_threshold_param = +{ + old_threshold: nat; + new_threshold: nat; +} + +type set_voting_period_param = +{ + old_period: nat; + new_period: nat; +} + +type pending_proposals = (bytes, proposal_info) big_map + +type dao_storage = { + ownership_token : single_token_storage; + voting_threshold : nat; + voting_period : nat; + vote_count : nat; + pending_proposals: pending_proposals; + metadata : contract_metadata; +} + +type dao_entrypoints = + | Fa2 of fa2_entry_points + | Vote of vote + (** self-governance entry point *) + | Set_voting_threshold of set_voting_threshold_param + (** self-governance entry point *) + | Set_voting_period of set_voting_period_param + (** self-governance entry point *) + | Flush_expired of dao_lambda + +type return = (operation list) * dao_storage + +[@inline] +let assert_self_call () = + if Tezos.sender = Tezos.self_address + then unit + else failwith "UNVOTED_CALL" + +let set_voting_threshold (t, s : set_voting_threshold_param * dao_storage) + : dao_storage = + if t.old_threshold <> s.voting_threshold + then (failwith "INVALID_OLD_THRESHOLD" : dao_storage) + else if t.new_threshold > s.ownership_token.total_supply + then (failwith "THRESHOLD_EXCEEDS_TOTAL_SUPPLY" : dao_storage) + else { s with voting_threshold = t.new_threshold; } + +let set_voting_period (p, s : set_voting_period_param * dao_storage) + : dao_storage = + if p.old_period <> s.voting_period + then (failwith "INVALID_OLD_PERIOD" : dao_storage) + else if p.new_period < 300n + then (failwith "PERIOD_TOO_SHORT" : dao_storage) + else { s with voting_period = p.new_period; } + +let is_expired (proposal, voting_period : proposal_info * nat) : bool = + if Tezos.now - proposal.timestamp > int(voting_period) + then true + else false + +let flush_expired (lambda, s : dao_lambda * dao_storage ) : dao_storage = + let key = Bytes.pack lambda in + match Big_map.find_opt key s.pending_proposals with + | None -> (failwith "PROPOSAL_DOES_NOT_EXIST" : dao_storage) + | Some proposal -> + if is_expired(proposal, s.voting_period) + then + let new_pending = Big_map.remove key s.pending_proposals in + { s with pending_proposals = new_pending; } + else (failwith "NOT_EXPIRED" : dao_storage) + + +let validate_permit (lambda, permit, vote_count + : dao_lambda * permit * nat) : address = + let signed_data = Bytes.pack ( + (Tezos.chain_id, Tezos.self_address), + (vote_count, lambda) + ) in + if Crypto.check permit.key permit.signature signed_data + then Tezos.address (Tezos.implicit_account (Crypto.hash_key (permit.key))) + else (failwith "MISSIGNED" : address) + +let get_voter_stake (voter, ledger : address * ledger) : nat = + match Big_map.find_opt voter ledger with + | None -> (failwith "NOT_VOTER" : nat) + | Some stake -> stake + +let update_proposal (proposal, vote_key, s : proposal_info * bytes * dao_storage) + : return = + let new_pending = Big_map.update vote_key (Some proposal) s.pending_proposals in + ([] : operation list), { s with pending_proposals = new_pending; } + +let execute_proposal (lambda, vote_key, s : dao_lambda * bytes * dao_storage) + : return = + let new_pending = Big_map.remove vote_key s.pending_proposals in + let ops = lambda () in + ops, { s with pending_proposals = new_pending; } + +let vote (v, s : vote * dao_storage) : return = + let voter = match v.permit with + | None -> Tezos.sender + | Some p -> validate_permit (v.lambda, p, s.vote_count) + in + let voter_stake = get_voter_stake (voter, s.ownership_token.ledger) in + let vote_key = Bytes.pack v.lambda in + let proposal = match Big_map.find_opt vote_key s.pending_proposals with + | None -> { + vote_amount = voter_stake; + voters = Set.literal [voter]; + timestamp = Tezos.now; + } + | Some p -> + if is_expired (p, s.voting_period) + then (failwith "EXPIRED" : proposal_info) + else if Set.mem voter p.voters + then (failwith "DUP_VOTE" : proposal_info) + else + { p with + vote_amount = p.vote_amount + voter_stake; + voters = Set.add voter p.voters; + } + in + if proposal.vote_amount < s.voting_threshold + then update_proposal (proposal, vote_key, s) + else execute_proposal (v.lambda, vote_key, s) + +let main(param, storage : dao_entrypoints * dao_storage) : return = + match param with + | Fa2 fa2 -> + let ops, new_ownership = fa2_main(fa2, storage.ownership_token) in + ops, { storage with ownership_token = new_ownership; } + + | Vote v -> vote (v, storage) + + | Set_voting_threshold t -> + let u = assert_self_call () in + let new_storage = set_voting_threshold (t, storage) in + ([] : operation list), new_storage + + | Set_voting_period p -> + let u = assert_self_call () in + let new_storage = set_voting_period (p, storage) in + ([] : operation list), new_storage + + | Flush_expired lambda -> + let new_storage = flush_expired (lambda, storage) in + ([] : operation list), new_storage + + +(* let token : single_token_storage = { + +} *) + +let sample_storage : dao_storage = { + ownership_token = { + ledger = Big_map.literal [ + (("tz1YPSCGWXwBdTncK2aCctSZAXWvGsGwVJqU" : address), 50n); + (("KT193LPqieuBfx1hqzXGZhuX2upkkKgfNY9w" : address), 50n); + ]; + operators = (Big_map.empty : operator_storage); + token_metadata = Big_map.literal [ + ( 0n, + { + token_id = 0n; + token_info = Map.literal [ + ("symbol", 0x544b31); + ("name", 0x5465737420546f6b656e); + ("decimals", 0x30); + ]; + } + ); + ]; + total_supply = 100n; + }; + voting_threshold = 75n; + voting_period = 10000000n; + vote_count = 0n; + pending_proposals = (Big_map.empty : pending_proposals); + metadata = Big_map.literal [ + ("", Bytes.pack "tezos-storage:content" ); + ("", 0x00); + ("content", 0x00) (* bytes encoded UTF-8 JSON *) + ]; +} + +#endif diff --git a/generic_fractional_dao/tests/.prettierrc b/generic_fractional_dao/tests/.prettierrc new file mode 100644 index 0000000..e59c880 --- /dev/null +++ b/generic_fractional_dao/tests/.prettierrc @@ -0,0 +1,9 @@ +{ + "semi": true, + "tabWidth": 2, + "trailingComma": "none", + "singleQuote": true, + "bracketSpacing": true, + "arrowParens": "avoid", + "printWidth": 80 +} diff --git a/generic_fractional_dao/tests/jest.config.js b/generic_fractional_dao/tests/jest.config.js new file mode 100644 index 0000000..5699881 --- /dev/null +++ b/generic_fractional_dao/tests/jest.config.js @@ -0,0 +1,5 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node' + // testMatch: ['**/__tests__/*.+(spec|test).[jt]s?(x)'] +}; diff --git a/generic_fractional_dao/tests/ligo.ts b/generic_fractional_dao/tests/ligo.ts new file mode 120000 index 0000000..a2b425a --- /dev/null +++ b/generic_fractional_dao/tests/ligo.ts @@ -0,0 +1 @@ +../../shared/typescript/ligo.ts \ No newline at end of file diff --git a/generic_fractional_dao/tests/tsconfig.json b/generic_fractional_dao/tests/tsconfig.json new file mode 100644 index 0000000..6f7e497 --- /dev/null +++ b/generic_fractional_dao/tests/tsconfig.json @@ -0,0 +1,69 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Basic Options */ + // "incremental": true, /* Enable incremental compilation */ + "target": "ES5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, + "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, + // "lib": [], /* Specify library files to be included in the compilation. */ + // "allowJs": true, /* Allow javascript files to be compiled. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + // "declaration": true, /* Generates corresponding '.d.ts' file. */ + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + // "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + // "outDir": "./", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + + /* Strict Type-Checking Options */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + + /* Advanced Options */ + "skipLibCheck": true /* Skip type checking of declaration files. */, + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + } +}