Skip to content

Commit

Permalink
chore: update package versions and add balanceOf method to account ob…
Browse files Browse the repository at this point in the history
…jects
  • Loading branch information
Ben-Rey committed Jan 3, 2025
1 parent eb1af14 commit 24c627e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 12 deletions.
74 changes: 65 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@massalabs/wallet-provider",
"version": "3.0.0",
"version": "3.1.0",
"description": "massa's wallet provider",
"main": "dist/esm/index.js",
"module": "dist/esm/index.js",
Expand Down Expand Up @@ -54,7 +54,7 @@
],
"dependencies": {
"@hicaru/bearby.js": "^0.5.9",
"@massalabs/massa-web3": "^5.0.1-dev",
"@massalabs/massa-web3": "^5.1.0",
"@metamask/providers": "^18.1.1",
"axios": "^0.28.0",
"bs58check": "^4.0.0",
Expand Down
18 changes: 18 additions & 0 deletions src/bearbyWallet/BearbyAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ export class BearbyAccount implements Provider {
}
}

public async balanceOf(
addresses: string[],
final?: boolean,
): Promise<{ address: string; balance: bigint }[]> {
const res = await web3.massa.getAddresses(...addresses);
if (res.error) {
throw new Error(res.error?.message || 'Bearby getAddresses error');
}

return res.result.map((addressInfo) => {
const { final_balance, candidate_balance } = addressInfo;
return {
address: addressInfo.address,
balance: Mas.fromString(final ? final_balance : candidate_balance),
};
});
}

public async networkInfos(): Promise<Network> {
return networkInfos();
}
Expand Down
27 changes: 27 additions & 0 deletions src/massaStation/MassaStationAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ export class MassaStationAccount implements Provider {
return Mas.fromString(final ? balances.final : balances.pending);
}

async balanceOf(
addresses: string[],
final = false,
): Promise<{ address: string; balance: bigint }[]> {
const queryParams = new URLSearchParams();

queryParams.append('attributes', 'balance');
addresses.forEach((address) => {
queryParams.append('addresses', address);
});

const res = 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;

return {
address,
balance: Mas.fromString(final ? balance.final : balance.pending),
};
});
}

public async sign(
data: Uint8Array | string,
opts?: SignOptions,
Expand Down
16 changes: 16 additions & 0 deletions src/metamaskSnap/MetamaskAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SmartContract,
strToBytes,
rpcTypes,
Mas,
} from '@massalabs/massa-web3';
import { WalletName } from '../wallet';
import { errorHandler } from '../errors/utils/errorHandler';
Expand Down Expand Up @@ -62,6 +63,21 @@ export class MetamaskAccount implements Provider {
return BigInt(final ? finalBalance : candidateBalance);
}

async balanceOf(
addresses: string[],
final = false,
): Promise<{ address: string; balance: bigint }[]> {
const client = await getClient();
const addressesInfo = await client.getMultipleAddressInfo(addresses);

return addressesInfo.map((addressInfo) => ({
address: addressInfo.address,
balance: final
? Mas.fromString(addressInfo.final_balance)
: Mas.fromString(addressInfo.candidate_balance),
}));
}

async networkInfos(): Promise<Network> {
return networkInfos();
}
Expand Down
2 changes: 1 addition & 1 deletion src/metamaskSnap/MetamaskWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { connectSnap, isConnected } from './snap';
import { MetamaskAccount } from './MetamaskAccount';
import { MetaMaskInpageProvider } from '@metamask/providers';
import { getActiveAccount, getNetwork, setRpcUrl } from './services';
import EventEmitter from 'events';
import EventEmitter from 'eventemitter3';

const METAMASK_NETWORK_CHANGED = 'METAMASK_NETWORK_CHANGED';

Expand Down

0 comments on commit 24c627e

Please sign in to comment.