Add connectMember method

This commit is contained in:
Sosthene 2024-11-22 16:11:47 +01:00
parent 322daf50a1
commit 9f76885b91

View File

@ -103,6 +103,50 @@ export default class Services {
return apiReturn;
}
public async connectMember(members: Member[]): Promise<void> {
if (members.length === 0) {
throw new Error('Trying to connect to empty members list');
}
const members_str = members.map(member => JSON.stringify(member));
const waitForAmount = async (): Promise<BigInt> => {
let attempts = 3;
while (attempts > 0) {
const amount = this.getAmount();
if (amount !== 0n) {
return amount;
}
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');
};
let availableAmt = this.getAmount();
if (availableAmt === 0n) {
const faucetMsg = this.createFaucetMessage();
this.sendFaucetMessage(faucetMsg);
try {
availableAmt = await waitForAmount();
} catch (e) {
console.error('Failed to retrieve amount:', e);
throw e; // Rethrow the error if needed
}
}
try {
const apiReturn = this.sdkClient.create_connect_transaction(members_str, 1);
await this.handleApiReturn(apiReturn);
} catch (e) {
console.error('Failed to connect:', e);
}
}
public async sendPairingTx(spAddress: string): Promise<void> {
const localAddress = this.sdkClient.get_address();
const emptyTxid = '0'.repeat(64);