Skip to content

Commit 1145095

Browse files
committed
withdraw and get usefunds
1 parent 72f6f65 commit 1145095

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

src/cli.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,32 @@ export async function createCLI() {
414414
console.log(chalk.green('Deposit successful'));
415415
});
416416

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+
417443
// Escrow authorization command
418444
program
419445
.command('authorizeEscrow')

src/commands.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
getIndexingWaitSettings,
1111
IndexerWaitParams,
1212
fixAndParseProviderFees,
13-
getConfigByChainId,
13+
getConfigByChainId
1414
} from "./helpers.js";
1515
import {
1616
Aquarius,
@@ -26,7 +26,8 @@ import {
2626
orderAsset,
2727
sendTx,
2828
unitsToAmount,
29-
EscrowContract
29+
EscrowContract,
30+
getTokenDecimals
3031
} from "@oceanprotocol/lib";
3132
import { Asset } from '@oceanprotocol/ddo-js';
3233
import { Signer, ethers } from "ethers";
@@ -1315,6 +1316,50 @@ export class Commands {
13151316
console.log(`Auth token successfully invalidated`);
13161317
}
13171318

1319+
public async getEscrowBalance(token: string): Promise<number> {
1320+
const config = await getConfigByChainId(Number(this.config.chainId));
1321+
const escrow = new EscrowContract(
1322+
ethers.utils.getAddress(config.Escrow),
1323+
this.signer,
1324+
Number(this.config.chainId)
1325+
);
1326+
1327+
try {
1328+
const balance = await escrow.getUserFunds(await this.signer.getAddress(), token);
1329+
const decimals = await getTokenDecimals(this.signer, token);
1330+
1331+
const sum = await balance.reduce(async (accPromise, curr) => {
1332+
const acc = await accPromise;
1333+
const amount = await unitsToAmount(this.signer, token, curr, decimals);
1334+
return acc + Number(amount);
1335+
}, Promise.resolve(0));
1336+
1337+
console.log(`Escrow user funds for token ${token}: ${sum}`);
1338+
return sum;
1339+
} catch (error) {
1340+
console.error("Error getting escrow balance:", error);
1341+
}
1342+
}
1343+
1344+
public async withdrawFromEscrow(token: string, amount: string): Promise<void> {
1345+
const config = await getConfigByChainId(Number(this.config.chainId));
1346+
const escrow = new EscrowContract(
1347+
ethers.utils.getAddress(config.Escrow),
1348+
this.signer,
1349+
Number(this.config.chainId)
1350+
);
1351+
1352+
const balance = await this.getEscrowBalance(token);
1353+
if (balance < Number(amount)) {
1354+
console.error(`Insufficient balance in escrow for token ${token}`);
1355+
return;
1356+
}
1357+
1358+
const withdrawTx = await escrow.withdraw([token], [amount]);
1359+
await withdrawTx.wait();
1360+
console.log(`Successfully withdrawn ${amount} ${token} from escrow`);
1361+
}
1362+
13181363
public async depositToEscrow(signer: Signer, token: string, amount: string, chainId: number) {
13191364
try {
13201365
const amountInUnits = await amountToUnits(signer, token, amount, 18);

src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
} from "@oceanprotocol/lib";
2727
import { homedir } from "os";
2828

29-
const ERC20Template = readFileSync('./node_modules/@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json', 'utf8') as any;
29+
const ERC20Template = readFileSync('./node_modules/@oceanprotocol/contracts/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.json', 'utf8') as any;
3030

3131
export async function downloadFile(
3232
url: string,

test/escrow.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,39 @@ describe("Ocean CLI Escrow", function () {
3636
it("should deposit tokens into escrow", async function () {
3737
const depositAmount = "1";
3838

39+
const initialTokenBalance = await runCommand(`npm run cli getUserFundsEscrow ${tokenAddress}`);
40+
const initialTokenBalanceNumber = initialTokenBalance.split(`${tokenAddress}: `)[1].trim();
41+
const numberInitialTokenBalance = Number(initialTokenBalanceNumber);
42+
3943
const output = await runCommand(
4044
`npm run cli depositEscrow ${tokenAddress} ${depositAmount}`
4145
);
4246

47+
const finalTokenBalance = await runCommand(`npm run cli getUserFundsEscrow ${tokenAddress}`);
48+
const finalTokenBalanceNumber = finalTokenBalance.split(`${tokenAddress}: `)[1].trim();
49+
const numberFinalTokenBalance = Number(finalTokenBalanceNumber);
4350

4451
expect(output).to.include("Deposit successful");
52+
expect(numberFinalTokenBalance).to.equal(Number(depositAmount) + numberInitialTokenBalance);
53+
});
54+
55+
it("should withdraw tokens from escrow", async function () {
56+
const withdrawAmount = "1";
57+
const output = await runCommand(
58+
`npm run cli withdrawFromEscrow ${tokenAddress} ${withdrawAmount}`
59+
);
60+
61+
expect(output).to.include("Successfully withdrawn");
4562
});
4663

4764
it("should fail to deposit with invalid amount", async function () {
48-
const invalidAmount = "10000000";
65+
const invalidAmount = "1000000000000000";
4966

5067
const output = await runCommand(
5168
`npm run cli depositEscrow ${tokenAddress} ${invalidAmount}`
5269
);
5370

71+
5472
expect(output).to.include("Deposit failed");
5573
});
5674

0 commit comments

Comments
 (0)