Skip to content

Commit

Permalink
[1] return value from `getTallyDollars(..), getTally(..), getTransact…
Browse files Browse the repository at this point in the history
…ions(..)` changed to return wrappers `{'tally':.., 'transactions':.., 'as-of';..}

[2] normalization now done by services to save on API rates
[3] fix bug with ohledger imparters whereby only PROD Stripe API was being called.
  • Loading branch information
Jakub Ner committed Apr 25, 2021
1 parent f4e23ec commit 26e0f31
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 60 deletions.
2 changes: 1 addition & 1 deletion dist/ledgers.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ledgers.js.map

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions docs/ledgers.js-rendered-docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ <h3 class='fl m0' id='getfromdollars'>
</div>


<p>Retrieve a converted amount in imparter specific denomination from a provided dollar amount.</p>
<p>Retrieve a (highest) converted amount in imparter specific denomination from a provided dollar amount at a very recent exchange rate.</p>

<div class='pre p1 fill-light mt0'>getFromDollars(imparterTag: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, dollarAmount: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></div>

Expand Down Expand Up @@ -1883,8 +1883,11 @@ <h3 class='fl m0' id='gettallydollars'>

<div class='py1 quiet mt1 prose-big'>Returns</div>
<code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code>:
with the tally value in US dollars: all transactions are exchanged to USD at an approximate exchange rate
close to the transactions' time.
with the
<code>{'tally':.., 'as-of':..}</code>
object, whereby the 'tally' value is in US dollars: all transactions
are exchanged to USD at an approximate (highest) exchange rate close to the transactions' time. The 'as-of' timestamp
is that of the call (pass this to back-end to retrieve cached values at better API call rates).



Expand Down Expand Up @@ -1996,7 +1999,10 @@ <h3 class='fl m0' id='gettally'>

<div class='py1 quiet mt1 prose-big'>Returns</div>
<code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code>:
with the tally value in imparter specific currency
with the
<code>{'tally':.., 'as-of':..}</code>
object, whereby the tally value is in imparter specific currency.
The 'as-of' timestamp is that of the call (pass this to back-end to retrieve cached values at better API call rates).



Expand Down Expand Up @@ -2109,8 +2115,10 @@ <h3 class='fl m0' id='gettransactions'>

<div class='py1 quiet mt1 prose-big'>Returns</div>
<code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code>:
with the transactions:
<code>[{"transaction-value":..,"transaction-date":..},..]</code>
with the
<code>{'transactions': [{"transaction-value":..,"transaction-date":..},..], 'as-of':..}</code>
object,
whereby 'transactions' is the list of transactions and 'as-of' is the timestamp of the call.



Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ledgers.js",
"version": "4.1.0",
"version": "4.2.0",
"description": "JavaScript library for ledger-based authorizations :: abstracting different ledgers to be used in an application's authentication and authorization workflows.",
"engines": {
"node": ">=10.13.0"
Expand Down
8 changes: 6 additions & 2 deletions src/fns/imparter_fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ class imparter_fns {
if (date && !(date instanceof Date)) throw new Error("'date' must be a Date is passed in");
if (!('address' in recipient) || !recipient.address) throw new Error("'address' required in recipient"); }

static async getTxs_retrieve(uri, from, to, tallyOnly, date, token, __fetch) {
static async getTxs_retrieve(uri, from, to, tallyOnly, tallyDollars, date, token, __fetch) {
if (!uri) throw new Error('no uri for request, unsupported network selected in wallet?');
let since = '';
if (date) {
since = `&since=${date.toISOString()}`;
}
return await __fetch(`${uri}/get-transactions/${from}/${to}?tally-only=${tallyOnly ? 'true' : 'false'}${since}&include-refunds=true`, {
let dollarsQuery = '';
if (tallyDollars) {
dollarsQuery = `&tally-dollars=true`
}
return await __fetch(`${uri}/get-transactions/${from}/${to}?tally-only=${tallyOnly ? 'true' : 'false'}${dollarsQuery}${since}&include-refunds=true`, {
headers: new Headers({
'Authorization': `Bearer ${token}`
})
Expand Down
22 changes: 2 additions & 20 deletions src/imparters/eth-web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,7 @@ class eth_web3 {
return dollarAmount / result[0].minrate;
}

async getTallyDollars(recipient, date) {
const txs = (await this.getTxs(recipient, date, false)).transactions;
if (!txs || txs.length == 0) return 0;
const values = txs.map(t => `${t['transaction-value']}@${(new Date(t['transaction-date'])).toISOString()}`);
const hostPrefix = this.web3_wallet.network === 'main' ? '' : 'test.';
const now = (new Date()).toISOString();
var tally = await this.__fetch(`https://${hostPrefix}rates.overhide.io/tallymax/wei/${values.join(',')}`, {
headers: new Headers({
'Authorization': `Bearer ${this.getToken()}`
})
})
.then(res => res.text())
.catch(e => {
throw String(e)
});
return (Math.round(tally * 100) / 100).toFixed(2);
}

