MCPcopy Create free account
hub / github.com/ElementsProject/lightning / load_currencyrates

Function load_currencyrates

plugins/bkpr/bookkeeper.c:1950–2006  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1948}
1949
1950static void load_currencyrates(struct command *cmd,
1951 struct bkpr *bkpr)
1952{
1953 struct json_out *params;
1954 const jsmntok_t *reply, *ds, *t;
1955 const char *buf;
1956 size_t i;
1957
1958 params = json_out_new(NULL);
1959 json_out_start(params, NULL, '{');
1960 json_out_start(params, "key", '[');
1961 json_out_addstr(params, NULL, "bookkeeper");
1962 json_out_addstr(params, NULL, "currencyrate");
1963 json_out_addstr(params, NULL, bkpr->currency->name);
1964 json_out_end(params, ']');
1965 json_out_end(params, '}');
1966 json_out_finished(params);
1967
1968 reply = jsonrpc_request_sync(tmpctx, cmd, "listdatastore",
1969 take(params), &buf);
1970 ds = json_get_member(buf, reply, "datastore");
1971 json_for_each_arr(i, t, ds) {
1972 const jsmntok_t *data, *keytok = json_get_member(buf, t, "key");
1973 jsmntok_t ratetok, durationtok;
1974 u64 ts;
1975 struct currencyrate *crate;
1976
1977 if (keytok->size != 4)
1978 goto weird;
1979
1980 /* key = ["bookkeeper", "currencyrate", "USD", "timestamp"] */
1981 if (!json_to_u64(buf, keytok + 4, &ts))
1982 goto weird;
1983
1984 data = json_get_member(buf, t, "string");
1985 if (!data)
1986 goto weird;
1987
1988 if (!split_tok(buf, data, ':', &ratetok, &durationtok))
1989 goto weird;
1990 crate = tal(bkpr->currency_rates, struct currencyrate);
1991 if (!json_to_u64(buf, &ratetok, &crate->raw_rate)
1992 || !json_to_u32(buf, &durationtok, &crate->duration)) {
1993 tal_free(crate);
1994 goto weird;
1995 }
1996
1997 uintmap_add(bkpr->currency_rates, ts, crate);
1998 continue;
1999
2000 weird:
2001 plugin_log(cmd->plugin, LOG_BROKEN,
2002 "Unexpected datastore rate entry '%.*s'",
2003 json_tok_full_len(t),
2004 json_tok_full(buf, t));
2005 }
2006}
2007

Callers 1

option_currencyFunction · 0.85

Calls 14

json_out_newFunction · 0.85
json_out_startFunction · 0.85
json_out_addstrFunction · 0.85
json_out_endFunction · 0.85
json_out_finishedFunction · 0.85
json_get_memberFunction · 0.85
split_tokFunction · 0.85
tal_freeFunction · 0.85
jsonrpc_request_syncFunction · 0.50
json_to_u64Function · 0.50
json_to_u32Function · 0.50
plugin_logFunction · 0.50

Tested by

no test coverage detected