Skip to content

Commit f9ca859

Browse files
committed
Paisley test token
1 parent 439cf7f commit f9ca859

File tree

4 files changed

+213
-2
lines changed

4 files changed

+213
-2
lines changed

convex-core/src/main/antlr4/convex/core/json/reader/antlr/JSON.g4

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ bool
3838

3939
string
4040
: STRING;
41-
41+
42+
// numbers allow extra IEEE754 values as per JSON5
4243
number
43-
: NUMBER;
44+
: NUMBER | 'NaN' | 'Infinity' | '+Infinity' | '-Infinity';
4445

4546
nil
4647
: 'null';
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
2+
(defn -qc [q]
3+
(cond (int? q) q ;; base case, quantity should always be an integer
4+
(nil? q) 0
5+
(fail :ARGUMENT "Invalid quantity")))
6+
7+
;; Initial token supply
8+
(def supply 1000000000000000000)
9+
10+
(set-holding *caller* supply)
11+
12+
;; Map of holder-address -> {offeree-address -> positive integer amount}
13+
;; Must enforce valid positive offers
14+
;;
15+
(def offers {})
16+
17+
;; Functions of the interface described in the `convex.asset` library
18+
(defn accept
19+
^:callable
20+
[sender quantity]
21+
(let [sender (address sender)
22+
quantity (-qc quantity)
23+
om (get offers sender) ;; if this is nil then OK, behaves as empty map
24+
sendbal (or (get-holding sender)
25+
0)
26+
offer (get om *caller* 0)]
27+
(cond
28+
(< quantity 0)
29+
(fail "Can't accept a negative quantity of fungible tokens.")
30+
31+
(< offer quantity)
32+
(fail :STATE "Offer is insufficient")
33+
34+
(< sendbal quantity)
35+
(fail :FUNDS "Sender token balance is insufficient")
36+
37+
(let [new-offer (- offer
38+
quantity)]
39+
(def offers
40+
(assoc offers
41+
sender
42+
(if (> new-offer
43+
0)
44+
(assoc om
45+
*caller*
46+
new-offer)
47+
(dissoc om *caller*))))
48+
(set-holding sender
49+
(- sendbal
50+
quantity))
51+
(set-holding *caller*
52+
(+ (or (get-holding *caller*)
53+
0)
54+
quantity))
55+
quantity))))
56+
57+
(defn balance
58+
^{:callable true}
59+
[addr]
60+
(or (get-holding addr) 0))
61+
62+
;; No restrictions on transfer by default.
63+
;;
64+
(defn check-transfer
65+
^:callable
66+
[_sender _receiver _quantity]
67+
nil)
68+
69+
(defn decimals
70+
^:callable
71+
[]
72+
~decimals)
73+
74+
(defn total-supply
75+
^:callable
76+
[]
77+
supply)
78+
79+
(defn direct-transfer
80+
^{:callable true}
81+
[addr amount data]
82+
(let [addr (address addr)
83+
amount (-qc amount)
84+
bal (or (get-holding *caller*) 0)
85+
tbal (or (get-holding addr) 0)]
86+
87+
;; Amount must be in valid range.
88+
(cond (< amount 0) (fail :ARGUMENT "negative transfer"))
89+
(cond (> amount bal) (fail :FUNDS "insufficent token balance"))
90+
91+
;; Need this in case of self-transfers.
92+
(when (= *caller* addr)
93+
(log "TR" *caller* addr amount bal bal data)
94+
(return amount))
95+
96+
97+
(let [nsb (- bal amount)
98+
nrb (+ tbal amount)]
99+
(log "TR" *caller* addr amount nsb nrb data)
100+
(set-holding *caller* nsb)
101+
(set-holding addr nrb))
102+
))
103+
104+
(defn get-offer
105+
^{:callable true}
106+
[sender receiver]
107+
(get-in offers [sender receiver] 0))
108+
109+
(defn offer
110+
^{:callable true}
111+
[receiver quantity]
112+
(let [receiver (address receiver)
113+
quantity (-qc quantity)]
114+
(if (<= quantity 0)
115+
(set! offers (dissoc-in offers [*caller* receiver])) ;; dissoc if no positive offer
116+
(set! offers (assoc-in offers [*caller* receiver] quantity)))
117+
quantity))
118+
119+
(defn quantity-add
120+
^{:callable true}
121+
[a b]
122+
(let [a (cond a (int a) 0)
123+
b (cond b (int b) 0)]
124+
(+ a b)))
125+
126+
(defn quantity-sub
127+
^{:callable true}
128+
[a b]
129+
(let [a (cond a (int a) 0)
130+
b (cond b (int b) 0)]
131+
(if (> a b)
132+
(- a b)
133+
0)))
134+
135+
(defn quantity-subset?
136+
^{:callable true}
137+
[a b]
138+
(<= (cond a (int a) 0)
139+
(cond b (int b) 0)))

