Skip to content

Commit

Permalink
feat: add cli helper for --local development endpoints PE-6754
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Oct 25, 2024
1 parent e1c5fb5 commit 79fe7a0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ services:

payment-service:
# build:
# context: ../payment-service
# context: ../payment-service
image: ghcr.io/ardriveapp/payment-service:latest
ports:
- '4000:4000'
Expand Down
38 changes: 23 additions & 15 deletions src/cli/commands/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,36 @@ export async function balance(options: AddressOptions) {
const config = configFromOptions(options);
const { address, privateKey } = await addressOrPrivateKeyFromOptions(options);

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

console.log(
`Turbo Balance for Native Address "${address}"\nCredits: ${
`Turbo Balance for Native Address "${nativeAddress}"\nCredits: ${
+winc / 1_000_000_000_000
}${
givenApprovals.length > 0
givenApprovals?.length > 0
? `\nGiven Approvals:\n${JSON.stringify(givenApprovals, null, 2)}`
: ''
}${
receivedApprovals.length > 0
receivedApprovals?.length > 0
? `\nReceived Approvals:\n${JSON.stringify(receivedApprovals, null, 2)}`
: ''
}`,
Expand Down
8 changes: 7 additions & 1 deletion src/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export const optionMap = {
},
dev: {
alias: '--dev',
description: 'Enable development endpoints',
description: 'Enable Turbo development endpoints',
default: false,
},
local: {
alias: '--local',
description: 'Enable local development endpoints',
default: false,
},
debug: {
Expand Down Expand Up @@ -151,6 +156,7 @@ export const walletOptions = [

export const globalOptions = [
optionMap.dev,
optionMap.local,
optionMap.gateway,
optionMap.debug,
optionMap.quiet,
Expand Down
1 change: 1 addition & 0 deletions src/cli/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

export type GlobalOptions = {
dev: boolean;
local: boolean;
gateway: string | undefined;
debug: boolean;
quiet: boolean;
Expand Down
9 changes: 9 additions & 0 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,20 @@ export function configFromOptions(
let uploadUrl: string | undefined = undefined;
let gatewayUrl: string | undefined = undefined;

if (options.local && options.dev) {
throw new Error('Cannot use both --local and --dev flags');
}

if (options.dev) {
// Use development endpoints
paymentUrl = developmentTurboConfiguration.paymentServiceConfig.url;
uploadUrl = developmentTurboConfiguration.uploadServiceConfig.url;
gatewayUrl = tokenToDevGatewayMap[token];
} else if (options.local) {
// Use local endpoints
paymentUrl = 'http://localhost:4000';
uploadUrl = 'http://localhost:3000';
gatewayUrl = 'http://localhost:1984';
} else {
// Use default endpoints
paymentUrl = defaultTurboConfiguration.paymentServiceConfig.url;
Expand Down

0 comments on commit 79fe7a0

Please sign in to comment.