1
//! Account-domain natives. Wraps `server::command::{CreateAccount, ListAccounts,
2
//! ListAccountsForManage, GetAccountForManage, SetAccountTag, GetAccount,
3
//! GetAccountCommodities, GetBalance, GetBalances, MergeAccounts}` as nomiscript-callable host fns.
4

            
5
use scripting::runtime::{alloc_commodity_ref, alloc_ratio_ref, alloc_string_ref, read_string_arg};
6
use server::command::account::{ListAccounts, ListAccountsForManage};
7
use wasmtime::{ArrayRef, Linker, Rooted};
8

            
9
use crate::natives::native::register_native;
10
use crate::session::SessionData;
11

            
12
mod balance;
13
pub(crate) mod create;
14
mod list;
15
mod merge;
16
#[cfg(test)]
17
mod tests;
18
#[cfg(test)]
19
mod tests_balance;
20
#[cfg(test)]
21
mod tests_lookup;
22

            
23
pub const REGISTERED_COMMANDS: &[&str] = &[
24
    "create-account",
25
    "merge-accounts",
26
    "list-accounts",
27
    "list-accounts-for-manage",
28
    "get-account-for-manage",
29
    "set-account-tag",
30
    "clear-account-tag",
31
    "get-account",
32
    "get-account-commodities",
33
    "get-balance",
34
    "get-balances",
35
];
36

            
37
230200
pub fn register(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
38
230200
    register_readonly(linker)?;
39
230200
    register_unlisted(linker)?;
40
230200
    register_mutators(linker)?;
41
230200
    Ok(())
42
230200
}
43

            
44
236811
pub fn register_readonly(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
45
236811
    register_native(linker, "account_list_accounts", |caller, ()| {
46
2240
        Box::pin(async move {
47
2240
            let user_id = caller.data().ctx().user_id;
48
2240
            let result = ListAccounts::new().user_id(user_id).run().await;
49
2240
            let entries = list::list_account_entries("list-accounts", result)?;
50
2240
            list::alloc_account_chain(caller, entries).await
51
2240
        })
52
2240
    })?;
53
236811
    register_native(
54
236811
        linker,
55
236811
        "account_get_account",
56
16062
        |caller, (key_arg,): (Option<Rooted<ArrayRef>>,)| {
57
16062
            Box::pin(async move {
58
16062
                let user_id = caller.data().ctx().user_id;
59
16062
                let key = read_string_arg(caller, key_arg)?;
60
16062
                list::run_get_account(caller, user_id, key).await
61
16062
            })
62
16062
        },
63
    )?;
64
236811
    register_native(
65
236811
        linker,
66
236811
        "account_get_balance",
67
162
        |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
68
162
            Box::pin(async move {
69
162
                let user_id = caller.data().ctx().user_id;
70
162
                let id = read_string_arg(caller, id_arg)?;
71
162
                let (numer, denom) = balance::run_get_balance_single(user_id, id).await?;
72
160
                Ok(Some(alloc_ratio_ref(caller, numer, denom)?))
73
162
            })
74
162
        },
75
    )?;
76
236811
    register_native(
77
236811
        linker,
78
236811
        "account_get_account_commodities",
79
160
        |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
80
160
            Box::pin(async move {
81
160
                let user_id = caller.data().ctx().user_id;
82
160
                let id = read_string_arg(caller, id_arg)?;
83
160
                list::run_get_account_commodities(caller, user_id, id).await
84
160
            })
85
160
        },
86
    )?;
87
236811
    register_native(linker, "account_list_accounts_for_manage", |caller, ()| {
88
160
        Box::pin(async move {
89
160
            let user_id = caller.data().ctx().user_id;
90
160
            let result = ListAccountsForManage::new().user_id(user_id).run().await;
91
160
            let entries = list::list_account_entries("list-accounts-for-manage", result)?;
92
160
            list::alloc_account_chain(caller, entries).await
93
160
        })
94
160
    })?;
95
236811
    register_native(
96
236811
        linker,
97
236811
        "account_get_account_for_manage",
98
320
        |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
99
320
            Box::pin(async move {
100
320
                let user_id = caller.data().ctx().user_id;
101
320
                let id = read_string_arg(caller, id_arg)?;
102
320
                list::run_get_account_for_manage(caller, user_id, id).await
103
320
            })
104
320
        },
105
    )?;
106
236811
    register_native(linker, "account_account_count", |caller, ()| {
107
160
        Box::pin(async move {
108
160
            let user_id = caller.data().ctx().user_id;
109
160
            balance::count_accounts(user_id).await
110
160
        })
111
160
    })?;
