Skip to content

Commit 93beda0

Browse files
authored
Feature/paid auth extra commands (#124)
* genereate and invalidate auth token * lint fix * fix tests * package update * withdraw and get usefunds * fix user funds * added get authorizations method
1 parent 2ade7cc commit 93beda0

6 files changed

Lines changed: 597 additions & 157 deletions

File tree

package-lock.json

Lines changed: 37 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
"dependencies": {
4747
"@oasisprotocol/sapphire-paratime": "^1.3.2",
4848
"@oceanprotocol/contracts": "^2.3.1",
49-
"@oceanprotocol/ddo-js": "^0.1.0",
50-
"@oceanprotocol/lib": "^4.1.4",
49+
"@oceanprotocol/ddo-js": "^0.1.1",
50+
"@oceanprotocol/lib": "^4.2.0",
5151
"commander": "^13.1.0",
5252
"cross-fetch": "^3.1.5",
5353
"crypto-js": "^4.1.1",

src/cli.ts

Lines changed: 128 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { unitsToAmount } from '@oceanprotocol/lib';
88
import { toBoolean } from './helpers.js';
99

1010
async function initializeSigner() {
11-
11+
1212
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC);
1313
let signer;
14-
14+
1515
if (process.env.PRIVATE_KEY) {
1616
signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
1717
} else {
@@ -38,7 +38,7 @@ export async function createCLI() {
3838
console.error(chalk.red("Have you forgot to set env NODE_URL?"));
3939
process.exit(1);
4040
}
41-
41+
4242
const program = new Command();
4343

4444
program
@@ -203,7 +203,7 @@ export async function createCLI() {
203203
const jobDuration = options.maxJobDuration || maxJobDuration;
204204
const token = options.token || paymentToken;
205205
const res = options.resources || resources;
206-
if (!dsDids || !aDid ||!envId || !jobDuration || !token || !res) {
206+
if (!dsDids || !aDid || !envId || !jobDuration || !token || !res) {
207207
console.error(chalk.red('Missing required arguments'));
208208
// process.exit(1);
209209
return
@@ -244,7 +244,7 @@ export async function createCLI() {
244244

245245
await commands.computeStart(computeArgs);
246246
console.log(chalk.green('Compute job started successfully.'));
247-
});
247+
});
248248

249249
// startFreeCompute command
250250
program
@@ -315,7 +315,7 @@ export async function createCLI() {
315315
}
316316
const { signer, chainId } = await initializeSigner();
317317
const commands = new Commands(signer, chainId);
318-
const args = [null, dsDid, jId];
318+
const args = [null, dsDid, jId];
319319
if (agrId) args.push(agrId);
320320
await commands.computeStop(args);
321321
});
@@ -369,5 +369,127 @@ export async function createCLI() {
369369
await commands.mintOceanTokens();
370370
});
371371

