Skip to content

Commit

Permalink
feat(delegated payments): add list-approvals command rather than over…
Browse files Browse the repository at this point in the history
…loaded balance command PE-6754
  • Loading branch information
fedellen committed Oct 25, 2024
1 parent 79fe7a0 commit ee44ef6
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import {
uploadFile,
uploadFolder,
} from './commands/index.js';
import { listApprovals } from './commands/listApprovals.js';
import { revokeApprovals } from './commands/revokeApprovals.js';
import {
createApprovalOptions,
globalOptions,
listApprovalsOptions,
optionMap,
revokeApprovalsOptions,
uploadFileOptions,
Expand Down Expand Up @@ -116,6 +118,17 @@ applyOptions(
await runCommand(command, revokeApprovals);
});

applyOptions(
program
.command('list-approvals')
.description(
'Lists all Turbo delegated payment approvals for given address or wallet',
),
listApprovalsOptions,
).action(async (_commandOptions, command: Command) => {
await runCommand(command, listApprovals);
});

if (
process.argv[1].includes('bin/turbo') || // Running from global .bin
process.argv[1].includes('cli/cli') // Running from source
Expand Down
21 changes: 11 additions & 10 deletions src/cli/commands/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BigNumber } from 'bignumber.js';

import { TurboFactory } from '../../node/factory.js';
import { wincPerCredit } from '../constants.js';
import { AddressOptions } from '../types.js';
import { addressOrPrivateKeyFromOptions, configFromOptions } from '../utils.js';

export async function balance(options: AddressOptions) {
const config = configFromOptions(options);
const { address, privateKey } = await addressOrPrivateKeyFromOptions(options);

const { winc, givenApprovals, receivedApprovals, nativeAddress } =
const { effectiveBalance, nativeAddress, winc, controlledWinc } =
await (async () => {
if (address !== undefined) {
return {
Expand All @@ -43,16 +46,14 @@ export async function balance(options: AddressOptions) {
})();

console.log(
`Turbo Balance for Native Address "${nativeAddress}"\nCredits: ${
+winc / 1_000_000_000_000
}${
givenApprovals?.length > 0
? `\nGiven Approvals:\n${JSON.stringify(givenApprovals, null, 2)}`
: ''
`Turbo Balance for Native Address "${nativeAddress}"\nEffective Credits: ${
+effectiveBalance / wincPerCredit
}${
receivedApprovals?.length > 0
? `\nReceived Approvals:\n${JSON.stringify(receivedApprovals, null, 2)}`
: ''
winc === controlledWinc
? ''
: `\nCredits Shared to Other Wallets: ${BigNumber(controlledWinc)
.minus(winc)
.div(wincPerCredit)}`
}`,
);
}
4 changes: 3 additions & 1 deletion src/cli/commands/createApproval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ export async function createApproval(
expiresBySeconds,
});

console.log('Created approval:', JSON.stringify(result, null, 2));
console.log(
JSON.stringify({ message: 'Created approval:', ...result }, null, 2),
);
}
3 changes: 2 additions & 1 deletion src/cli/commands/cryptoFund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import prompts from 'prompts';

import { tokenToBaseMap } from '../../common/index.js';
import { TurboFactory } from '../../node/factory.js';
import { wincPerCredit } from '../constants.js';
import { CryptoFundOptions } from '../types.js';
import {
configFromOptions,
Expand Down Expand Up @@ -54,7 +55,7 @@ export async function cryptoFund(options: CryptoFundOptions) {
const { winc } = await turbo.getWincForToken({ tokenAmount });
const targetWallet = (await turbo.getTurboCryptoWallets())[token];

const credits = (+winc / 1_000_000_000_000).toFixed(12);
const credits = (+winc / wincPerCredit).toFixed(12);

const { confirm } = await prompts({
type: 'confirm',
Expand Down
67 changes: 67 additions & 0 deletions src/cli/commands/listApprovals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TurboFactory } from '../../node/factory.js';
import { ListApprovalsOptions } from '../types.js';
import { addressOrPrivateKeyFromOptions, configFromOptions } from '../utils.js';

export async function listApprovals(
options: ListApprovalsOptions,
): Promise<void> {
const config = configFromOptions(options);
const { address, privateKey } = await addressOrPrivateKeyFromOptions(options);

const { givenApprovals, receivedApprovals, nativeAddress } =
await (async () => {
if (address !== undefined) {
const approvals = await TurboFactory.unauthenticated(
config,
).getDelegatedPaymentApprovals({
userAddress: address,
});
return { ...approvals, nativeAddress: address };
}
if (privateKey === undefined) {
throw new Error('Must provide an (--address) or use a valid wallet');
}
const turbo = TurboFactory.authenticated({
...config,
privateKey,
});
const approvals = await turbo.getDelegatedPaymentApprovals();
return {
...approvals,
nativeAddress: await turbo.signer.getNativeAddress(),
};
})();

const hasApprovals =
givenApprovals?.length === 0 && receivedApprovals?.length === 0;
const body = {
message:
`${hasApprovals ? 'No approvals found ' : 'Approvals found'}` +
` for native address '${nativeAddress}'`,
givenApprovals,
receivedApprovals,
};

if (givenApprovals?.length > 0) {
body['givenApprovals'] = givenApprovals;
}
if (receivedApprovals?.length > 0) {
body['receivedApprovals'] = receivedApprovals;
}
console.log(JSON.stringify(body, null, 2));
}
3 changes: 2 additions & 1 deletion src/cli/commands/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { currencyMap } from '../../common/currency.js';
import { isTokenType, tokenToBaseMap } from '../../common/index.js';
import { TurboFactory } from '../../node/factory.js';
import { fiatCurrencyTypes, isCurrency, tokenTypes } from '../../types.js';
import { wincPerCredit } from '../constants.js';
import { PriceOptions } from '../types.js';
import { configFromOptions } from '../utils.js';

Expand Down Expand Up @@ -63,7 +64,7 @@ export async function price(options: PriceOptions) {

console.log(
`Current price estimate for ${value} ${type} is ~${(
+winc / 1_000_000_000_000
+winc / wincPerCredit
).toFixed(12)} Credits`,
);
}
6 changes: 4 additions & 2 deletions src/cli/commands/revokeApprovals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function revokeApprovals(
const { address: revokedAddress } = options;
if (revokedAddress === undefined) {
throw new Error(
'Must provide an approved --address to create approval for',
'Must provide an approved --address to revoke approvals for',
);
}

Expand All @@ -32,5 +32,7 @@ export async function revokeApprovals(
revokedAddress,
});

console.log('Created approval:', JSON.stringify(result, null, 2));
console.log(
JSON.stringify({ message: 'Revoked approvals', ...result }, null, 2),
);
}
2 changes: 2 additions & 0 deletions src/cli/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ export const turboCliTags: { name: string; value: string }[] = [
{ name: 'App-Version', value: version },
{ name: 'App-Platform', value: process.platform },
];

export const wincPerCredit = 1_000_000_000_000;
2 changes: 2 additions & 0 deletions src/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,5 @@ export const createApprovalOptions = [
];

export const revokeApprovalsOptions = [...walletOptions, optionMap.address];

export const listApprovalsOptions = revokeApprovalsOptions;
2 changes: 2 additions & 0 deletions src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ export type CreateApprovalOptions = WalletOptions & {
export type RevokeApprovalsOptions = WalletOptions & {
address: string | undefined;
};

export type ListApprovalsOptions = RevokeApprovalsOptions;

0 comments on commit ee44ef6

Please sign in to comment.