npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stfil/metalink-filecoin-types

v1.0.3

Published

MetaLinkFilecoin types defines the Messages and snap interaction MetaLinkFilecoin Snap API.

Downloads

7

Readme

MetaLinkFilecoin types

License: MIT License

MetaLinkFilecoin types defines the Messages and snap interaction MetaLinkFilecoin Snap API.

API Documentation

MetaLinkFilecoin Snap API

configure(configuration: Partial<SnapConfig>): Promise<void>

Configures snap for the specific network. It is possible to send custom configuration or select one from a set of predefined configurations by defining specific network.

There are two predefined configurations for testnet "t" and for mainet "f". If selecting a predefined configuration only network property is required.

export interface SnapConfig {
  derivationPath: string;
  network: FilecoinNetwork;
  rpc: {
    token: string;
    url: string
  };
  unit?: UnitConfiguration;
}

It is also possible to choose a predefined configuration and only change some specific properties. In the example SnapConfig below we selected predefined configuration for testnet network and only changed URL for RPC endpoint (rpcUrl), all other properties will be the same as in predefined configuration for testnet network.

{
  network: "t",
  rpc: {
    token: "",
    url: "test.rpc.url"
  },
}
getPublicKey(): Promise<string>

Returns the public key for the generated account.

getAddress(): Promise<string>

Returns address for the generated account.

getBalance(): Promise<string>

Return balance for the generated account.

exportPrivateKey(): Promise<string>

Return private key for the generated account.

This method will invoke Metamask prompt to confirm action

Messages

Sending a message is a two-step process (sign message, send message). First, create SignedMessage using signMessage method then send a signed message using sendMessage method. Additionally it is possible to estimate gas parameters with calculateGasForMessage method.

calculateGasForMessage(message: MessageRequest, maxFee?: string): Promise<MessageGasEstimate>

The function accepts message request object and additional optional parameter for maximum allowed fee (if omitted this will be set to 0.1 FIL)

interface MessageRequest {
  to: string;
  value: string;
  gaslimit?: number;   // leave empty
  gasfeecap?: string;  // leave empty
  gaspremium?: string; // leave empty
  nonce?: number;      // leave empty
}

Returns MessageGasEstimate with estimated values for gas parameters for the provided message, see below.

interface MessageGasEstimate {
  gaslimit: number;
  gasfeecap: string;
  gaspremium: string;
  maxfee: string
}
signMessage(message: MessageRequest): Promise<SignMessageResponse>

If gas parameters are left out then they will be filled with estimates (see estimateMessageGas function).

interface MessageRequest {
  to: string;
  value: string;
  gaslimit?: number;
  gasfeecap?: string;
  gaspremium?: string;
  nonce?: number;
}

Returns SignMessageResponse with information on sign request status.

interface SignMessageResponse {
  signedMessage: SignedMessage // signed message if sucesfull, null otherwise
  confirmed: boolean // information if user accepted to sign message
  error: Error // null if everything was sucessfull
}

If signing was successful you can find all message details and generated signature inside signedMessage prop, see below.

interface SignedMessage {
  message: Message;
  signature: MessageSignature;
}

interface Message {
  to: string;
  from: string;
  nonce: number;
  value: string;
  gasfeecap: string;
  gaspremium: string;
  gaslimit: number;
  method: number;
  params?: string;
}

interface MessageSignature {
  data: string;
  type: number;
}
signMessageRaw(message: string): Promise<SignRawMessageResponse>
interface SignRawMessageResponse {
  signature: string
  confirmed: boolean
  error: Error
}
sendMessage(signedMessage: SignedMessage): Promise<MessageStatus>
export interface SignedMessage {
  message: Message;
  signature: {
    data: string;
    type: number;
  };
}
getMessages(): Promise<MessageStatus[]>

Returns all messages saved inside the snap state (all messages sent through Filecoin snap) as MessageStatus.

interface MessageStatus {
  message: Message;
  cid: string;
}

It holds information about message parameters as Message and a unique code identifier for the message or CID.

interface Message {
  to: string;
  from: string;
  nonce: number;
  value: string;
  gasfeecap: string;
  gaspremium: string;
  gaslimit: number;
  method: number;
  params?: string;
}

MetaLinkFilecoin Snap API usage examples

Send message

// snap is already installed
const api = await MetaLinkFilecoinSnap.getApi();

const toAddress = "t1wnanhvadbski2fru4l4kry3x2hqq4jobzzic6dq"
const amountAttoFIL = "100000"

const gasEstimate = await api.calculateGasForMessage({
  to: toAddress,
  value: amountAttoFIL,
});

const partialMessage: PartialMessage = {
  to: toAddress,
  value: amountAttoFIL,
  gaslimit: gasEstimate.gaslimit,
  gasfeecap: gasEstimate.gasfeecap,
  gaspremium: gasEstimate.gaspremium,
}

const response: SignMessageResponse = await api.signMessage(partialMessage);
const messageCid = await api.sendMessage(response.signedMessage);

console.log(`Message sent with cid: ${messageCid["/"]}`);