112
236811
    register_native(
113
236811
        linker,
114
236811
        "account_account_balance",
115
2400
        |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
116
2400
            Box::pin(async move {
117
2400
                let user_id = caller.data().ctx().user_id;
118
2400
                let id = read_string_arg(caller, id_arg)?;
119
2400
                let (numer, denom, commodity_id) = balance::resolve_balance(user_id, id).await?;
120
1920
                let ref_ = alloc_commodity_ref(caller, numer, denom, commodity_id).await?;
121
1920
                Ok(Some(ref_))
122
2400
            })
123
2400
        },
124
    )?;
125
236811
    Ok(())
126
236811
}
127

            
128
/// Reads that are not on the render allowlist; only the full linker binds them.
129
230200
pub fn register_unlisted(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
130
230200
    register_native(
131
230200
        linker,
132
230200
        "account_get_balances",
133
1760
        |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
134
1760
            Box::pin(async move {
135
1760
                let user_id = caller.data().ctx().user_id;
136
1760
                let id = read_string_arg(caller, id_arg)?;
137
1760
                let lines = balance::run_get_balances(user_id, id).await?;
138
1600
                crate::natives::alloc_string_pair_chain(caller, lines).await
139
1760
            })
140
1760
        },
141
    )?;
142
230200
    Ok(())
143
230200
}
144

            
145
230200
pub fn register_mutators(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
146
230200
    register_native(
147
230200
        linker,
148
230200
        "account_set_account_tag",
149
644
        |caller, (id_arg, name_arg, value_arg): super::StringArgTriple| {
150
644
            Box::pin(async move {
151
644
                let user_id = caller.data().ctx().user_id;
152
644
                let id = read_string_arg(caller, id_arg)?;
153
644
                let name = read_string_arg(caller, name_arg)?;
154
644
                let value = read_string_arg(caller, value_arg)?;
155
644
                let (done, notice) = create::run_set_account_tag(user_id, id, name, value).await?;
156
162
                notice
157
162
                    .into_iter()
158
1
                    .for_each(|n| caller.data().push_notice(n));
159
162
                Ok(done)
160
644
            })
161
644
        },
162
    )?;
163
230200
    register_native(
164
230200
        linker,
165
230200
        "account_clear_account_tag",
166
        |caller, (id_arg, name_arg): (Option<Rooted<ArrayRef>>, Option<Rooted<ArrayRef>>)| {
167
            Box::pin(async move {
168
                let user_id = caller.data().ctx().user_id;
169
                let id = read_string_arg(caller, id_arg)?;
170
                let name = read_string_arg(caller, name_arg)?;
171
                create::run_clear_account_tag(user_id, id, name).await
172
            })
173
        },
174
    )?;
175
230200
    register_native(
176
230200
        linker,
177
230200
        "account_create_account",
178
11842
        |caller, (name_arg, parent_arg): (Option<Rooted<ArrayRef>>, Option<Rooted<ArrayRef>>)| {
179
11842
            Box::pin(async move {
180
11842
                let user_id = caller.data().ctx().user_id;
181
11842
                let name = read_string_arg(caller, name_arg)?;
182
11842
                let parent = read_string_arg(caller, parent_arg)?;
183
11842
                let (id, notice) = create::run_create_account(user_id, name, parent).await?;
184
11842
                notice
185
11842
                    .into_iter()
186
481
                    .for_each(|n| caller.data().push_notice(n));
187
11842
                Ok(Some(alloc_string_ref(caller, id.as_bytes())?))
188
11842
            })
189
11842
        },
190
    )?;
191
230200
    register_native(
192
230200
        linker,
193
230200
        "account_merge_accounts",
194
6
        |caller, (into_arg, from_arg): (Option<Rooted<ArrayRef>>, Option<Rooted<ArrayRef>>)| {
195
6
            Box::pin(async move {
196
6
                let user_id = caller.data().ctx().user_id;
197
6
                let into = read_string_arg(caller, into_arg)?;
198
6
                let from = read_string_arg(caller, from_arg)?;
199
6
                let id = merge::run_merge_accounts(user_id, into, from).await?;
200
2
                Ok(Some(alloc_string_ref(caller, id.as_bytes())?))
201
6
            })
202
6
        },
203
    )?;
204
230200
    Ok(())
205
230200
}
206

            
207
3850
pub(super) fn quote_string(s: &str) -> String {
208
3850
    let mut q = String::with_capacity(s.len() + 2);
209
3850
    q.push('"');
210
3850
    for ch in s.chars() {
211
61662
        match ch {
212
3
            '"' => q.push_str("\\\""),
213
1
            '\\' => q.push_str("\\\\"),
214
61658
            other => q.push(other),
215
        }
216
    }
217
3850
    q.push('"');
218
3850
    q
219
3850
}