Skip to content

Instantly share code, notes, and snippets.

@janewang
Last active March 31, 2022 22:34
Show Gist options
  • Save janewang/da68454ba30c40929743542f31cb2997 to your computer and use it in GitHub Desktop.
Save janewang/da68454ba30c40929743542f31cb2997 to your computer and use it in GitHub Desktop.
Pool details
const nearAPI = require('near-api-js')
const { keyStores, KeyPair, connect } = nearAPI;
const keyStore = new keyStores.InMemoryKeyStore();
// const PRIVATE_KEY =
// "by8kdJoJHu7uUkKfoaLd2J2Dp1q1TigeWMG123pHdu9UREqPcshCM223kWadm";
// // creates a public / private key pair using the provided private key
// const keyPair = KeyPair.fromString(PRIVATE_KEY);
// // adds the keyPair you created to keyStore
// await keyStore.setKey("testnet", "example-account.testnet", keyPair);
// create config
const config = {
networkId: "mainnet",
keyStore: keyStore,
nodeUrl: "https://rpc.mainnet.near.org",
walletUrl: "https://wallet.mainnet.near.org",
helperUrl: "https://helper.mainnet.near.org",
explorerUrl: "https://explorer.mainnet.near.org",
};
async function routes(fastify, options) {
fastify.get('/pool', async (request, reply) => {
// connect to NEAR
const near = await connect(config);
// get account
const account = await near.account("name.near");
// get contract
const contract = new nearAPI.Contract(
account, // the account object that is connecting
"name.near",
{
// name of contract you're connecting to
viewMethods: [], // view methods do not change state but usually return a value
changeMethods: [], // change methods modify state
sender: account, // account object to initialize and sign transactions.
}
);
// pool details
const response = await account.viewFunction('name.near', 'get_fields_by_pool', {'pool_id': 'zavodil.poolv1.near'})
return { response }
})
}
module.exports = routes
@packetstracer
Copy link

packetstracer commented Mar 31, 2022

maybe this code is not needed (but don't really know), because you're using the contract as an account and calling the viewFunction, instead of loading the contract and operating with it

      // get contract
      const contract = new nearAPI.Contract(
        account, // the account object that is connecting
        "name.near",
        {
          // name of contract you're connecting to
          viewMethods: [], // view methods do not change state but usually return a value
          changeMethods: [], // change methods modify state
          sender: account, // account object to initialize and sign transactions.
        }
      );

operating with the contract:

      const contract = new nearAPI.Contract(account, 'name.near', {
        viewMethods: ['get_all_fields', 'get_num_pools'],
        sender: account,
      })

      const poolsCount = await contract.get_num_pools()
      const pools = await contract.get_all_fields({ from_index: i * 100, limit: 100 })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment