@hicaru/bearby.js
v0.6.1
Published
The web3 inject of bearby walet to access massa blockchain
Downloads
942
Maintainers
Readme
Introduction
The main web3 module of bearby wallet.
The following table provides a description of each module and what you may want to use it for.
| Package | Version | Description | Dependencies |
| ------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| @hicaru/bearby.js | | Core abstractions and base classes, such as
ContentProvider and network logic for interfacing with the bearby wallet. | |
Installation
$ yarn # or npm installrun dev mode
$ yarn dev # or npm run devbuilding
$ yarn build # or npm run buildQuick Start
In your project:
yarn add @hicaru/bearby.js # or npm install @hicaru/bearby.jsimport (this script works only in the browser!)
import { web3 } from '@hicaru/bearby.js';states:
import { web3 } from '@hicaru/bearby.js';
web3.contract // the instance for read, call, deploy contracts
web3.massa // JsonRPC methods connection to nodes.
web3.wallet // main statements of the walletweb3.wallet:
web3.wallet.connectedthe bool type,falseif user didn't connected to website.web3.wallet.enabledthe bool type,falseif wallet locked.web3.wallet.accountthe account object, has methods and properties about current address.web3.wallet.networkthe network object, has methods and properties about current network.web3.wallet.connect()the method, after call shows a popup with website information, user can approve or reject.web3.wallet.signMessage("any message to sign")the method for sign any messages, shows a popup about message info.web3.wallet.signTransaction(params)the method for sending transaction to network.web3.wallet.isMassaAddressThe method validation of massa base58 address
Account observer:
import { web3 } from '@hicaru/bearby.js';
/// Emit everytime when address been changed, or changed wallet status.
const observer = web3.wallet.account.subscribe((base58) => console.log(base58));
/// To avoid memory leak don't forget unsubscribe!!!
observer.unsubscribe();Network observer:
import { web3 } from '@hicaru/bearby.js';
/// Emit everytime when network been changed.
const observer = web3.wallet.network.subscribe((net) => console.log(net))
/// To avoid memory leak don't forget unsubscribe!!!
observer.unsubscribe();Contracts:
deploy
import { web3 } from '@hicaru/bearby.js';
const hash = await bearby.contract.deploy({
fee: 0,
maxGas: 4_200_000_000,
maxCoins: 0.1 * 10**9,
coins: 100000000n,
parameters: [
{
type: bearby.contract.types.STRING,
value: "Hello, World!"
}
],
deployerBase64: "AGFzbQEAAAABWA9gAX8Bf2AAAGACf38AAAAAAAAAAAIAAAACAAAAA=",
contractDataBase64: "AGFzbQEAAAABKQhgAX8AYAJ/fbWFw"
});call
import { web3, ArgTypes } from '@hicaru/bearby.js';
const hash = await web3.contract.call({
maxGas: 2000000,
coins: 0,
targetAddress: 'A12KqAUVvPZAAybdmJijkKbynfJeDUsfztEUh8JCSx6DPjczdYLt',
functionName: 'transfer',
parameters: [
{
type: ArgTypes.STRING,
value: "Hello, World!"
},
{
type: ArgTypes.BOOL,
value: true
},
{
type: globalThis.bearby.contract.types.F64,
value: 32
},
{
type: globalThis.bearby.contract.types.U256,
value: '435435345234324324324323243242398854684'
}
]
});
// UNSAFE_PARAMS:
import { Args } from "@massalabs/massa-web3";
const args = new Args()
.addString("Hello word")
.addBool(true)
.addF64(0.3);
const hash = await web3.contract.call({
maxGas: 2000000,
coins: 0,
targetAddress: 'A12KqAUVvPZAAybdmJijkKbynfJeDUsfztEUh8JCSx6DPjczdYLt',
functionName: 'transfer',
unsafeParameters: args.serialize()
});read
import { web3, Args } from '@hicaru/bearby.js';
const data = await web3.contract.readSmartContract({
fee: 0,
maxGas: 200000,
simulatedGasPrice: 0,
targetAddress: 'A12KqAUVvPZAAybdmJijkKbynfJeDUsfztEUh8JCSx6DPjczdYLt',
targetFunction: "balanceOf",
parameter: []
});
console.log(data);events
import { web3 } from '@hicaru/bearby.js';
const eventsFilter = {
start: null,
end: null,
original_caller_address: null,
original_operation_id: hash,
emitter_address: null,
};
const response = await web3.contract.getFilteredSCOutputEvent(eventsFilter);
if (response && response.result && response.result[0] && response.result[0].data) {
const contract = String(response.result[0].data).replace('Address:', '');
console.log(contract);
}Request pubkey.
import { web3 } from '@hicaru/bearby.js';
const pubkey = await web3.wallet.requestPubKey();types
import { JsonRPCResponseExecuteReadOnlyBytecode } from '@hicaru/bearby.js/types';
const res?: JsonRPCResponseExecuteReadOnlyBytecode;