bstest001 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/deposit.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- export declare function deposit({ provider: rawProvider, depositAmountInput, keyBasePath, signature, address, txSender }: {
2
- provider: any;
1
+ export declare function deposit({ depositAmountInput, keyBasePath, signature, address, txSender }: {
3
2
  depositAmountInput: number;
4
3
  keyBasePath: string;
5
4
  signature: string;
package/dist/deposit.js CHANGED
@@ -1,19 +1,20 @@
1
1
  import { BigNumber, ethers } from 'ethers';
2
2
  import EtherPoolAbi from './utils/EtherPool.abi.json' with { type: 'json' };
3
- import { BASE_SEPOLIA_RPC, CONTRACT_ADDRESS, INDEXER_URL } from './utils/constants.js';
3
+ import { BASE_SEPOLIA_RPC, CONTRACT_ADDRESS, INDEXER_URL, MIN_DEPOSIT_AMOUNT } from './utils/constants.js';
4
4
  import { deriveKeys } from './utils/encryption.js';
5
5
  import { logger } from './utils/logger';
6
6
  import { findUnspentUtxos, prepareTransaction, toFixedHex } from './utils/utils.js';
7
7
  import { Utxo } from './utils/utxo.js';
8
- export async function deposit({ provider: rawProvider, depositAmountInput, keyBasePath, signature, address, txSender }) {
9
- const provider = rawProvider?.request
10
- ? new ethers.providers.Web3Provider(rawProvider)
11
- : rawProvider;
8
+ export async function deposit({ depositAmountInput, keyBasePath, signature, address, txSender }) {
12
9
  const readProvider = new ethers.providers.JsonRpcProvider(BASE_SEPOLIA_RPC);
13
10
  const etherPoolAddress = CONTRACT_ADDRESS;
14
11
  if (!etherPoolAddress) {
15
12
  throw new Error('Missing CONTRACT_ADDRESS in environment');
16
13
  }
14
+ // check if depositAmountInput is above minimum
15
+ if (depositAmountInput < MIN_DEPOSIT_AMOUNT) {
16
+ throw new Error(`Deposit amount must be at least ${MIN_DEPOSIT_AMOUNT} ETH`);
17
+ }
17
18
  const contractAddress = ethers.utils.getAddress(etherPoolAddress);
18
19
  logger.debug(`Depositor: ${address}`);
19
20
  logger.info('Signing in to derive keys...');
@@ -29,6 +30,7 @@ export async function deposit({ provider: rawProvider, depositAmountInput, keyBa
29
30
  throw new Error(`Deposit amount ${depositAmountInput} ETH exceeds pool limit of ${ethers.utils.formatEther(maxDeposit)} ETH`);
30
31
  }
31
32
  // post address to /screen_address of indexer to check if it's blacklisted
33
+ logger.info('screening walleting');
32
34
  logger.debug(`screening address ${address}`);
33
35
  let res = await fetch(INDEXER_URL + '/screen_address', {
34
36
  method: 'POST',
@@ -68,14 +70,13 @@ export async function deposit({ provider: rawProvider, depositAmountInput, keyBa
68
70
  const outputAmount = inputSum.add(depositAmount);
69
71
  const outputUtxo = new Utxo({ amount: outputAmount, keypair });
70
72
  logger.debug(`Depositing ${depositAmountInput} ETH (new output: ${ethers.utils.formatEther(outputAmount)} ETH)`);
73
+ logger.info('generating ZK proof');
71
74
  const { args, extData } = await prepareTransaction({
72
75
  inputs,
73
76
  outputs: [outputUtxo],
74
77
  encryptionKey,
75
78
  keyBasePath,
76
79
  });
77
- // Build the transaction (Populate)
78
- logger.info('Building transaction...');
79
80
  const partialTx = await etherPool.populateTransaction.transact(args, extData, {
80
81
  value: depositAmount,
81
82
  gasLimit: 3000000,
@@ -97,8 +98,9 @@ export async function deposit({ provider: rawProvider, depositAmountInput, keyBa
97
98
  throw new Error('Failed to fetch network fee data for transaction signing');
98
99
  }
99
100
  try {
101
+ logger.info('waiting for user signature');
100
102
  let tx = await txSender(unsignedTx);
101
- logger.info(`Transaction sent!`);
103
+ logger.info(`confirming transaction`);
102
104
  logger.debug('veryfying transaction on indexer...', extData.encryptedOutput1);
103
105
  let retryTimes = 0;
104
106
  let itv = 2;
@@ -6,3 +6,5 @@ export declare let BASE_SEPOLIA_RPC: string;
6
6
  export declare const RENT_FEE = 0.00025;
7
7
  export declare const FEE_RATE = 35;
8
8
  export declare const SIGN_PRIVACY_MESSAGE = "Privacy Money account sign in";
9
+ export declare const MIN_DEPOSIT_AMOUNT = 0.001;
10
+ export declare const MIN_WITHDRAWAL_AMOUNT = 0.001;
@@ -6,3 +6,5 @@ export let BASE_SEPOLIA_RPC = process.env.BASE_SEPOLIA_RPC || 'https://sepolia.b
6
6
  export const RENT_FEE = 0.00025;
7
7
  export const FEE_RATE = 35; // 0.35% (basis points out of 10000)
8
8
  export const SIGN_PRIVACY_MESSAGE = 'Privacy Money account sign in';
9
+ export const MIN_DEPOSIT_AMOUNT = 0.001; // Minimum deposit amount in ETH
10
+ export const MIN_WITHDRAWAL_AMOUNT = 0.001; // Minimum withdrawal amount in ETH
@@ -190,7 +190,8 @@ export async function findUnspentUtxos({ address, etherPool, encryptionKey, keyp
190
190
  throw new Error(`Failed to fetch encrypted UTXOs: ${res.statusText}`);
191
191
  }
192
192
  const data = await res.json();
193
- const { encrypted_outputs, hasMore: more, start: realStart } = data;
193
+ const { encrypted_outputs, hasMore: more, start: realStart, total } = data;
194
+ logger.info(`decrypting utxo ${startIndex}/${total}`);
194
195
  if (encrypted_outputs && encrypted_outputs.length > 0) {
195
196
  encrypted_outputs.forEach((encryptedOutput, i) => {
196
197
  const index = (realStart ?? startIndex) + i;
package/dist/withdraw.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BigNumber, ethers } from 'ethers';
2
2
  import EtherPoolAbi from './utils/EtherPool.abi.json' with { type: 'json' };
3
- import { CONTRACT_ADDRESS, FEE_RATE, FEE_RECIPIENT_ADDRESS, INDEXER_URL, RENT_FEE } from './utils/constants.js';
3
+ import { CONTRACT_ADDRESS, FEE_RATE, FEE_RECIPIENT_ADDRESS, INDEXER_URL, MIN_WITHDRAWAL_AMOUNT, RENT_FEE } from './utils/constants.js';
4
4
  import { deriveKeys } from './utils/encryption.js';
5
5
  import { logger } from './utils/logger';
6
6
  import { findUnspentUtxos, prepareTransaction, toFixedHex } from './utils/utils.js';
@@ -14,6 +14,10 @@ export async function withdraw({ provider: rawProvider, withdrawAmountInput, rec
14
14
  if (!etherPoolAddress) {
15
15
  throw new Error('Missing CONTRACT_ADDRESS in environment');
16
16
  }
17
+ // check if withdrawAmountInput is above minimum
18
+ if (withdrawAmountInput < MIN_WITHDRAWAL_AMOUNT) {
19
+ throw new Error(`Withdrawal amount must be at least ${MIN_WITHDRAWAL_AMOUNT} ETH`);
20
+ }
17
21
  const feeRecipientEnv = FEE_RECIPIENT_ADDRESS;
18
22
  const contractAddress = ethers.utils.getAddress(etherPoolAddress);
19
23
  logger.debug(`Withdrawing ${withdrawAmountInput} ETH to recipient: ${recipient}`);
@@ -47,19 +51,20 @@ export async function withdraw({ provider: rawProvider, withdrawAmountInput, rec
47
51
  const fee = FLAT_FEE.add(withdrawAmount.mul(FEE_RATE).div(10000));
48
52
  logger.debug(`Fee recipient: ${feeRecipient}`);
49
53
  logger.debug(`Fee: ${ethers.utils.formatEther(fee)} ETH (0.00025 + 0.35%)`);
50
- if (withdrawAmount.lte(fee)) {
51
- throw new Error(`Invalid withdraw amount: ${withdrawAmountInput} ETH. Amount must be greater than fee (${ethers.utils.formatEther(fee)} ETH).`);
52
- }
53
- if (inputSum.lt(withdrawAmount.add(fee))) {
54
- throw new Error(`Insufficient balance. Have ${ethers.utils.formatEther(inputSum)} ETH, need ${ethers.utils.formatEther(withdrawAmount.add(fee))} ETH (${withdrawAmountInput} + fee).`);
54
+ // withdrawAmount should includes fee
55
+ let realAriveAmount = withdrawAmount.sub(fee);
56
+ logger.debug(`Amount to arrive at recipient: ${ethers.utils.formatEther(realAriveAmount)} ETH`);
57
+ if (inputSum.lt(withdrawAmount)) {
58
+ throw new Error(`Insufficient balance. Have ${ethers.utils.formatEther(inputSum)} ETH, need ${ethers.utils.formatEther(withdrawAmount)} ETH (${withdrawAmountInput}).`);
55
59
  }
56
- const changeAmount = inputSum.sub(withdrawAmount).sub(fee);
60
+ const changeAmount = inputSum.sub(withdrawAmount);
57
61
  const outputs = [];
58
62
  if (changeAmount.gt(0)) {
59
63
  outputs.push(new Utxo({ amount: changeAmount, keypair }));
60
64
  logger.debug(`Change UTXO: ${ethers.utils.formatEther(changeAmount)} ETH`);
61
65
  }
62
- logger.debug(`\nWithdrawing ${withdrawAmountInput} ETH to ${recipient}...`);
66
+ logger.debug(`\nWithdrawing ${realAriveAmount} ETH to ${recipient}...`);
67
+ logger.info('generating ZK proof');
63
68
  const { args, extData } = await prepareTransaction({
64
69
  inputs,
65
70
  outputs,
@@ -69,7 +74,7 @@ export async function withdraw({ provider: rawProvider, withdrawAmountInput, rec
69
74
  encryptionKey,
70
75
  keyBasePath,
71
76
  });
72
- logger.info(`\nSending transaction to relayer`);
77
+ logger.info(`submitting transaction to relayer`);
73
78
  const response = await fetch(`${INDEXER_URL}/relayer/withdraw`, {
74
79
  method: 'POST',
75
80
  headers: {
@@ -91,6 +96,7 @@ export async function withdraw({ provider: rawProvider, withdrawAmountInput, rec
91
96
  if (changeAmount.gt(0)) {
92
97
  logger.debug(`\nChange UTXO created (${ethers.utils.formatEther(changeAmount)} ETH)`);
93
98
  }
99
+ logger.info('confirming transaction');
94
100
  let retryTimes = 0;
95
101
  let itv = 2;
96
102
  // const encryptedOutputStr = Buffer.from(extData.encryptedOutput1).toString('hex')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bstest001",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "exports": {
package/src/deposit.ts CHANGED
@@ -1,27 +1,29 @@
1
1
  import { BigNumber, ethers } from 'ethers';
2
2
  import EtherPoolAbi from './utils/EtherPool.abi.json' with { type: 'json' };
3
- import { BASE_SEPOLIA_RPC, CONTRACT_ADDRESS, INDEXER_URL } from './utils/constants.js';
3
+ import { BASE_SEPOLIA_RPC, CONTRACT_ADDRESS, INDEXER_URL, MIN_DEPOSIT_AMOUNT } from './utils/constants.js';
4
4
  import { deriveKeys } from './utils/encryption.js';
5
5
  import { logger } from './utils/logger';
6
6
  import { findUnspentUtxos, prepareTransaction, toFixedHex } from './utils/utils.js';
7
7
  import { Utxo } from './utils/utxo.js';
8
8
 
9
- export async function deposit({ provider: rawProvider, depositAmountInput, keyBasePath, signature, address, txSender }: {
10
- provider: any,
9
+ export async function deposit({ depositAmountInput, keyBasePath, signature, address, txSender }: {
11
10
  depositAmountInput: number,
12
11
  keyBasePath: string,
13
12
  signature: string,
14
13
  address: string,
15
14
  txSender: any
16
15
  }) {
17
- const provider: ethers.providers.Provider = rawProvider?.request
18
- ? new ethers.providers.Web3Provider(rawProvider)
19
- : rawProvider;
20
16
  const readProvider = new ethers.providers.JsonRpcProvider(BASE_SEPOLIA_RPC);
21
17
  const etherPoolAddress = CONTRACT_ADDRESS;
22
18
  if (!etherPoolAddress) {
23
19
  throw new Error('Missing CONTRACT_ADDRESS in environment');
24
20
  }
21
+
22
+ // check if depositAmountInput is above minimum
23
+ if (depositAmountInput < MIN_DEPOSIT_AMOUNT) {
24
+ throw new Error(`Deposit amount must be at least ${MIN_DEPOSIT_AMOUNT} ETH`);
25
+ }
26
+
25
27
  const contractAddress = ethers.utils.getAddress(etherPoolAddress);
26
28
 
27
29
  logger.debug(`Depositor: ${address}`);
@@ -44,6 +46,7 @@ export async function deposit({ provider: rawProvider, depositAmountInput, keyBa
44
46
  }
45
47
 
46
48
  // post address to /screen_address of indexer to check if it's blacklisted
49
+ logger.info('screening walleting')
47
50
  logger.debug(`screening address ${address}`)
48
51
  let res = await fetch(INDEXER_URL + '/screen_address', {
49
52
  method: 'POST',
@@ -87,6 +90,8 @@ export async function deposit({ provider: rawProvider, depositAmountInput, keyBa
87
90
 
88
91
  logger.debug(`Depositing ${depositAmountInput} ETH (new output: ${ethers.utils.formatEther(outputAmount)} ETH)`);
89
92
 
93
+ logger.info('generating ZK proof')
94
+
90
95
  const { args, extData } = await prepareTransaction({
91
96
  inputs,
92
97
  outputs: [outputUtxo],
@@ -94,9 +99,6 @@ export async function deposit({ provider: rawProvider, depositAmountInput, keyBa
94
99
  keyBasePath,
95
100
  });
96
101
 
97
- // Build the transaction (Populate)
98
- logger.info('Building transaction...');
99
-
100
102
  const partialTx = await etherPool.populateTransaction.transact(args, extData, {
101
103
  value: depositAmount,
102
104
  gasLimit: 3000000,
@@ -118,8 +120,9 @@ export async function deposit({ provider: rawProvider, depositAmountInput, keyBa
118
120
  throw new Error('Failed to fetch network fee data for transaction signing');
119
121
  }
120
122
  try {
123
+ logger.info('waiting for user signature')
121
124
  let tx = await txSender(unsignedTx)
122
- logger.info(`Transaction sent!`);
125
+ logger.info(`confirming transaction`);
123
126
  logger.debug('veryfying transaction on indexer...', extData.encryptedOutput1)
124
127
  let retryTimes = 0
125
128
  let itv = 2
@@ -7,4 +7,7 @@ export let BASE_SEPOLIA_RPC = process.env.BASE_SEPOLIA_RPC || 'https://sepolia.b
7
7
  export const RENT_FEE = 0.00025
8
8
  export const FEE_RATE = 35; // 0.35% (basis points out of 10000)
9
9
 
10
- export const SIGN_PRIVACY_MESSAGE = 'Privacy Money account sign in';
10
+ export const SIGN_PRIVACY_MESSAGE = 'Privacy Money account sign in';
11
+
12
+ export const MIN_DEPOSIT_AMOUNT = 0.001; // Minimum deposit amount in ETH
13
+ export const MIN_WITHDRAWAL_AMOUNT = 0.001; // Minimum withdrawal amount in ETH
@@ -283,8 +283,8 @@ export async function findUnspentUtxos({
283
283
  throw new Error(`Failed to fetch encrypted UTXOs: ${res.statusText}`);
284
284
  }
285
285
  const data = await res.json();
286
- const { encrypted_outputs, hasMore: more, start: realStart } = data;
287
-
286
+ const { encrypted_outputs, hasMore: more, start: realStart, total } = data;
287
+ logger.info(`decrypting utxo ${startIndex}/${total}`)
288
288
  if (encrypted_outputs && encrypted_outputs.length > 0) {
289
289
  encrypted_outputs.forEach((encryptedOutput: string, i: number) => {
290
290
  const index = (realStart ?? startIndex) + i;
package/src/withdraw.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BigNumber, ethers } from 'ethers';
2
2
  import EtherPoolAbi from './utils/EtherPool.abi.json' with { type: 'json' };
3
- import { CONTRACT_ADDRESS, FEE_RATE, FEE_RECIPIENT_ADDRESS, INDEXER_URL, RENT_FEE } from './utils/constants.js';
3
+ import { CONTRACT_ADDRESS, FEE_RATE, FEE_RECIPIENT_ADDRESS, INDEXER_URL, MIN_WITHDRAWAL_AMOUNT, RENT_FEE } from './utils/constants.js';
4
4
  import { deriveKeys } from './utils/encryption.js';
5
5
  import { logger } from './utils/logger';
6
6
  import { findUnspentUtxos, prepareTransaction, toFixedHex } from './utils/utils.js';
@@ -23,6 +23,12 @@ export async function withdraw({ provider: rawProvider, withdrawAmountInput, rec
23
23
  if (!etherPoolAddress) {
24
24
  throw new Error('Missing CONTRACT_ADDRESS in environment');
25
25
  }
26
+
27
+ // check if withdrawAmountInput is above minimum
28
+ if (withdrawAmountInput < MIN_WITHDRAWAL_AMOUNT) {
29
+ throw new Error(`Withdrawal amount must be at least ${MIN_WITHDRAWAL_AMOUNT} ETH`);
30
+ }
31
+
26
32
  const feeRecipientEnv = FEE_RECIPIENT_ADDRESS;
27
33
  const contractAddress = ethers.utils.getAddress(etherPoolAddress);
28
34
 
@@ -65,17 +71,15 @@ export async function withdraw({ provider: rawProvider, withdrawAmountInput, rec
65
71
  logger.debug(`Fee recipient: ${feeRecipient}`);
66
72
  logger.debug(`Fee: ${ethers.utils.formatEther(fee)} ETH (0.00025 + 0.35%)`);
67
73
 
68
- if (withdrawAmount.lte(fee)) {
69
- throw new Error(
70
- `Invalid withdraw amount: ${withdrawAmountInput} ETH. Amount must be greater than fee (${ethers.utils.formatEther(fee)} ETH).`,
71
- );
72
- }
74
+ // withdrawAmount should includes fee
75
+ let realAriveAmount = withdrawAmount.sub(fee);
76
+ logger.debug(`Amount to arrive at recipient: ${ethers.utils.formatEther(realAriveAmount)} ETH`);
73
77
 
74
- if (inputSum.lt(withdrawAmount.add(fee))) {
75
- throw new Error(`Insufficient balance. Have ${ethers.utils.formatEther(inputSum)} ETH, need ${ethers.utils.formatEther(withdrawAmount.add(fee))} ETH (${withdrawAmountInput} + fee).`);
78
+ if (inputSum.lt(withdrawAmount)) {
79
+ throw new Error(`Insufficient balance. Have ${ethers.utils.formatEther(inputSum)} ETH, need ${ethers.utils.formatEther(withdrawAmount)} ETH (${withdrawAmountInput}).`);
76
80
  }
77
81
 
78
- const changeAmount = inputSum.sub(withdrawAmount).sub(fee);
82
+ const changeAmount = inputSum.sub(withdrawAmount);
79
83
  const outputs: Utxo[] = [];
80
84
 
81
85
  if (changeAmount.gt(0)) {
@@ -83,7 +87,8 @@ export async function withdraw({ provider: rawProvider, withdrawAmountInput, rec
83
87
  logger.debug(`Change UTXO: ${ethers.utils.formatEther(changeAmount)} ETH`);
84
88
  }
85
89
 
86
- logger.debug(`\nWithdrawing ${withdrawAmountInput} ETH to ${recipient}...`);
90
+ logger.debug(`\nWithdrawing ${realAriveAmount} ETH to ${recipient}...`);
91
+ logger.info('generating ZK proof')
87
92
 
88
93
  const { args, extData } = await prepareTransaction({
89
94
  inputs,
@@ -95,7 +100,7 @@ export async function withdraw({ provider: rawProvider, withdrawAmountInput, rec
95
100
  keyBasePath,
96
101
  });
97
102
 
98
- logger.info(`\nSending transaction to relayer`);
103
+ logger.info(`submitting transaction to relayer`);
99
104
  const response = await fetch(`${INDEXER_URL}/relayer/withdraw`, {
100
105
  method: 'POST',
101
106
  headers: {
@@ -120,6 +125,7 @@ export async function withdraw({ provider: rawProvider, withdrawAmountInput, rec
120
125
  logger.debug(`\nChange UTXO created (${ethers.utils.formatEther(changeAmount)} ETH)`);
121
126
  }
122
127
 
128
+ logger.info('confirming transaction')
123
129
  let retryTimes = 0
124
130
  let itv = 2
125
131
  // const encryptedOutputStr = Buffer.from(extData.encryptedOutput1).toString('hex')