Skip to content
This repository has been archived by the owner on Jul 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from logicalmechanism/adding-royalty-struc
Browse files Browse the repository at this point in the history
Adding royalty struc
  • Loading branch information
logicalmechanism authored Oct 4, 2022
2 parents 7d99ddc + f123b6c commit 5f67a2f
Show file tree
Hide file tree
Showing 19 changed files with 511 additions and 164 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ A smart contract for UTxO-based cognomens. Each UTxO is a wallet identifier simi

[Oracle](#oracle)

[Royalty](#royalty)

# Cogno

A user is always in control of their cognomen. A user may update or remove their cogno at anytime as long as their public key hash is present inside the removal transaction. Other users may give kudos to a cogno at any time to show praise. The cogno is designed to hold many familiar social media profile-related data such as contact information or a profile image. There is a minimum ADA threshold to use some of the endpoints. This is to ensure there is always enough ADA on the UTxO for whatever cogno data may be present inside a transaction. This minimum ADA can also act like a cogno's skin in the game via a refundable ADA deposit. The minimum value is currently set to 10 ADA for testing.
Expand Down Expand Up @@ -175,6 +177,37 @@ The key use case for the oracle data is providing an on-chain data feed of price

[Back To Top](#table-of-contents)

# Royalty

The royalty data type is slightly different than the other cogno-related data types. The data relates royalty payout information by connecting to a policy id and token name instead of a cogno profile. It may be used to reference the royalty rate and the list of payout addresses associated with the royalty. The royalty has the ability to determine the preferred payout token, allowing royalty payouts to occur in royalty group's favorite token.

The data structure is built to handle groups via a list of keys and a threshold vote parameter. The endpoints on this data type are all multisig agreements with everyone in the royalty group. This means even removing the royalty has to be a group decision. This may be trouble for some set ups but it does allow some security knowing that not all the variables can be destroyed for the gains of a singular user.

Each UTxO for a group is for a single policy id and token name. If there happen to be many tokens on a policy id that all have the same royalty rate then the token name may be left blank to indicate its policy base only. But if finer control is required then a token by token style royalty system can be built with this style of data.

```hs
data RoyaltyData = RoyaltyData
{ rPkhs :: [PlutusV2.PubKeyHash]
-- ^ The list of all royalty receiving public key hashes.
, rScs :: [PlutusV2.PubKeyHash]
-- ^ The list of all royalty stake key hashes.
, rInPid :: PlutusV2.CurrencySymbol
-- ^ The policy id for applying the royalty.
, rInTkn :: PlutusV2.TokenName
-- ^ The token name for applying the royalty.
, rRate :: Integer
-- ^ The royalty rate for the token.
, rOutPid :: PlutusV2.CurrencySymbol
-- ^ The policy id of the royalty payout.
, rOutTkn :: PlutusV2.TokenName
-- ^ The token name of the royalty payout.
, rThres :: Integer
-- ^ The multisig threshold.
}
```

[Back To Top](#table-of-contents)

### Address to PubKeyHash Example

Obtaining the PubKeyHash format for the datum from a base address is rather easy. Assume the wallet address is
Expand Down
8 changes: 7 additions & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,10 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/raduom/hysterical-screams
tag: f3bbd38a19f99de5c8ddc650c94330b2d09a865b
tag: f3bbd38a19f99de5c8ddc650c94330b2d09a865b

source-repository-package
type: git
location: https://github.com/logicalmechanism/useful-funcs
tag: c389842f483f42ac5882244ee32861471435b7f0
subdir: useful-funcs
3 changes: 2 additions & 1 deletion cogno-contract/cogno-contract.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ library
TagDataType
RankDataType
OracleDataType
HelperFunctions
RoyaltyDataType

build-depends: bytestring
, cardano-api
Expand All @@ -59,6 +59,7 @@ library
, plutus-tx-plugin
, plutus-script-utils
, serialise
, useful-funcs

ghc-options: -fobject-code -fno-ignore-interface-pragmas -fno-omit-interface-pragmas

Expand Down
2 changes: 1 addition & 1 deletion cogno-contract/cogno-contract.plutus

Large diffs are not rendered by default.

103 changes: 69 additions & 34 deletions cogno-contract/src/CognoContract.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import CognoDataType
import TagDataType
import RankDataType
import OracleDataType
import HelperFunctions
import RoyaltyDataType
import UsefulFuncs
{- |
Author : The Ancient Kraken
Copyright: 2022
Expand All @@ -59,19 +60,26 @@ import HelperFunctions
The Glorious Glasgow Haskell Compilation System, version 8.10.7
-}
-- | The minimum value of ADA in a wallet to update a cogno. 10 ADA
thresholdLovelace :: Integer
thresholdLovelace = 10000000

minimumValue :: PlutusV2.Value
minimumValue = Value.singleton Value.adaSymbol Value.adaToken thresholdLovelace
-------------------------------------------------------------------------------
-- | Create the datum type.
-------------------------------------------------------------------------------
data CustomDatumType = Cogno CognoData |
Tag TagData |
Rank RankData |
Oracle OracleData
PlutusTx.makeIsDataIndexed ''CustomDatumType [ ( 'Cogno, 0 )
, ( 'Tag, 1 )
, ( 'Rank, 2 )
, ( 'Oracle, 3 )
data CustomDatumType = Cogno CognoData |
Tag TagData |
Rank RankData |
Oracle OracleData |
Royalty RoyaltyData
PlutusTx.makeIsDataIndexed ''CustomDatumType [ ( 'Cogno, 0 )
, ( 'Tag, 1 )
, ( 'Rank, 2 )
, ( 'Oracle, 3 )
, ( 'Royalty, 4 )
]

-------------------------------------------------------------------------------
-- | Create the redeemer type.
-------------------------------------------------------------------------------
Expand All @@ -91,6 +99,40 @@ PlutusTx.makeIsDataIndexed ''CustomRedeemerType [ ( 'Remove, 0 )
mkValidator :: CustomDatumType -> CustomRedeemerType -> PlutusV2.ScriptContext -> Bool
mkValidator datum redeemer context =
case datum of
{- | The royalty state
Any and all royalty validation logic will be here.
-}
(Royalty rd) ->
let userPkhs = rPkhs rd
in case redeemer of
-- remove utxo from the contract and send it out
Remove -> do
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 0 -- single input no outputs
; let b = traceIfFalse "Wrong Tx Signer" $ checkValidMultisig info userPkhs (rThres rd) -- wallet must sign it
; traceIfFalse "Royalty Remove Error" $ all (==(True :: Bool)) [a,b]
}

-- update the utxo datum and send the utxo back to the contract
Update ->
case getOutboundDatum contTxOutputs validatingValue of
Nothing -> False
Just outboundDatum ->
case outboundDatum of
( Royalty rd' ) -> do
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 1 -- single input single output
; let b = traceIfFalse "Wrong Tx Signer" $ checkValidMultisig info userPkhs (rThres rd) -- wallet must sign it
; let c = traceIfFalse "Incorrect Datum" $ updateRoyaltyData rd rd' -- the datum changes correctly
; traceIfFalse "Royalty Update Error" $ all (==(True :: Bool)) [a,b,c]
}

-- only royalty datum
_ -> False

-- only remove or update redeemers
_ -> False

{- | The oracle state
Any and all oracle validation logic will be here.
Expand All @@ -102,10 +144,10 @@ mkValidator datum redeemer context =
in case redeemer of
-- remove utxo from the contract and send to user's address
Remove -> do
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 0 -- single input no outputs
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Value Not Paid" $ isAddrGettingPaid txOutputs userAddr validatingValue -- send back the leftover
; traceIfFalse "Rank Remove Error" $ all (==(True :: Bool)) [a,b,c]
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 0 -- single input no outputs
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Value Not Paid" $ isAddrGettingPaidExactly txOutputs userAddr validatingValue -- send back the leftover
; traceIfFalse "Oracle Remove Error" $ all (==(True :: Bool)) [a,b,c]
}

-- update the utxo datum and send the utxo back to the contract
Expand All @@ -115,10 +157,10 @@ mkValidator datum redeemer context =
Just outboundDatum ->
case outboundDatum of
( Oracle od' ) -> do
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 1 -- single input single output
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Incorrect Datum" $ updateOracleData od od' -- the datum changes correctly
; traceIfFalse "Rank Update Error" $ all (==(True :: Bool)) [a,b,c]
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 1 -- single input single output
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Incorrect Datum" $ updateOracleData od od' -- the datum changes correctly
; traceIfFalse "Oracle Update Error" $ all (==(True :: Bool)) [a,b,c]
}

-- only oracle datum
Expand All @@ -138,9 +180,9 @@ mkValidator datum redeemer context =
in case redeemer of
-- remove utxo from the contract and send to user's address
Remove -> do
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 0 -- single input no outputs
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Value Not Paid" $ isAddrGettingPaid txOutputs userAddr validatingValue -- send back the leftover
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 0 -- single input no outputs
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Value Not Paid" $ isAddrGettingPaidExactly txOutputs userAddr validatingValue -- send back the leftover
; traceIfFalse "Rank Remove Error" $ all (==(True :: Bool)) [a,b,c]
}

Expand Down Expand Up @@ -203,9 +245,9 @@ mkValidator datum redeemer context =
in case redeemer of
-- remove utxo from the contract and send to user's address
Remove -> do
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 0 -- single input no outputs
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Value Not Paid" $ isAddrGettingPaid txOutputs userAddr validatingValue -- send back the leftover
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 0 -- single input no outputs
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Value Not Paid" $ isAddrGettingPaidExactly txOutputs userAddr validatingValue -- send back the leftover
; traceIfFalse "Tag Remove Error" $ all (==(True :: Bool)) [a,b,c]
}

Expand Down Expand Up @@ -239,9 +281,9 @@ mkValidator datum redeemer context =
in case redeemer of
-- remove utxo from the contract and send it back to the user's address
Remove -> do
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 0 -- single input no outputs
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Value Not Paid" $ isAddrGettingPaid txOutputs userAddr validatingValue -- send back the leftover
{ let a = traceIfFalse "Incorrect In/Out" $ isNInputs txInputs 1 && isNOutputs contTxOutputs 0 -- single input no outputs
; let b = traceIfFalse "Wrong Tx Signer" $ ContextsV2.txSignedBy info userPkh -- wallet must sign it
; let c = traceIfFalse "Value Not Paid" $ isAddrGettingPaidExactly txOutputs userAddr validatingValue -- send back the leftover
; traceIfFalse "Cogno Remove Error" $ all (==True) [a,b,c]
}

Expand Down Expand Up @@ -303,13 +345,6 @@ mkValidator datum redeemer context =
Nothing -> traceError "" -- This error should never be hit.
Just input -> PlutusV2.txOutValue $ PlutusV2.txInInfoResolved input

-- | The minimum value of ADA in a wallet to update a cogno. 10 ADA
thresholdLovelace :: Integer
thresholdLovelace = 10000000

minimumValue :: PlutusV2.Value
minimumValue = Value.singleton Value.adaSymbol Value.adaToken thresholdLovelace

-- | Get the inline datum that holds a value from a list of tx outs.
getOutboundDatum :: [PlutusV2.TxOut] -> PlutusV2.Value -> Maybe CustomDatumType
getOutboundDatum [] _ = Nothing
Expand Down
9 changes: 1 addition & 8 deletions cogno-contract/src/CognoDataType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fexpose-all-unfoldings #-}
module CognoDataType
( CognoData
, cdPkh
, cdSc
, cdKudos
, cdCogno
, cdImage
, cdDetail
, cdLocale
( CognoData (..)
, updateCognoData
, giveAKudo
) where
Expand Down
87 changes: 0 additions & 87 deletions cogno-contract/src/HelperFunctions.hs

This file was deleted.

13 changes: 1 addition & 12 deletions cogno-contract/src/OracleDataType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,7 @@
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fexpose-all-unfoldings #-}
module OracleDataType
( OracleData
, oPkh
, oSc
, oInPid
, oInTkn
, oInAmt
, oOutPid
, oOutTkn
, oOutAmt
, oAge
, oCognoTxId
, oCognoIndex
( OracleData (..)
, getInPriceConversion
, getOutPriceConversion
, updateOracleData
Expand Down
10 changes: 1 addition & 9 deletions cogno-contract/src/RankDataType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fexpose-all-unfoldings #-}
module RankDataType
( RankData
, rPkh
, rSc
, rUpVote
, rDownVote
, rAge
, rCognoTxId
, rCognoIndex
, rType
( RankData (..)
, checkForUpVote
, checkForDownVote
, checkForNewCogno
Expand Down
Loading

0 comments on commit 5f67a2f

Please sign in to comment.