async getTxs(recipient, date, tallyOnly) {
async getTxs(recipient, date, tallyOnly, tallyDollars) {
imparter_fns.getTxs_check_details(recipient, date);

const to = recipient.address;
Expand All @@ -102,7 +84,7 @@ class eth_web3 {
if (!this.web3_wallet.walletAddress) throw new Error("from 'walletAddress' not set: use wallet");
var from = this.web3_wallet.walletAddress;

return await imparter_fns.getTxs_retrieve(uri, from, to, tallyOnly, date, this.getToken(), this.__fetch);
return await imparter_fns.getTxs_retrieve(uri, from, to, tallyOnly, tallyDollars, date, this.getToken(), this.__fetch);
}

async isOnLedger() {
Expand Down
9 changes: 2 additions & 7 deletions src/imparters/ohledger-web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ class ohledger_web3 {
return dollarAmount * 100;
}

async getTallyDollars(recipient, date) {
var tally = (await this.getTxs(recipient, date, true)).tally;
return (tally / 100).toFixed(2);
}

async getTxs(recipient, date, tallyOnly) {
async getTxs(recipient, date, tallyOnly, tallyDollars) {
imparter_fns.getTxs_check_details(recipient, date);

const to = recipient.address;
Expand All @@ -74,7 +69,7 @@ class ohledger_web3 {
if (!this.web3_wallet.walletAddress) throw new Error("from 'walletAddress' not set: use wallet");
var from = this.web3_wallet.walletAddress;

return await imparter_fns.getTxs_retrieve(uri, from, to, tallyOnly, date, this.getToken(), this.__fetch);
return await imparter_fns.getTxs_retrieve(uri, from, to, tallyOnly, tallyDollars, date, this.getToken(), this.__fetch);
}

async isOnLedger() {
Expand Down
9 changes: 2 additions & 7 deletions src/imparters/ohledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ class ohledger {
return dollarAmount * 100;
}

async getTallyDollars(recipient, date) {
var tally = (await this.getTxs(recipient, date, true)).tally;
return (tally / 100).toFixed(2);
}

async getTxs(recipient, date, tallyOnly) {
async getTxs(recipient, date, tallyOnly, tallyDollars) {
imparter_fns.getTxs_check_details(recipient, date);

const to = recipient.address;
Expand All @@ -95,7 +90,7 @@ class ohledger {
if (!this.address) throw new Error("from 'address' not set: use setCredentials");
const from = this.address;

return await imparter_fns.getTxs_retrieve(uri, from, to, tallyOnly, date, this.getToken(), this.__fetch);
return await imparter_fns.getTxs_retrieve(uri, from, to, tallyOnly, tallyDollars, date, this.getToken(), this.__fetch);
}

async isOnLedger() {
Expand Down
26 changes: 13 additions & 13 deletions src/ledgers.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ const oh$ = (function() {
* @namespace oh$
* @function getFromDollars
* @description
* Retrieve a converted amount in imparter specific denomination from a provided dollar amount.
* Retrieve a (highest) converted amount in imparter specific denomination from a provided dollar amount at a very recent exchange rate.
* @param {string} imparterTag
* @param {number} dollarAmount - the dollar amount.
* @returns {Promise} with the value in imparter specific currency at the present time (based on recent exchange rate).
Expand All @@ -455,8 +455,9 @@ const oh$ = (function() {
* > | ohledger-web3 | `{address:..}` |
*
* @param {Date} since - date to start tally since: date of oldest transaction to include. No restriction if 'null'.
* @returns {Promise} with the tally value in US dollars: all transactions are exchanged to USD at an approximate exchange rate
* close to the transactions' time.
* @returns {Promise} with the `{'tally':.., 'as-of':..}` object, whereby the 'tally' value is in US dollars: all transactions
* are exchanged to USD at an approximate (highest) exchange rate close to the transactions' time. The 'as-of' timestamp
* is that of the call (pass this to back-end to retrieve cached values at better API call rates).
*/
getTallyDollars = getTallyDollars;

Expand All @@ -477,7 +478,8 @@ const oh$ = (function() {
* > | ohledger-web3 | `{address:..}` |
*
* @param {Date} since - date to start tally since: date of oldest transaction to include. No restriction if 'null'.
* @returns {Promise} with the tally value in imparter specific currency
* @returns {Promise} with the `{'tally':.., 'as-of':..}` object, whereby the tally value is in imparter specific currency.
* The 'as-of' timestamp is that of the call (pass this to back-end to retrieve cached values at better API call rates).
*/
getTally = getTally;

Expand All @@ -499,7 +501,8 @@ const oh$ = (function() {
* > | ohledger | `{address:..}` |
* > | ohledger-web3 | `{address:..}` |
*
* @returns {Promise} with the transactions: `[{"transaction-value":..,"transaction-date":..},..]`
* @returns {Promise} with the `{'transactions': [{"transaction-value":..,"transaction-date":..},..], 'as-of':..}` object,
* whereby 'transactions' is the list of transactions and 'as-of' is the timestamp of the call.
*/
getTransactions = getTransactions;

Expand Down Expand Up @@ -703,25 +706,22 @@ const oh$ = (function() {
}

async function getTallyDollars(imparterTag, recipient, date) {
if (!imparterTag in imparters) throw new Error("invalid imparterTag");
if (await isEnabled && !__fetch) throw new Error('did you forget to `oh$.enable(..)`?');

return await imparters[imparterTag].getTallyDollars(recipient, date);
return (await getTxs(imparterTag, recipient, date, true, true));
}

async function getTally(imparterTag, recipient, date) {
return (await getTxs(imparterTag, recipient, date, true)).tally;
return (await getTxs(imparterTag, recipient, date, true, false));
}

async function getTransactions(imparterTag, recipient, date) {
return (await getTxs(imparterTag, recipient, date, false)).transactions;
return (await getTxs(imparterTag, recipient, date, false, false));
}

async function getTxs(imparterTag, recipient, date, tallyOnly) {
async function getTxs(imparterTag, recipient, date, tallyOnly, tallyDollars) {
if (!imparterTag in imparters) throw new Error("invalid imparterTag");
if (await isEnabled && !__fetch) throw new Error('did you forget to `oh$.enable(..)`?');

return await imparters[imparterTag].getTxs(recipient, date, tallyOnly);
return await imparters[imparterTag].getTxs(recipient, date, tallyOnly, tallyDollars);
}

async function isOnLedger(imparterTag) {
Expand Down
6 changes: 4 additions & 2 deletions src/wallets/overhide_wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ class overhide_wallet {
loadOhLedgerTransactFns() {
// load prod ohledger transact fn
this.loadJS(`${this.remuneration_uri.prod}/transact.js`, () => {
this.oh_ledger_transact_fn.prod = (...args) => { oh_ledger_transact(...args); return this.setupNewPromise(); }
const fn = oh_ledger_transact;
this.oh_ledger_transact_fn.prod = (...args) => { fn(...args); return this.setupNewPromise(); }
}, document.body);

// load test ohledger transact fn
this.loadJS(`${this.remuneration_uri.test}/transact.js`, () => {
this.oh_ledger_transact_fn.test = (...args) => { oh_ledger_transact(...args); return this.setupNewPromise(); }
const fn = oh_ledger_transact;
this.oh_ledger_transact_fn.test = (...args) => { fn(...args); return this.setupNewPromise(); }
}, document.body);
}
}
Expand Down

0 comments on commit 26e0f31

Please sign in to comment.