Skip to content

Commit

Permalink
feat: update kudos (#7)
Browse files Browse the repository at this point in the history
* refactor: kudos

* build: update deps

* feat: add logic comparison

* revert: reintroduce example

* fix: naming

* docs: improve comment

* feat: update kudos

* refactor: formatting and naming

* feat: improve kudos error handling

* feat: imporved kudo check

* build: update deps

* feat: implement getBalance

* fix: wrong import

* refactor: update deps and label

* build: use nightly tag

* build: update deps

* build: update deps

* feat: improve error handling

* build: update deps

* build: update deps

* fix: example

* chore: update deps

* chore: update deps and adapt

* refactor: imports

* refactor: adapt Kudos

* chore: update deps

* chore: bump deps and cleaning

* chore: bump deps

* chore: update deps

* feat: adapted apps

* chore: use refactored applib

* chore: update deps

* chore: bump dep
  • Loading branch information
heueristik authored Oct 23, 2024
1 parent a4f04aa commit 10113e3
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 543 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/test.yaml → .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

name: Test anoma-apps

on:
Expand All @@ -8,18 +7,25 @@ on:
workflow_dispatch:

jobs:
test:
tests:
name: Run test suite
runs-on: ubuntu-latest
defaults:
run:
working-directory: Kudos
steps:
- name: checkout code
uses: actions/checkout@v3

- name: Download latest nightly Juvix binary
uses: jaxxstorm/[email protected]
with:
repo: anoma/juvix-nightly-builds
cache: enable

- name: Run tests
run: juvix eval tests/Main.juvix
- name: Clean
run: juvix clean --global && juvix clean && juvix dependencies update
- name: Type Check
run: juvix typecheck
- name: Format Check
run: juvix format
- name: Run Example
run: juvix eval Example.juvix
35 changes: 28 additions & 7 deletions Kudos/Example.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,36 @@ module Example;

import Anoma open;
import Stdlib.Prelude open;
import Stdlib.Debug.Fail open using {failwith};

import Kudos open;
import Applib.Resource.Error open;
import Applib.Authorization.Identities open;
import Applib.Helpers open;

alice : KeyPair := mkKeyPair@{
pubKey := 0xddd315c76991f8e058760cacdd19c21bf6a12c72bc229a60ad6aaa314fa07ac1;
privKey := 0xddd315c76991f8e058760cacdd19c21bf6a12c72bc229a60ad6aaa314fa07ac11662fc6e7829efcb0f4500827d49bb699af7b5475cef5220fd600ebbf9709a58;
};
import Anoma.Transaction open;
import Anoma.Identity open;
import Anoma.State.CommitmentTree open;

bob : PublicKey := 0x7d59c5623dd40a74aa4d5a32ac645d3b3f95daeae4c22be25476dd6a486f7382;
import Interface open;

alice : Identity := Universal.identity;

bob : ExternalIdentity := Zero.externalIdentity;

standardInputs : StandardInputs :=
mkStandardInputs@{
identity := alice;
currentRoot := mkRoot 0
};

--- Create 10 Kudo tokens as Alice (the originator) for Bob (the owner).
main : Transaction := create@{self := alice; amount := 10; receiver := bob};
main : Transaction :=
case
initialize@{
standardInputs;
quantity := 10;
receiver := bob
}
of
| error err := failwith (Show.show err)
| ok tx := tx;
150 changes: 150 additions & 0 deletions Kudos/Interface.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
module Interface;

import Stdlib.Prelude open;

import Anoma.Identity open;
import Anoma.Transaction open;
import Anoma.Resource open;

import Applib.Resource.Error open;
import Applib.Transaction as Transaction;

import Applib.Token.Resource open;
import Applib.Token.Label open;
import Applib.Intent.Asset open;
import Applib.Intent.Swap.Resource open;
import Applib.Intent.Swap.Solution open;

import Applib.Helpers open;

import Label open;

initialize
(standardInputs : StandardInputs)
(quantity : Quantity)
(receiver : ExternalIdentity)
: Result StandardError Transaction :=
let
self := standardInputs |> StandardInputs.identity |> Identity.external;
token :=
Token.create@{
quantity;
tokenLabel := mkKudoLabel self;
owner := receiver
};
in Transaction.initialize@{
standardInputs;
toInitialize := token;
maybeDummy := nothing
};

finalize (standardInputs : StandardInputs) (token : Token) : Result StandardError Transaction :=
let
self : ExternalIdentity := standardInputs |> StandardInputs.identity |> Identity.external;
in case isKudo (self) token of
| false := throw notKudoError
| true :=
Transaction.finalize@{
standardInputs;
toFinalize := token
};

send
(standardInputs : StandardInputs)
(token : Token)
(quantity : Quantity)
(receiver : ExternalIdentity)
: Result StandardError Transaction :=
let
self : ExternalIdentity := standardInputs |> StandardInputs.identity |> Identity.external;
in case isKudo self token of
| false := throw notKudoError
| true :=
Transaction.send@{
standardInputs;
toSend := token;
quantity;
receiver
};

transfer
(standardInputs : StandardInputs)
(token : Token)
(receiver : ExternalIdentity)
: Result StandardError Transaction :=
let
self : ExternalIdentity := standardInputs |> StandardInputs.identity |> Identity.external;
in case isKudo (self) token of
| false := throw notKudoError
| true :=
Transaction.transfer@{
standardInputs;
toTransfer := token;
receiver
};

split
(standardInputs : StandardInputs)
(token : Token)
-- TODO refactor type
(quantitiesAndReceivers : List (Pair Quantity ExternalIdentity))
: Result StandardError Transaction :=
let
self : ExternalIdentity := standardInputs |> StandardInputs.identity |> Identity.external;
in case isKudo self token of
| false := throw notKudoError
| true :=
Transaction.split@{
standardInputs;
toSplit := token;
quantitiesAndReceivers
};

merge
(standardInputs : StandardInputs)
(tokens : List Token)
(receiver : ExternalIdentity)
: Result StandardError Transaction :=
let
self := standardInputs |> StandardInputs.identity |> Identity.external;
in case all (t in tokens) {isKudo self t} of
| false := throw notKudoError
| true :=
Transaction.merge@{
standardInputs;
toMerge := tokens;
receiver
};

swap
(standardInputs : StandardInputs)
(toSwap : List Token)
(want : QuantifiedAssets)
(solver : ExternalIdentity)
: Result StandardError Transaction :=
let
self := standardInputs |> StandardInputs.identity |> Identity.external;
in case all (t in toSwap) {isKudo self t} of
| false := throw notKudoError
| true :=
Transaction.swap@{
standardInputs;
toSwap;
intent :=
SwapIntent.create@{
want;
receiver := self;
solver
}
};

settle
(standardInputs : StandardInputs)
(transactions : List Transaction)
(solutions : List Solution)
: Result StandardError Transaction :=
Transaction.settle@{
standardInputs;
transactions;
solutions
};
38 changes: 2 additions & 36 deletions Kudos/Kudos.juvix
Original file line number Diff line number Diff line change
@@ -1,38 +1,4 @@
module Kudos;

import Stdlib.Prelude open;
import Anoma open;
import Token.Transaction open;
import Token.Logic open;

import Label open;

--- Creates an amount of Kudo tokens to a receiver.
create
(self : KeyPair)
(amount : Nat)
(receiver : PublicKey)
: Transaction :=
let
kudoLabel : Label := mkKudoLabel (KeyPair.pubKey self);
in Token.Transaction.mint self kudoLabel amount receiver;

--- Sends an amount of Kudo tokens to a receiver.
--- If the token ;Resource; has no Kudos ;Label; this function returns nothing.
--- If the token logic function differs from the current implementation, this function returns nothing,
--- If the calling ;KeyPair; is not the owner, this function returns nothing.
send
(self : KeyPair)
(token : Resource)
(amount : Nat)
(receiver : PublicKey)
: Maybe Transaction :=
let
isKudo (r : Resource) : Bool :=
getSymbol r == "Kudos"
&& getDecimals r == 18
&& anomaEncode(Resource.logic r) == anomaEncode(tokenLogic);
in if
| isKudo token :=
Token.Transaction.send self token amount receiver
| else := nothing;
import Label open public;
import Interface open public;
44 changes: 35 additions & 9 deletions Kudos/Label.juvix
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
module Label;

import Anoma open;
import Stdlib.Prelude open;
import Applib.Helpers open;

import Token.Label open public;
import Applib.Token.Resource open;
import Applib.Token.Label open;
import Applib.Token.Logic open;

mkKudoLabel (originator : PublicKey) : Label :=
mkLabel@{
name := originator;
symbol := "Kudos";
decimals := 18
import Applib.Resource.Error open;
import Applib.Resource.Traits open;

import Anoma.Identity open;
import Anoma.Builtin.System open;

mkKudoLabel (originator : ExternalIdentity) : TokenLabel :=
mkTokenLabel@{
name := "Kudos";
symbol := "KDS";
decimals := 18;
supply := Unbound;
transferability := Transferability.Transferable;
originator
};

getOriginator (r : Resource) : PublicKey :=
Label.name (anomaDecode (Resource.label r));
isKudo (originator : ExternalIdentity) (token : Token) : Bool :=
let
expectedLabel : TokenLabel := mkKudoLabel originator;
label : TokenLabel := HasLabel.get token;
expectedLogicEncoded : Nat :=
tokenLogic (TokenLabel.supply expectedLabel) originator |> anomaEncode;
logicEncoded : Nat := HasLogic.get token |> anomaEncode;
in if
| label /= expectedLabel := false
| logicEncoded /= expectedLogicEncoded := false
| else := true;

notKudoError : DefaultError :=
mkDefaultError@{
msg := "The input resource is not a Kudo token."
};
Loading

0 comments on commit 10113e3

Please sign in to comment.