deepsquare-io / deepsquare-client

DeepSquare typescript SDK
10 stars 0 forks source link

Feature Request: Add avvy support to deepsquare-client #6

Open efelem opened 1 year ago

efelem commented 1 year ago

Currently, using avvy domains can be helpful for contract name resolution and testing different versions of smart contracts. However, using avvy in the deepsquare telegram bot requires some custom code. Here is what I do now in the deepsquare telegram bot code (I use a personal name for that (aditthana.avax but @Lymnah has control of deepsquare.avax) :

const AVVY = require('@avvy/client/index.cjs')
import { ethers } from 'ethers';
import DeepSquareClient from "@deepsquare/deepsquare-client";
import dotenv from 'dotenv'

dotenv.config()

// Prevent XSS Code injection
function validateRecord(record: string) {
  const regex = /^METASCHEDULER_ADDR=0x[a-fA-F0-9]{40};CREDIT_ADDR=0x[a-fA-F0-9]{40}$/;
  return regex.test(record);
}

const getContractAddresses = async () => {
  const PROVIDER_URL = 'https://api.avax.network/ext/bc/C/rpc';
  const provider = new ethers.providers.JsonRpcProvider(PROVIDER_URL);
  const avvy = new AVVY(provider, {});

  const domain = 'aditthana.avax';
  const content = await avvy.name(domain).resolve(8); // CONTENT

  if (validateRecord(content)) {
    console.log("Record is valid");
    // Extrait les adresses de contrat à partir du contenu
    const contracts = content.split(';');
    const metaSchedulerAddress = contracts[0].split('=')[1];
    const creditAddress = contracts[1].split('=')[1];

    return [metaSchedulerAddress, creditAddress]
  } else {
    console.log("Record is invalid");
    throw new Error("Record is invalid");
  }
};

const createDeepSquareClient = async () => {
  let metaSchedulerAddress;
  let creditAddress;

  try {
    [metaSchedulerAddress, creditAddress] = await getContractAddresses();
    console.log("Using addresses from domain record");
    console.log(`     metaSchedulerAddress: ${metaSchedulerAddress}`);
    console.log(`     creditAddress: ${creditAddress}`);
  } catch (error) {
    console.log("Failed to fetch addresses from domain record, using environment variables");
    metaSchedulerAddress = process.env.METASCHEDULER_ADDR as string;
    creditAddress = process.env.CREDIT_ADDR as string;
  }

  return new DeepSquareClient(
    process.env.PRIVATE_KEY as string,
    metaSchedulerAddress,
    creditAddress,
    process.env.ENDPOINT as string
  );
};

To make it easier for developers using the deepsquare-client library to keep track of the latest contracts and test new smart contracts, it would be great to have a similar interface for avvy domain name resolution directly in the library.

With avvy support, developers can use a human-readable name (.avax) to associate with different records on the c-chain. This will help to eliminate the need to keep track of the latest contract addresses.

To make this possible, the dev team can set up an avvy domain name and update the smart contract record to point to the relevant contract addresses. Once the domain is set up, the dev team can integrate avvy support directly into the deepsquare-client library.

What do you think ?

valentinpollart commented 1 year ago

To me it's a bad idea. By design, this package is conceived to match a specific smarts contract version per package version (at least a version each time a smart contract function signature changes). Since it is planned to support multiple version of this package, especially from a production-readiness prospective, this would cause any version which doesn't use the latest ABIs not to work. Actually, it is not recommended to use the package with smart contract addresses not provided by the package itself since we cannot guarantee that the features provided in the package version are compatible with smart contract version you provide (I'm even considering removing the constructor arguments for smart contracts addresses, at some point if you want to use a different smart contract version, you'll have to use the matching package version; that was just a nice to have for early development).