Skip to content

API Documentation

SotaTek-TungNguyen2 edited this page Jan 11, 2024 · 5 revisions

Configurations

mina_networkConfig
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_networkConfig",
    },
  },
});

Returns the current network configurations

type ResponseNetworkConfig = {
  name: string;
  gqlUrl: string;
  gqlTxUrl: string;
  explorerUrl: string;
  token: {
    name: string;
    coinType: number;
    symbol: string;
    decimals: number;
  };
};

mina_changeNetwork
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_changeNetwork",
      params: {
        networkName: NetworkName,
      },
    },
  },
});

The accepted values of the NetworkName are:

enum ENetworkName {
  MAINNET = "Mainnet",
  DEVNET = "Devnet",
  BERKELEY = "Berkeley",
}

Changes to the selected network and returns the selected network configurations.

type NetworkConfig = {
  name: string;
  gqlUrl: string;
  gqlTxUrl: string;
  explorerUrl: string;
  token: {
    name: string;
    coinType: number;
    symbol: string;
    decimals: number;
  };
};

Account

mina_createAccount
const accountName: string = "Account1";
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_createAccount",
      params: {
        name: accountName,
      },
    },
  },
});

Creates and returns a new account

type CreateAccountResult = {
  name: string;
  address: string;
  balance: string;
  inferredNonce: string;
};

mina_accountList
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_accountList",
    },
  },
});

Returns the accounts of the user.

type AccountListResult = {
  name: string;
  address: string;
  index?: number;
  balance?: { total: string };
  isImported?: boolean;
}[];

mina_changeAccount
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_changeAccount",
      params: {
        accountIndex,
        isImported,
      },
    },
  },
});

Change the current account to the selected account.


mina_accountInfo
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_accountInfo",
    },
  },
});

Get current account information

To get the token account, you need to pass a tokenId: string as a parameter:

const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_accountInfo",
      params: { tokenId }
    },
  },
});
type Account = {
  name: string;
  publicKey: string;
  balance: { total: string };
  inferredNonce: string;
  nonce: string;
};

mina_importAccountByPrivateKey
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_importAccountByPrivateKey",
      params: {
        name,
        privateKey,
      },
    },
  },
});

Import an account with its private key

type ImportAccountResult = {
  name: string;
  address: string;
  balance: string;
  inferredNonce: string;
};

mina_exportPrivateKey
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_exportPrivateKey",
      params: {
        index, //number
        isImport, //optional boolean
      },
    },
  },
});

Return private key of the current account

type ExportPrivateKeyResult = {
  privateKey: string;
};

mina_editAccountName
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_editAccountName",
      params: {
        name, //string
        index, //number
        isImport, //optional boolean
      },
    },
  },
});

Edit the name of an account


Transaction


mina_signMessage
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_signMessage",
      params: {
        message,
      },
    },
  },
});

Sign custom message

type SignatureResult = {
  data: any;
  publickey: string;
  signature: {
    field: string;
    scalar: string;
  };
};

mina_verifyMessage
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_verifyMessage",
      params: signedData, //type SignatureResult
    },
  },
});

Verify signed message. Return a boolean


mina_sendPayment
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_sendPayment",
      params: {
        to,
        amount,
        fee,
        memo,
        nonce,
        validUntil,
      },
    },
  },
});

Transfer token on Mina Protocol

type SendTransactionResult = {
  id: string;
  hash: string;
  isDelegation: boolean;
  kind: string;
};

mina_getTxDetail
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_getTxDetail",
      params: {
        hash,
      },
    },
  },
});

Return transaction detail by transaction hash

type SendTransactionResult = {
  id: string;
  hash: string;
  isDelegation: boolean;
  kind: string;
};

mina_getTxHistory
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_getTxHistory",
      params: {
        limit, //number
        sortBy, // "DATETIME_DESC", "DATETIME_ASC"
        canonical, //boolean
      },
    },
  },
});

Return transaction history of the current address

type TransactionListResult = {
  amount: number;
  dateTime: string;
  failureReason: string;
  fee: number;
  feeToken: string;
  hash: string;
  id: string;
  isDelegation: boolean;
  kind: string;
  memo: string;
  nonce: number;
  receiver: {
    publicKey: string;
  };
  source: {
    publicKey: string;
  };
  status: string;
}[];

mina_sendTransaction
const result = await ethereum.request({
  method: "wallet_invokeSnap",
  params: {
    snapId: "npm:mina-portal",
    request: {
      method: "mina_sendTransaction",
      params: {
        transaction, // JSON
        feePayer: {
          fee, //string
          memo, //string
        },
      },
    },
  },
});

Send ZkApp transaction


Send ZK Transaction Example

import { Mina, isReady, PublicKey, fetchAccount, Field } from 'o1js';

(async() => {
    await isReady;
    const { <Your smart contract class name> } = await import(<Path to your built smart contract>);
    const zkAppAddress = <Your deployed smart contract address>
    const graphqlEndpoint = <replace the graphql endpoint here>
    const zkApp = new <Your smart contract class name>(PublicKey.fromBase58(zkAppAddress));
    Mina.setActiveInstance(Mina.Network(<graphqlEndpoint>));
    <Your smart contract class name>.compile();
    const account = await fetchAccount({publicKey: zkAppAddress, ...zkApp}, <graphqlEndpoint>);
    const tx = await Mina.transaction({
        sender: PublicKey.fromBase58(<sender address>),
        fee: <replace the fee here>
    }, () => {
        zkApp.update(<your parameter>);
    });
    const provedTx = await tx.prove();
    await ethereum.request({
      method: 'wallet_invokeSnap',
      params: {
        snapId: <the mina snap id>,
        request: {
          method: 'mina_sendTransaction',
          params: {
            transaction: tx.toJSON(),
            feePayer: {
              fee: <replace the fee here>,
              memo: <user' memo>,
            }
          }
        },
      },
    });
})();
Clone this wiki locally