Skip to content

Commit

Permalink
refactor MS wallet api error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Feb 12, 2025
1 parent 426b255 commit c828e7b
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 138 deletions.
95 changes: 46 additions & 49 deletions src/massaStation/MassaStationAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ export class MassaStationAccount implements Provider {
}

public async balance(final = false): Promise<bigint> {
const res = await getRequest<MSBalancesResp>(
const { result } = await getRequest<MSBalancesResp>(
`${MASSA_STATION_URL}massa/addresses?attributes=balance&addresses=${this.address}`,
);

if (res.isError) throw res.error;

const balances = res.result.addressesAttributes[this.address].balance;
const balances = result.addressesAttributes[this.address].balance;

return Mas.fromString(final ? balances.final : balances.pending);
}
Expand All @@ -87,14 +85,12 @@ export class MassaStationAccount implements Provider {
queryParams.append('addresses', address);
});

const res = await getRequest<MSBalancesResp>(
const { result } = await getRequest<MSBalancesResp>(
`${MASSA_STATION_URL}massa/addresses?${queryParams.toString()}`,
);

if (res.isError) throw res.error;

return addresses.map((address) => {
const balance = res.result.addressesAttributes[address].balance;
const balance = result.addressesAttributes[address].balance;

return {
address,
Expand All @@ -113,24 +109,24 @@ export class MassaStationAccount implements Provider {
DisplayData: opts?.displayData ?? true,
};

const res = await postRequest<MSAccountSignResp>(
`${walletApiUrl()}/accounts/${this.accountName}/signMessage`,
signData,
);

if (res.isError) {
throw errorHandler(operationType.Sign, res.error);
}
try {
const { result } = await postRequest<MSAccountSignResp>(
`${walletApiUrl()}/accounts/${this.accountName}/signMessage`,
signData,
);

// MS Wallet encodes signature in base64... so we need to decode it en re-encode it in base58
const signature = bs58check.encode(
await base64ToByteArray(res.result.signature),
);
// MS Wallet encodes signature in base64... so we need to decode it en re-encode it in base58
const signature = bs58check.encode(
await base64ToByteArray(result.signature),
);

return {
publicKey: res.result.publicKey,
signature,
};
return {
publicKey: result.publicKey,
signature,
};
} catch (error) {
throw errorHandler(operationType.Sign, error);
}
}

private async minimalFee(): Promise<bigint> {
Expand All @@ -150,16 +146,16 @@ export class MassaStationAccount implements Provider {
amount: amount.toString(),
side: type === operationType.BuyRolls ? 'buy' : 'sell',
};
try {
const { result } = await postRequest<MSSendOperationResp>(
`${walletApiUrl()}/accounts/${this.accountName}/rolls`,
body,
);

const res = await postRequest<MSSendOperationResp>(
`${walletApiUrl()}/accounts/${this.accountName}/rolls`,
body,
);

if (res.isError) {
throw errorHandler(type, res.error);
return new Operation(this, result.operationId);
} catch (error) {
throw errorHandler(type, error);
}
return new Operation(this, res.result.operationId);
}

public async buyRolls(
Expand Down Expand Up @@ -189,16 +185,16 @@ export class MassaStationAccount implements Provider {
recipientAddress: to.toString(),
};

const res = await postRequest<MSSendOperationResp>(
`${walletApiUrl()}/accounts/${this.accountName}/transfer`,
body,
);
try {
const { result } = await postRequest<MSSendOperationResp>(
`${walletApiUrl()}/accounts/${this.accountName}/transfer`,
body,
);

if (res.isError) {
throw errorHandler(operationType.SendTransaction, res.error);
return new Operation(this, result.operationId);
} catch (error) {
throw errorHandler(operationType.SendTransaction, error);
}

return new Operation(this, res.result.operationId);
}

public async callSC(params: CallSCParams): Promise<Operation> {
Expand All @@ -224,15 +220,16 @@ export class MassaStationAccount implements Provider {
async: true,
};

const res = await postRequest<MSSendOperationResp>(
`${MASSA_STATION_URL}cmd/executeFunction`,
body,
);
try {
const { result } = await postRequest<MSSendOperationResp>(
`${MASSA_STATION_URL}cmd/executeFunction`,
body,
);

if (res.isError) {
throw errorHandler('callSmartContract', res.error);
return new Operation(this, result.operationId);
} catch (error) {
throw errorHandler('callSmartContract', error);
}
return new Operation(this, res.result.operationId);
}

public async networkInfos(): Promise<Network> {
Expand Down Expand Up @@ -290,12 +287,12 @@ export class MassaStationAccount implements Provider {
)} $MAS fee for operation`,
};

const res = await postRequest<MSSendOperationResp>(
const { result } = await postRequest<MSSendOperationResp>(
`${MASSA_STATION_URL}cmd/deploySC`,
body,
);

const operationId = res.result?.operationId;
const operationId = result?.operationId;

if (!operationId) throw new Error('Operation ID not found');

Expand Down
27 changes: 16 additions & 11 deletions src/massaStation/MassaStationDiscovery.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { MASSA_STATION_URL } from './MassaStationWallet';
import { JsonRpcResponseData, getRequest } from './RequestHandler';
import { PluginInfo } from './types';

// Constants for URLs and plugin information
const MASSA_STATION_URL = 'https://station.massa/plugin-manager';
const PLUGIN_NAME = 'Massa Wallet';
const PLUGIN_AUTHOR = 'Massa Labs';
const TIMEOUT = 4000;

async function fetchPluginData(): Promise<JsonRpcResponseData<PluginInfo[]>> {
return getRequest<PluginInfo[]>(MASSA_STATION_URL, TIMEOUT);
return getRequest<PluginInfo[]>(
MASSA_STATION_URL + 'plugin-manager',
TIMEOUT,
);
}

function findWalletPlugin(plugins: PluginInfo[]): PluginInfo | undefined {
Expand All @@ -18,18 +21,20 @@ function findWalletPlugin(plugins: PluginInfo[]): PluginInfo | undefined {
}

export async function isMassaStationAvailable(): Promise<boolean> {
const response = await fetchPluginData();
return !response.isError;
try {
await fetchPluginData();
return true;
} catch (_) {
return false;
}
}

export async function isMassaWalletEnabled(): Promise<boolean> {
const response = await fetchPluginData();

if (response.isError) {
console.warn('Error fetching plugin data:', response.error);
try {
const { result } = await fetchPluginData();
const walletPlugin = findWalletPlugin(result);
return walletPlugin && walletPlugin.status === 'Up';
} catch (_) {
return false;
}

const walletPlugin = findWalletPlugin(response.result);
return walletPlugin && walletPlugin.status === 'Up';
}
46 changes: 10 additions & 36 deletions src/massaStation/MassaStationWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ export class MassaStationWallet implements Wallet {
public async accounts(): Promise<MassaStationAccount[]> {
const res = await getRequest<MSAccountsResp>(walletApiUrl() + '/accounts');

if (res.isError) {
throw res.error;
}
return res.result
.filter((account) => {
return account.status === MassaStationAccountStatus.OK;
Expand All @@ -83,13 +80,10 @@ export class MassaStationWallet implements Wallet {
publicKey: string,
privateKey: string,
): Promise<void> {
const res = await putRequest(walletApiUrl() + '/accounts', {
await putRequest(walletApiUrl() + '/accounts', {
publicKey,
privateKey,
});
if (res.isError) {
throw res.error;
}
}

public async deleteAccount(address: string): Promise<void> {
Expand All @@ -98,8 +92,6 @@ export class MassaStationWallet implements Wallet {
walletApiUrl() + '/accounts',
);

if (allAccounts.isError) throw allAccounts.error;

const accountToDelete = allAccounts.result.find(
(account) => account.address === address,
);
Expand All @@ -108,13 +100,9 @@ export class MassaStationWallet implements Wallet {
throw new Error('Account not found');
}

const res = await deleteRequest<unknown>(
await deleteRequest<unknown>(
`${walletApiUrl()}/accounts/${accountToDelete.nickname}`,
);

if (res.isError) {
throw res.error;
}
}

public async networkInfos(): Promise<Network> {
Expand All @@ -139,8 +127,6 @@ export class MassaStationWallet implements Wallet {
{},
);

if (response.isError) throw response.error;

return new MassaStationAccount(
response.result.address,
response.result.nickname,
Expand Down Expand Up @@ -222,11 +208,8 @@ export class MassaStationWallet implements Wallet {
* @returns The configuration of MS wallet.
*/
public async getConfig(): Promise<Config> {
const res = await getRequest<Config>(walletApiUrl() + '/config');
if (res.isError) {
throw res.error;
}
return res.result;
const { result } = await getRequest<Config>(walletApiUrl() + '/config');
return result;
}

/**
Expand All @@ -242,7 +225,7 @@ export class MassaStationWallet implements Wallet {
rule: SignRule,
desc?: string,
): Promise<AddUpdateSignRuleResponse> {
const res = await postRequest<AddUpdateSignRuleResponse>(
const { result } = await postRequest<AddUpdateSignRuleResponse>(
walletApiUrl() + '/accounts/' + accountName + '/signrules',
{
description: desc,
Expand All @@ -252,10 +235,7 @@ export class MassaStationWallet implements Wallet {
enabled: rule.enabled,
},
);
if (res.isError) {
throw res.error;
}
return res.result;
return result;
}

/**
Expand All @@ -272,7 +252,7 @@ export class MassaStationWallet implements Wallet {
rule: SignRule,
desc?: string,
): Promise<AddUpdateSignRuleResponse> {
const res = await putRequest<AddUpdateSignRuleResponse>(
const { result } = await putRequest<AddUpdateSignRuleResponse>(
walletApiUrl() + '/accounts/' + accountName + '/signrules/' + rule.id,
{
description: desc,
Expand All @@ -282,11 +262,8 @@ export class MassaStationWallet implements Wallet {
enabled: rule.enabled,
},
);
if (res.isError) {
console.log('error', res);
throw res.error;
}
return res.result;

return result;
}

/**
Expand All @@ -300,11 +277,8 @@ export class MassaStationWallet implements Wallet {
accountName: string,
ruleId: string,
): Promise<void> {
const res = await deleteRequest(
await deleteRequest(
walletApiUrl() + '/accounts/' + accountName + '/signrules/' + ruleId,
);
if (res.isError) {
throw res.error;
}
}
}
Loading

0 comments on commit c828e7b

Please sign in to comment.