跳转到主要内容
Tenderly SDK 提供了 ContractRepository 类,用于管理与您的 Tenderly 项目相关联的合约。ContractRepository 允许您添加、更新和移除合约,以及验证它们。 初始化 Tenderly SDK 后,您可以使用 contracts 命名空间访问 ContractRepository 实例。要管理同一项目中的钱包,请参见 管理钱包

添加合约

要添加新合约,请使用 contracts 命名空间的 add 方法:
example.jsx
try {
  const contractAddress = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e';
  const contract = await tenderly.contracts.add(contractAddress, {
    displayName: 'MyContract',
  });

  console.log('Added contract:', addedContract);
} catch (error) {
  console.error('Error adding contract:', error);
}

更新合约

要更新现有合约,请使用 contracts 命名空间的 update 方法:
example.jsx
try {
  const contractAddress = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e';
  const contract = await tenderly.contracts.update(contractAddress, {
    displayName: 'My Contract',
  });
  console.log('Updated contract:', contract);
} catch (error) {
  console.error('Error updating contract:', error);
}

移除合约

要移除合约,请使用 contracts 命名空间的 remove 方法:
example.jsx
try {
  const contractAddress = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e';
  await tenderly.contracts.remove(contractAddress);
  console.log('Contract removed successfully');
} catch (error) {
  console.error('Error removing contract:', error);
}

验证合约

要验证合约,您可以使用 contracts 命名空间的 verify 方法。 以下三个示例展示了 tenderly.contracts.verify 方法的用法:
  • 第一个示例展示了验证一个没有依赖的简单 Counter 合约。
  • 第二个示例展示了验证一个具有多个依赖(其他合约)的 MyToken 合约。
  • 第三个示例展示了验证一个具有库依赖的 LibraryToken 合约。
您可以在我们的 tenderly-sdk 仓库的 examples 文件夹 中找到这些示例以及启动它们的说明。这里仅展示带有几个依赖的 MyToken 合约,让您了解如何导入它们:
example.js
const myTokenAddress = `0x1a273A64C89CC45aBa798B1bC31B416A199Be3b3`.toLowerCase() as Web3Address;
const ROOT_FOLDER = `examples/contractVerification/withDependencies/contracts`;

const tenderly = new Tenderly({
  accessKey: process.env.TENDERLY_ACCESS_KEY || ``,
  accountName: process.env.TENDERLY_ACCOUNT_NAME || ``,
  projectName: process.env.TENDERLY_PROJECT_NAME || ``,
  network: Network.SEPOLIA,
});

const result = await tenderly.contracts.verify(myTokenAddress, {
  config: {
    mode: `public`,
  },
  contractToVerify: `${ROOT_FOLDER}/MyToken.sol:MyToken`,
  solc: {
    version: `v0.8.19`,
    sources: {
      [`${ROOT_FOLDER}/MyToken.sol`]: {
        content: readFileSync(`${ROOT_FOLDER}/MyToken.sol`, `utf8`),
      },
      [`@openzeppelin/contracts/token/ERC20/ERC20.sol`]: {
        content: readFileSync(`${ROOT_FOLDER}/ERC20.sol`, `utf8`),
      },
      [`@openzeppelin/contracts/token/ERC20/IERC20.sol`]: {
        content: readFileSync(`${ROOT_FOLDER}/IERC20.sol`, `utf8`),
      },
      [`@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol`]: {
        content: readFileSync(`${ROOT_FOLDER}/IERC20Metadata.sol`, `utf8`),
      },
      [`@openzeppelin/contracts/utils/Context.sol`]: {
        content: readFileSync(`${ROOT_FOLDER}/Context.sol`, `utf8`),
      },
    },
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
      },
    },
  },
});

console.log(`Result:`, result);