Skip to content

Commit

Permalink
refactor: use credit sharing in commands/methods/docs PE-6754
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Nov 1, 2024
1 parent d7a6f5e commit 1bcb4b7
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 132 deletions.
63 changes: 31 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ Welcome to the `@ardrive/turbo-sdk`! This SDK provides functionality for interac
- [Polygon (POL / MATIC) Crypto Top Up](#polygon-pol--matic-crypto-top-up)
- [Solana (SOL) Crypto Top Up](#solana-sol-crypto-top-up)
- [KYVE Crypto Top Up](#kyve-crypto-top-up)
- [`createDelegatedPaymentApproval({ approvedAddress, approvedWincAmount, expiresBySeconds })`](#createdelegatedpaymentapproval-approvedaddress-approvedwincamount-expiresbyseconds-)
- [`revokeDelegatedPaymentApprovals({ approvedAddress })`](#revokedelegatedpaymentapprovals-approvedaddress-)
- [`getDelegatedPaymentApprovals({ userAddress })`](#getdelegatedpaymentapprovals-useraddress-)
- [`shareCredits({ approvedAddress, approvedWincAmount, expiresBySeconds })`](#sharecredits-approvedaddress-approvedwincamount-expiresbyseconds-)
- [`revokeCredits({ approvedAddress })`](#revokecredits-approvedaddress-)
- [`getCreditShareApprovals({ userAddress })`](#getcreditshareapprovals-useraddress-)
- [CLI](#cli)
- [Install CLI](#install-cli)
- [CLI Usage](#cli-usage)
Expand All @@ -79,8 +79,8 @@ Welcome to the `@ardrive/turbo-sdk`! This SDK provides functionality for interac
- [`upload-folder`](#upload-folder)
- [`upload-file`](#upload-file)
- [`price`](#price)
- [`create-approval`](#create-approval)
- [`revoke-approvals`](#revoke-approvals)
- [`share-credits`](#share-credits)
- [`revoke-credits`](#revoke-credits)
- [`list-approvals`](#list-approvals)
- [Developers](#developers)
- [Requirements](#requirements)
Expand Down Expand Up @@ -683,36 +683,35 @@ const { winc, status, id, ...fundResult } = await turbo.topUpWithTokens({
});
```

#### `createDelegatedPaymentApproval({ approvedAddress, approvedWincAmount, expiresBySeconds })`
#### `shareCredits({ approvedAddress, approvedWincAmount, expiresBySeconds })`

Creates a delegated payment approval from the connected wallet to the provided native address and approved winc amount. This action will create a data item for the approval
Shares credits from the connected wallet to the provided native address and approved winc amount. This action will create a signed data item for the approval

```typescript
const { approvalDataItemId, approvedWincAmount } =
await turbo.createDelegatedPaymentApproval({
approvedAddress: '2cor...VUa',
approvedWincAmount: 0.08315565032,
expiresBySeconds: 3600,
});
const { approvalDataItemId, approvedWincAmount } = await turbo.shareCredits({
approvedAddress: '2cor...VUa',
approvedWincAmount: 0.08315565032,
expiresBySeconds: 3600,
});
```

#### `revokeDelegatedPaymentApprovals({ approvedAddress })`
#### `revokeCredits({ approvedAddress })`

Revokes all delegated payment approvals from the connected wallet to the provided native address.
Revokes all credits shared from the connected wallet to the provided native address.

```typescript
const revokedApprovals = await turbo.revokeDelegatePaymentApprovals({
approvedAddress: '2cor...VUa',
});
```

#### `getDelegatedPaymentApprovals({ userAddress })`
#### `getCreditShareApprovals({ userAddress })`

Returns all delegated payment approvals from the connected wallet or the provided native address.
Returns all given or received credit share approvals for the connected wallet or the provided native address.

```typescript
const { givenApprovals, receivedApprovals } =
await turbo.getDelegatedPaymentApprovals({
await turbo.getCreditShareApprovals({
userAddress: '2cor...VUa',
});
```
Expand Down Expand Up @@ -782,8 +781,8 @@ Wallet options:
Upload options:

- `--paid-by <paidBy...>` - A list of native addresses to pay for the upload.
- `--ignore-approvals` - When no paid by is provided, the CLI will look for and use any received delegated payment approvals to pay for the upload. This flag will ignore any approvals and only use the connected wallet's balance for upload payment. Default: false
- `--use-signer-balance-first` - Use the connected wallet's balance before using any delegated payment approvals for the upload. Default: false
- `--ignore-approvals` - When no paid by is provided, the CLI will look for and use any received credit share approvals to pay for the upload. This flag will ignore any approvals and only use the connected wallet's balance for upload payment. Default: false
- `--use-signer-balance-first` - Use the connected wallet's balance before using any credit share approvals for the upload. Default: false

#### Commands

Expand Down Expand Up @@ -896,43 +895,43 @@ turbo price --value 1024 --type bytes
turbo price --value 1.1 --type arweave
```

##### `create-approval`
##### `share-credits`

Create a delegated payment approval from the connected wallet to the provided native address and approved winc amount.
Shares credits from the connected wallet to the provided native address and approved winc amount.

Command Options:

- `-a, --address <nativeAddress>` - Native address to that will receive the delegated payment approval
- `-v, --value <value>` - Value of winc to create delegated payment approval for
- `-e, --expires-by-seconds <seconds>` - Expiry time in seconds for the delegated payment approval
- `-a, --address <nativeAddress>` - Native address to that will receive the Credits
- `-v, --value <value>` - Value of winc to share to the target address
- `-e, --expires-by-seconds <seconds>` - Expiry time in seconds for the credit share approval

e.g:

```shell
turbo create-approval --address 2cor...VUa --value 0.083155650320 --wallet-file ../path/to/my/wallet --expires-by-seconds 3600
turbo share-credits --address 2cor...VUa --value 0.083155650320 --wallet-file ../path/to/my/wallet --expires-by-seconds 3600
```

##### `revoke-approvals`
##### `revoke-credits`

Revoke all delegated payment approvals from the connected wallet to the provided native address.
Revoke all credits shared from the connected wallet to the provided native address.

Command Options:

- `-a, --address <nativeAddress>` - Native address to revoke delegated payment approvals for
- `-a, --address <nativeAddress>` - Native address to revoke credit share approvals for

e.g:

```shell
turbo revoke-approvals --wallet-file ../path/to/my/wallet
turbo revoke-credits --wallet-file ../path/to/my/wallet
```

##### `list-approvals`

List all given and received delegated payment approvals from the connected wallet or the provided native address.
List all given and received credit share approvals from the connected wallet or the provided native address.

Command Options:

- `-a, --address <nativeAddress>` - Native address to list delegated payment approvals for
- `-a, --address <nativeAddress>` - Native address to list credit share approvals for

e.g:

Expand Down
20 changes: 9 additions & 11 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import { Command, program } from 'commander';

import { version } from '../version.js';
import { createApproval } from './commands/createApproval.js';
import {
balance,
cryptoFund,
Expand All @@ -30,7 +29,8 @@ import {
uploadFolder,
} from './commands/index.js';
import { listApprovals } from './commands/listApprovals.js';
import { revokeApprovals } from './commands/revokeApprovals.js';
import { revokeCredits } from './commands/revokeCredits.js';
import { shareCredits } from './commands/shareCredits.js';
import {
createApprovalOptions,
globalOptions,
Expand Down Expand Up @@ -101,29 +101,27 @@ applyOptions(

applyOptions(
program
.command('create-approval')
.description('Create a Turbo delegated payment approval'),
.command('share-credits')
.description('Create a Turbo credit share approval'),
createApprovalOptions,
).action(async (_commandOptions, command: Command) => {
await runCommand(command, createApproval);
await runCommand(command, shareCredits);
});

applyOptions(
program
.command('revoke-approvals')
.description(
'Revokes all Turbo delegated payment approvals for given address',
),
.command('revoke-credits')
.description('Revokes all Turbo credit share approvals for given address'),
revokeApprovalsOptions,
).action(async (_commandOptions, command: Command) => {
await runCommand(command, revokeApprovals);
await runCommand(command, revokeCredits);
});

applyOptions(
program
.command('list-approvals')
.description(
'Lists all Turbo delegated payment approvals for given address or wallet',
'Lists all given or received Turbo credit share approvals for specified address or connected wallet',
),
listApprovalsOptions,
).action(async (_commandOptions, command: Command) => {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/listApprovals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function listApprovals(
if (address !== undefined) {
const approvals = await TurboFactory.unauthenticated(
config,
).getDelegatedPaymentApprovals({
).getCreditShareApprovals({
userAddress: address,
});
return { ...approvals, nativeAddress: address };
Expand All @@ -40,7 +40,7 @@ export async function listApprovals(
...config,
privateKey,
});
const approvals = await turbo.getDelegatedPaymentApprovals();
const approvals = await turbo.getCreditShareApprovals();
return {
...approvals,
nativeAddress: await turbo.signer.getNativeAddress(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { RevokeApprovalsOptions } from '../types.js';
import { RevokeCreditsOptions } from '../types.js';
import { turboFromOptions } from '../utils.js';

export async function revokeApprovals(
options: RevokeApprovalsOptions,
export async function revokeCredits(
options: RevokeCreditsOptions,
): Promise<void> {
const { address: revokedAddress } = options;
if (revokedAddress === undefined) {
Expand All @@ -28,13 +28,13 @@ export async function revokeApprovals(

const turbo = await turboFromOptions(options);

const revokedApprovals = await turbo.revokeDelegatedPaymentApprovals({
const revokedApprovals = await turbo.revokeCredits({
revokedAddress,
});

console.log(
JSON.stringify(
{ message: 'Revoked delegated payment approvals!', revokedApprovals },
{ message: 'Revoked credit share approvals!', revokedApprovals },
null,
2,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
import { BigNumber } from 'bignumber.js';

import { CreateApprovalOptions } from '../types.js';
import { ShareCreditsOptions } from '../types.js';
import { turboFromOptions } from '../utils.js';

export async function createApproval(
options: CreateApprovalOptions,
export async function shareCredits(
options: ShareCreditsOptions,
): Promise<void> {
const {
address: approvedAddress,
Expand All @@ -40,15 +40,15 @@ export async function createApproval(
const approvedWincAmount = new BigNumber(creditAmount)
.shiftedBy(12)
.toFixed(0);
const result = await turbo.createDelegatedPaymentApproval({
const result = await turbo.shareCredits({
approvedAddress,
approvedWincAmount,
expiresBySeconds,
});

console.log(
JSON.stringify(
{ message: 'Created delegated payment approval!', ...result },
{ message: 'Created credit share approval!', ...result },
null,
2,
),
Expand Down
4 changes: 2 additions & 2 deletions src/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ export const optionMap = {
ignoreApprovals: {
alias: '--ignore-approvals',
description:
"Ignore all delegated payment approvals, only use signing wallet's balance",
"Ignore all credit share approvals, only use signing wallet's balance",
default: false,
},
useSignerBalanceFirst: {
alias: '--use-signer-balance-first',
description:
'Use the signer balance first before using delegated payment approvals',
'Use the signer balance first before using credit share approvals',
default: false,
},
} as const;
Expand Down
6 changes: 3 additions & 3 deletions src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ export type CryptoFundOptions = WalletOptions & {
txId: string | undefined;
};

export type CreateApprovalOptions = WalletOptions & {
export type ShareCreditsOptions = WalletOptions & {
address: string | undefined;
value: string | undefined;
expiresBySeconds: number | undefined;
};

export type RevokeApprovalsOptions = WalletOptions & {
export type RevokeCreditsOptions = WalletOptions & {
address: string | undefined;
};

export type ListApprovalsOptions = RevokeApprovalsOptions;
export type ListApprovalsOptions = RevokeCreditsOptions;
6 changes: 3 additions & 3 deletions src/common/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class TurboUnauthenticatedPaymentService
throw new Error('Unknown response from payment service: ' + response);
}

public async getDelegatedPaymentApprovals({
public async getCreditShareApprovals({
userAddress,
}: {
userAddress: UserAddress;
Expand Down Expand Up @@ -314,13 +314,13 @@ export class TurboAuthenticatedPaymentService
return super.getBalance(userAddress);
}

public async getDelegatedPaymentApprovals({
public async getCreditShareApprovals({
userAddress,
}: {
userAddress?: string;
}): Promise<GetDelegatedPaymentApprovalsResponse> {
userAddress ??= await this.signer.getNativeAddress();
return super.getDelegatedPaymentApprovals({ userAddress });
return super.getCreditShareApprovals({ userAddress });
}

public async getWincForFiat({
Expand Down
Loading

0 comments on commit 1bcb4b7

Please sign in to comment.