Skip to content

Commit

Permalink
feat: adapted apps
Browse files Browse the repository at this point in the history
  • Loading branch information
heueristik committed Oct 21, 2024
1 parent f405b64 commit c751862
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 465 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/test.yaml

This file was deleted.

16 changes: 10 additions & 6 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test anoma-app-patterns
name: Test anoma-apps

on:
push:
Expand All @@ -7,21 +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: Clean
run: juvix clean --global && juvix clean && juvix dependencies update

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
17 changes: 14 additions & 3 deletions Kudos/Example.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@ import Stdlib.Debug.Fail open using {failwith};
import Resource.Error open;
import Authorization.Identities open;

import Anoma.Transaction open;
import Anoma.Identity open;
import Anoma.State.CommitmentTree open;

import AnomaHelpers open;
import Interface open;

alice : KeyPair := Universal.keyPair;
alice : Identity := Universal.identity;

bob : ExternalIdentity := Zero.externalIdentity;

bob : PublicKey := Zero.pubKey;
standardInputs : StandardInputs :=
mkStandardInputs@{
identity := alice;
currentRoot := mkRoot 0
};

--- Create 10 Kudo tokens as Alice (the originator) for Bob (the owner).
main : Transaction :=
case
initialize@{
self := alice;
standardInputs;
quantity := 10;
receiver := bob
}
Expand Down
193 changes: 111 additions & 82 deletions Kudos/Interface.juvix
Original file line number Diff line number Diff line change
@@ -1,121 +1,150 @@
module Interface;

import Stdlib.Prelude open;
import Anoma open;
import Data.Set as Set open using {Set};

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

import Resource.Error open;
import Resource.Traits open;
import Transaction as Transaction open;
import Projection.Balance as Projection open;
import Transaction as Transaction;

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

import AnomaHelpers open;

import Label open;

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

finalize (self : KeyPair) (token : Token) : Result StandardError Transaction :=
case isKudo (KeyPair.pubKey self) token of
| false := throw notKudoError
| true :=
Transaction.finalize@{
self;
toFinalize := token
};
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
(self : KeyPair)
(standardInputs : StandardInputs)
(token : Token)
(quantity : Nat)
(receiver : PublicKey)
(quantity : Quantity)
(receiver : ExternalIdentity)
: Result StandardError Transaction :=
case isKudo (KeyPair.pubKey self) token of
| false := throw notKudoError
| true :=
Transaction.send@{
self;
toSend := token;
quantity;
receiver
};
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
(self : KeyPair) (token : Token) (receiver : PublicKey) : Result StandardError Transaction :=
case isKudo (KeyPair.pubKey self) token of
| false := throw notKudoError
| true :=
Transaction.transfer@{
self;
toTransfer := token;
receiver
};
(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
(self : KeyPair)
(standardInputs : StandardInputs)
(token : Token)
(quantitiesAndReceivers : List (Pair Nat PublicKey))
-- TODO refactor type
(quantitiesAndReceivers : List (Pair Quantity ExternalIdentity))
: Result StandardError Transaction :=
case isKudo (KeyPair.pubKey self) token of
| false := throw notKudoError
| true :=
Transaction.split@{
self;
toSplit := token;
quantitiesAndReceivers
};
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
(self : KeyPair)
(standardInputs : StandardInputs)
(tokens : List Token)
(receiver : PublicKey)
(receiver : ExternalIdentity)
: Result StandardError Transaction :=
case all (t in tokens) {isKudo (KeyPair.pubKey self) t} of
| false := throw notKudoError
| true :=
Transaction.merge@{
self;
toMerge := tokens;
receiver
};
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
(self : KeyPair)
(tokens : List Token)
(standardInputs : StandardInputs)
(toSwap : List Token)
(want : QuantifiedAssets)
(solver : PublicKey)
(solver : ExternalIdentity)
: Result StandardError Transaction :=
Transaction.swap@{
self;
toSwap := tokens;
intent :=
SwapIntent.create@{
want;
receiver := KeyPair.pubKey self;
solver
}
};

--- Returns the total quantity of all Kudo ;Kind; resources from an orignator ;PublicKey; for an account ;PublicKey;
balance (originator : PublicKey) (account : PublicKey) : Nat :=
let
label : Label := mkKudoLabel originator;
logic : Resource -> Transaction -> Bool := tokenLogic (Label.supply label) originator;
kind : Kind := anomaDecode (anomaEncode (tokenLogic, label));
ownedKudos : Set Resource := fetchOwnedResources kind account;
in Projection.balance ownedKudos;
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
};
2 changes: 1 addition & 1 deletion Kudos/Kudos.juvix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Kudos;

import Label open public;
import Interface open public;
import Interface open public;
17 changes: 10 additions & 7 deletions Kudos/Label.juvix
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Label;

import Stdlib.Prelude open;
import Anoma open;
import AnomaHelpers open;

import Token.Resource open;
Expand All @@ -11,8 +10,11 @@ import Token.Logic open;
import Resource.Error open;
import Resource.Traits open;

mkKudoLabel (originator : PublicKey) : Label :=
mkLabel@{
import Anoma.Identity open;
import Anoma.Builtin.System open;

mkKudoLabel (originator : ExternalIdentity) : TokenLabel :=
mkTokenLabel@{
name := "Kudos";
symbol := "KDS";
decimals := 18;
Expand All @@ -21,11 +23,12 @@ mkKudoLabel (originator : PublicKey) : Label :=
originator
};

isKudo (originator : PublicKey) (token : Token) : Bool :=
isKudo (originator : ExternalIdentity) (token : Token) : Bool :=
let
expectedLabel : Label := mkKudoLabel originator;
label : Label := HasLabel.get token;
expectedLogicEncoded : Nat := tokenLogic (Label.supply expectedLabel) originator |> anomaEncode;
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
Expand Down
Loading

0 comments on commit c751862

Please sign in to comment.