Skip to content

Commit bc1d884

Browse files
authored
docs: added examples (#88)
1 parent 559426b commit bc1d884

18 files changed

+517
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
vars {
2+
number $x = 10 + 1
3+
}
4+
5+
send [USD/2 10] (
6+
source = @world
7+
destination = @alice
8+
)
9+
10+
set_tx_meta("key", $x/2)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json",
3+
"featureFlags": [
4+
"experimental-overdraft-function",
5+
"experimental-mid-script-function-call",
6+
"experimental-oneof"
7+
],
8+
"testCases": [
9+
{
10+
"it": "sets the expected meta",
11+
"expect.txMetadata": {
12+
"key": "11/2"
13+
}
14+
}
15+
]
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
vars {
2+
number $amt
3+
}
4+
5+
send [USD $amt] (
6+
source = oneof {
7+
@s1
8+
@s2
9+
{
10+
@s1
11+
@s2
12+
}
13+
}
14+
destination = @dest
15+
)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json",
3+
"variables": {
4+
"amt": "10"
5+
},
6+
"featureFlags": ["experimental-oneof"],
7+
"testCases": [
8+
{
9+
"it": "sends from the first source when there is enough balance",
10+
"balances": {
11+
"s1": {
12+
"USD": 999
13+
}
14+
},
15+
"expect.postings": [
16+
{
17+
"source": "s1",
18+
"destination": "dest",
19+
"amount": 10,
20+
"asset": "USD"
21+
}
22+
]
23+
},
24+
{
25+
"it": "sends from the second one when it has enough balance but the first one doesn't",
26+
"balances": {
27+
"s1": {
28+
"USD": 9
29+
},
30+
"s2": {
31+
"USD": 999
32+
}
33+
},
34+
"expect.postings": [
35+
{
36+
"source": "s2",
37+
"destination": "dest",
38+
"amount": 10,
39+
"asset": "USD"
40+
}
41+
]
42+
},
43+
{
44+
"it": "sends partially from both when none of them has enough balance on its own",
45+
"balances": {
46+
"s1": {
47+
"USD": 6
48+
},
49+
"s2": {
50+
"USD": 9
51+
}
52+
},
53+
"expect.postings": [
54+
{
55+
"source": "s1",
56+
"destination": "dest",
57+
"amount": 6,
58+
"asset": "USD"
59+
},
60+
{
61+
"source": "s2",
62+
"destination": "dest",
63+
"amount": 4,
64+
"asset": "USD"
65+
}
66+
]
67+
},
68+
{
69+
"it": "fails if there aren't enough funds between all sources",
70+
"balances": {
71+
"s1": {
72+
"USD": 5
73+
},
74+
"s2": {
75+
"USD": 4
76+
}
77+
},
78+
"expect.missingFunds": true
79+
}
80+
]
81+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
vars {
2+
number $amt
3+
}
4+
5+
6+
send [EUR $amt] (
7+
source = @world
8+
destination = oneof {
9+
max overdraft(@alice, EUR) to @alice
10+
max overdraft(@bob, EUR) to @bob
11+
remaining kept
12+
}
13+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json",
3+
"featureFlags": [
4+
"experimental-overdraft-function",
5+
"experimental-mid-script-function-call",
6+
"experimental-oneof"
7+
],
8+
"variables": {
9+
"amt": "100"
10+
},
11+
"testCases": [
12+
{
13+
"it": "should be a noop when all balances are >= 0",
14+
"balances": { "alice": { "EUR": 100 }, "bob": { "EUR": 200 } },
15+
"expect.postings": []
16+
},
17+
{
18+
"it": "should prioritize alice when both have missing funds",
19+
"balances": {
20+
"alice": { "EUR": -120 },
21+
"bob": { "EUR": -120 }
22+
},
23+
"expect.postings": [
24+
{
25+
"source": "world",
26+
"destination": "alice",
27+
"amount": 100,
28+
"asset": "EUR"
29+
}
30+
]
31+
},
32+
{
33+
"it": "doesn't send funds to alice if there aren't enough funds for the account to be topped-up",
34+
"balances": {
35+
"alice": { "EUR": -80 },
36+
"bob": { "EUR": -120 }
37+
},
38+
"expect.postings": [
39+
{
40+
"source": "world",
41+
"destination": "bob",
42+
"amount": 100,
43+
"asset": "EUR"
44+
}
45+
]
46+
},
47+
{
48+
"it": "funds are kept if there are spare funds",
49+
"balances": {
50+
"alice": { "EUR": -10 },
51+
"bob": { "EUR": -20 }
52+
},
53+
"expect.postings": []
54+
}
55+
]
56+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vars {
2+
monetary $alice_overdraft = overdraft(@alice, EUR)
3+
}
4+
5+
send $alice_overdraft (
6+
source = @world
7+
destination = @alice
8+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json",
3+
"featureFlags": ["experimental-overdraft-function"],
4+
"testCases": [
5+
{
6+
"it": "should not emit postings",
7+
"balances": { "alice": { "EUR": 100 } },
8+
"expect.postings": []
9+
},
10+
{
11+
"it": "should send the missing amount to an overdraft account",
12+
"balances": { "alice": { "EUR": -100 } },
13+
"expect.volumes": {
14+
"alice": { "EUR": 0 },
15+
"world": { "EUR": -100 }
16+
},
17+
"expect.movements": {
18+
"world": {
19+
"alice": { "EUR": 100 }
20+
}
21+
},
22+
"expect.postings": [
23+
{
24+
"source": "world",
25+
"destination": "alice",
26+
"amount": 100,
27+
"asset": "EUR"
28+
}
29+
]
30+
}
31+
]
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
vars {
2+
number $amt
3+
account $dest
4+
monetary $bank_overdraft = overdraft(@bank_account, EUR)
5+
}
6+
7+
send [EUR $amt] (
8+
source = max $bank_overdraft from @wallet
9+
destination = $dest
10+
)
11+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/formancehq/numscript/main/specs.schema.json",
3+
"featureFlags": [
4+
"experimental-overdraft-function",
5+
"experimental-mid-script-function-call",
6+
"experimental-oneof"
7+
],
8+
"variables": {
9+
"amt": "100",
10+
"dest": "alice"
11+
},
12+
"testCases": [
13+
{
14+
"it": "should authorize transfer if both wallet and bank accounts display enough balance",
15+
"balances": {
16+
"wallet": { "EUR": 100 },
17+
"bank_account": { "EUR": -100 }
18+
},
19+
"expect.postings": [
20+
{
21+
"source": "wallet",
22+
"destination": "alice",
23+
"amount": 100,
24+
"asset": "EUR"
25+
}
26+
]
27+
},
28+
{
29+
"it": "should not authorize transfer if wallet does not display enough balance and bank account does",
30+
"balances": {
31+
"wallet": { "EUR": 50 },
32+
"bank_account": { "EUR": -100 }
33+
},
34+
"expect.missingFunds": true
35+
},
36+
{
37+
"it": "should not authorize transfer if bank account does not display enough balance and wallet does",
38+
"balances": {
39+
"wallet": { "EUR": 100 },
40+
"bank_account": { "EUR": -50 }
41+
},
42+
"expect.missingFunds": true
43+
}
44+
]
45+
}

0 commit comments

Comments
 (0)