[bug] check for non zero amount before calling the faucet
This commit is contained in:
parent
d6358c089e
commit
2c3ba1ec92
@ -13,7 +13,7 @@ export const U32_MAX = 4294967295;
|
|||||||
|
|
||||||
const storageUrl = `https://demo.4nkweb.com/storage`;
|
const storageUrl = `https://demo.4nkweb.com/storage`;
|
||||||
const BOOTSTRAPURL = [`https://demo.4nkweb.com/ws/`];
|
const BOOTSTRAPURL = [`https://demo.4nkweb.com/ws/`];
|
||||||
|
const DEFAULTAMOUNT = 1000n;
|
||||||
|
|
||||||
export default class Services {
|
export default class Services {
|
||||||
private static initializing: Promise<Services> | null = null;
|
private static initializing: Promise<Services> | null = null;
|
||||||
@ -195,39 +195,44 @@ export default class Services {
|
|||||||
|
|
||||||
const members_str = members.map((member) => JSON.stringify(member));
|
const members_str = members.map((member) => JSON.stringify(member));
|
||||||
|
|
||||||
const waitForAmount = async (): Promise<BigInt> => {
|
try {
|
||||||
let attempts = 3;
|
// Ensure the amount is available before proceeding
|
||||||
while (attempts > 0) {
|
await this.ensureSufficientAmount();
|
||||||
const amount = this.getAmount();
|
return this.sdkClient.create_connect_transaction(members_str, 1);
|
||||||
if (amount !== 0n) {
|
} catch (e) {
|
||||||
return amount;
|
console.error('Failed to connect member:', e);
|
||||||
}
|
throw e;
|
||||||
attempts--;
|
}
|
||||||
if (attempts > 0) {
|
}
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new Error('Amount is still 0 after 3 attempts');
|
|
||||||
};
|
|
||||||
|
|
||||||
|
private async ensureSufficientAmount(): Promise<void> {
|
||||||
let availableAmt = this.getAmount();
|
let availableAmt = this.getAmount();
|
||||||
if (availableAmt === 0n) {
|
const target: BigInt = DEFAULTAMOUNT * BigInt(2);
|
||||||
|
|
||||||
|
if (availableAmt < target) {
|
||||||
const faucetMsg = this.createFaucetMessage();
|
const faucetMsg = this.createFaucetMessage();
|
||||||
this.sendFaucetMessage(faucetMsg);
|
this.sendFaucetMessage(faucetMsg);
|
||||||
|
|
||||||
try {
|
await this.waitForAmount(target);
|
||||||
availableAmt = await waitForAmount();
|
}
|
||||||
} catch (e) {
|
}
|
||||||
console.error('Failed to retrieve amount:', e);
|
|
||||||
throw e; // Rethrow the error if needed
|
private async waitForAmount(target: BigInt): Promise<BigInt> {
|
||||||
|
let attempts = 3;
|
||||||
|
|
||||||
|
while (attempts > 0) {
|
||||||
|
const amount = this.getAmount();
|
||||||
|
if (amount >= target) {
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
attempts--;
|
||||||
|
if (attempts > 0) {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
throw new Error('Amount is still 0 after 3 attempts');
|
||||||
return this.sdkClient.create_connect_transaction(members_str, 1);
|
|
||||||
} catch (e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createMessagingProcess(otherMembers: Member[],relayAddress: string, feeRate: number): Promise<ApiReturn> {
|
public async createMessagingProcess(otherMembers: Member[],relayAddress: string, feeRate: number): Promise<ApiReturn> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user