convex-core/src/test/java/convex/core/utils/JSONUtilsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ public void testJSONComments() {
134134

135135
}
136136

137+
@Test
138+
public void testJSONDoubles() {
139+
assertEquals(CVMDouble.POSITIVE_INFINITY,JSONUtils.parse("Infinity"));
140+
assertEquals(CVMDouble.POSITIVE_INFINITY,JSONUtils.parse("+Infinity"));
141+
assertEquals(CVMDouble.NEGATIVE_INFINITY,JSONUtils.parse("-Infinity"));
142+
assertEquals(CVMDouble.NEGATIVE_INFINITY,JSONUtils.parse(" -Infinity"));
143+
assertEquals(CVMDouble.NaN,JSONUtils.parse(" NaN"));
144+
145+
assertThrows(ParseException.class,()->JSONUtils.parse("- Infinity")); // space between
146+
assertThrows(ParseException.class,()->JSONUtils.parse("Inf")); // not a JSON5 value
147+
assertThrows(ParseException.class,()->JSONUtils.parse("NAN")); // incorrect ccase
148+
149+
}
150+
137151
@Test
138152
public void testJSONRoundTrips() {
139153

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package lab;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.io.IOException;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import convex.core.cvm.Address;
13+
import convex.core.cvm.Context;
14+
import convex.core.data.ACell;
15+
import convex.core.data.AHashMap;
16+
import convex.core.data.ASet;
17+
import convex.core.data.AString;
18+
import convex.core.data.Strings;
19+
import convex.core.lang.ACVMTest;
20+
import convex.core.lang.TestState;
21+
import convex.core.util.Utils;
22+
import convex.lib.AssetTester;
23+
24+
public class PaisleyTest extends ACVMTest {
25+
26+
Address PAI;
27+
28+
@Override protected Context buildContext(Context ctx) {
29+
ctx=TestState.CONTEXT.fork();
30+
31+
ctx=exec(ctx,"(import convex.asset :as asset)");
32+
ctx=exec(ctx,"(import convex.trust :as trust)");
33+
ctx=exec(ctx,"(import convex.fungible :as fun)");
34+
35+
// User accounts for testing
36+
ctx=exec(ctx,"(def HERO *address*)");
37+
ctx=exec(ctx,"(def VILLAIN "+VILLAIN+")");
38+
39+
ctx=exec(ctx,"(def pai (deploy '(set-controller *caller*)))");
40+
PAI=ctx.getResult();
41+
42+
try {
43+
String code=Utils.readResourceAsString("/app/paisley/pai.cvx");
44+
ctx=exec(ctx,"(eval-as pai '(do "+code+"))");
45+
46+
47+
} catch (IOException e) {
48+
// TODO Auto-generated catch block
49+
e.printStackTrace();
50+
throw new Error(e);
51+
}
52+
return ctx;
53+
}
54+
55+
@Test public void testPAIToken() {
56+
}
57+
}

0 commit comments

Comments
 (0)