372+
// Generate new auth token
373+
program
374+
.command('generateAuthToken')
375+
.description('Generate new auth token')
376+
.action(async () => {
377+
const { signer, chainId } = await initializeSigner();
378+
const commands = new Commands(signer, chainId);
379+
await commands.generateAuthToken();
380+
});
381+
382+
383+
// Invalidate auth token
384+
program
385+
.command('invalidateAuthToken')
386+
.description('Invalidate auth token')
387+
.argument('<token>', 'Auth token')
388+
.option('-t, --token <token>', 'Auth token')
389+
.action(async (token, options) => {
390+
const { signer, chainId } = await initializeSigner();
391+
const commands = new Commands(signer, chainId);
392+
await commands.invalidateAuthToken([token || options.token]);
393+
});
394+
395+
// Escrow deposit command
396+
program
397+
.command('depositEscrow')
398+
.description('Deposit tokens into the escrow contract')
399+
.argument('<token>', 'Address of the token to deposit')
400+
.argument('<amount>', 'Amount of tokens to deposit')
401+
.option('-t, --token <token>', 'Address of the token to deposit')
402+
.option('-a, --amount <amount>', 'Amount of tokens to deposit')
403+
.action(async (token, amount, options) => {
404+
const { signer, chainId } = await initializeSigner();
405+
const commands = new Commands(signer, chainId);
406+
const tokenAddress = options.token || token;
407+
const amountToDeposit = options.amount || amount;
408+
const success = await commands.depositToEscrow(signer, tokenAddress, amountToDeposit, chainId);
409+
if (!success) {
410+
console.log(chalk.red('Deposit failed'));
411+
return;
412+
}
413+
414+
console.log(chalk.green('Deposit successful'));
415+
});
416+
417+
// Check escrow deposited balance
418+
program
419+
.command('getUserFundsEscrow')
420+
.description('Get deposited token amount in escrow for user')
421+
.argument('<token>', 'Address of the token to check')
422+
.option('-t, --token <token>', 'Address of the token to check')
423+
.action(async (token, options) => {
424+
const { signer, chainId } = await initializeSigner();
425+
const commands = new Commands(signer, chainId);
426+
await commands.getEscrowBalance(token || options.token);
427+
});
428+
429+
// Withdraw from escrow
430+
program
431+
.command('withdrawFromEscrow')
432+
.description('Withdraw tokens from escrow')
433+
.argument('<token>', 'Address of the token to check')
434+
.argument('<amount>', 'Amount of tokens to withdraw')
435+
.option('-t, --token <token>', 'Address of the token to check')
436+
.option('-a, --amount <amount>', 'Amount of tokens to withdraw')
437+
.action(async (token, amount, options) => {
438+
const { signer, chainId } = await initializeSigner();
439+
const commands = new Commands(signer, chainId);
440+
await commands.withdrawFromEscrow(token || options.token, amount);
441+
});
442+
443+
// Escrow authorization command
444+
program
445+
.command('authorizeEscrow')
446+
.description('Authorize a payee to lock and claim funds from escrow')
447+
.argument('<token>', 'Address of the token to authorize')
448+
.argument('<payee>', 'Address of the payee to authorize')
449+
.argument('<maxLockedAmount>', 'Maximum amount that can be locked by payee')
450+
.argument('<maxLockSeconds>', 'Maximum lock duration in seconds')
451+
.argument('<maxLockCounts>', 'Maximum number of locks allowed')
452+
.option('-t, --token <token>', 'Address of the token to authorize')
453+
.option('-p, --payee <payee>', 'Address of the payee to authorize')
454+
.option('-m, --maxLockedAmount <maxLockedAmount>', 'Maximum amount that can be locked by payee')
455+
.option('-s, --maxLockSeconds <maxLockSeconds>', 'Maximum lock duration in seconds')
456+
.option('-c, --maxLockCounts <maxLockCounts>', 'Maximum number of locks allowed')
457+
.action(async (token, payee, maxLockedAmount, maxLockSeconds, maxLockCounts, options) => {
458+
const { signer, chainId } = await initializeSigner();
459+
const commands = new Commands(signer, chainId);
460+
const tokenAddress = options.token || token;
461+
const payeeAddress = options.payee || payee;
462+
const maxLockedAmountValue = options.maxLockedAmount || maxLockedAmount;
463+
const maxLockSecondsValue = options.maxLockSeconds || maxLockSeconds;
464+
const maxLockCountsValue = options.maxLockCounts || maxLockCounts;
465+
466+
const success = await commands.authorizeEscrowPayee(
467+
tokenAddress,
468+
payeeAddress,
469+
maxLockedAmountValue,
470+
maxLockSecondsValue,
471+
maxLockCountsValue,
472+
);
473+
474+
if (!success) {
475+
console.log(chalk.red('Authorization failed'));
476+
return;
477+
}
478+
479+
console.log(chalk.green('Authorization successful'));
480+
});
481+
482+
program
483+
.command('getAuthorizationsEscrow')
484+
.description('Get authorizations for escrow')
485+
.argument('<token>', 'Address of the token to check')
486+
.argument('<payee>', 'Address of the payee to check')
487+
.option('-t, --token <token>', 'Address of the token to check')
488+
.action(async (token, payee, options) => {
489+
const { signer, chainId } = await initializeSigner();
490+
const commands = new Commands(signer, chainId);
491+
await commands.getAuthorizationsEscrow(token || options.token, payee || options.payee);
492+
});
493+
372494
return program;
373495
}

0 commit comments

Comments
 (0)