diff --git a/tests/e2e/ans/publishANSContracts.ts b/tests/e2e/ans/publishANSContracts.ts index 1f7493768..ed37729d5 100644 --- a/tests/e2e/ans/publishANSContracts.ts +++ b/tests/e2e/ans/publishANSContracts.ts @@ -1,82 +1,54 @@ import { execSync } from "child_process"; -import "dotenv"; import { AccountAddress, Aptos, AptosApiType, Ed25519PrivateKey } from "../../../src"; import { LOCAL_ANS_ACCOUNT_PK, LOCAL_ANS_ACCOUNT_ADDRESS } from "../../../src/internal/ans"; -/** - * TS SDK supports ANS. Since ANS contract is not part of aptos-framework - * we need to get the ANS contract, publish it to local testnet and test against it. - * This script clones the aptos-names-contracts repo {@link https://github.com/aptos-labs/aptos-names-contracts}, - * uses a pre created account address and private key to fund that account and - * then publish the contract under that account. - * After the contract is published, we delete the cloned repo folder. - * - * This script runs when testing locally and on CI (as part of sdk-release.yaml) using `pnpm test`. - */ - -/* eslint-disable no-console */ -/* eslint-disable max-len */ - -// ANS account we use to publish the contract - +// Function to execute a command and return the output as a string function execCmdString(command: string): string { console.log(`Executing '${command}'`); return execSync(command, { encoding: "utf8" }); } + +// Function to execute a command with inheriting stdio function execCmdBuffer(command: string): Buffer { console.log(`Executing '${command}'`); return execSync(command, { stdio: "inherit" }); } -export async function publishAnsContract( - aptos: Aptos, -): Promise<{ address: AccountAddress; privateKey: Ed25519PrivateKey }> { +// Function to publish ANS contract +export async function publishAnsContract(aptos: Aptos): Promise<{ address: AccountAddress; privateKey: Ed25519PrivateKey }> { const ret = { address: AccountAddress.fromString(LOCAL_ANS_ACCOUNT_ADDRESS), privateKey: new Ed25519PrivateKey(LOCAL_ANS_ACCOUNT_PK), }; + try { + // Check if ANS contract is already published await aptos.account.getAccountModule({ accountAddress: LOCAL_ANS_ACCOUNT_ADDRESS, moduleName: "domains", }); console.log("ANS contract already published"); - // If it's already published, we'll skip return ret; } catch { - // If it fails, we'll publish + // If not published, proceed with publishing } try { - // 0. Create a temporary directory to clone the repo into. Note: For this to work in - // CI, it is essential that TMPDIR is set to a directory that can actually be mounted. - // Learn more here: https://stackoverflow.com/a/76523941/3846032. + // Create a temporary directory to clone the ANS repo console.log("---creating temporary directory for ANS code---"); const tempDir = execSync("mktemp -d").toString("utf8").trim(); - // 1. Clone the ANS repo into the temporary directory. + // Clone the ANS repo into the temporary directory console.log(`---cloning ANS repository to ${tempDir}---`); execSync(`git clone https://github.com/aptos-labs/aptos-names-contracts.git ${tempDir}`); - // If we're using a local CLI we just use the temp dir directly. - console.log("---running CLI using local binary---"); - // The command we use to run the CLI. - const cliInvocation = "aptos"; - // Where the CLI should look to find the ANS repo. - const repoDir = tempDir; - - // Derive the router signer address. - // TODO: We should derive this with the SDK - const ROUTER_SIGNER = `0x${ - JSON.parse( - execCmdString( - `${cliInvocation} account derive-resource-account-address --address ${LOCAL_ANS_ACCOUNT_ADDRESS} --seed "ANS ROUTER" --seed-encoding utf8`, - ), - ).Result - }`; + // Derive the router signer address + const ROUTER_SIGNER = `0x${JSON.parse( + execCmdString(`aptos account derive-resource-account-address --address ${LOCAL_ANS_ACCOUNT_ADDRESS} --seed "ANS ROUTER" --seed-encoding utf8`), + ).Result}`; console.log(`Resource account ${ROUTER_SIGNER}`); - // 2. Fund ANS account. + // Fund ANS account console.log("---funding account---"); const fundTxn = await aptos.fundAccount({ accountAddress: LOCAL_ANS_ACCOUNT_ADDRESS.toString(), @@ -85,15 +57,12 @@ export async function publishAnsContract( await aptos.waitForTransaction({ transactionHash: fundTxn.hash }); console.log(`Test account funded ${LOCAL_ANS_ACCOUNT_ADDRESS}`); - // 3. Publish the ANS modules under the ANS account. + // Publish the ANS modules under the ANS account console.log("---publishing ans modules---"); const contracts = ["core", "core_v2", "router"]; - // eslint-disable-next-line no-restricted-syntax for (const contract of contracts) { execCmdBuffer( - `${cliInvocation} move publish --package-dir ${repoDir}/${contract} --assume-yes --private-key=${LOCAL_ANS_ACCOUNT_PK} --named-addresses aptos_names=${LOCAL_ANS_ACCOUNT_ADDRESS},router=${LOCAL_ANS_ACCOUNT_ADDRESS},aptos_names_v2_1=${LOCAL_ANS_ACCOUNT_ADDRESS},aptos_names_admin=${LOCAL_ANS_ACCOUNT_ADDRESS},aptos_names_funds=${LOCAL_ANS_ACCOUNT_ADDRESS},router_signer=${ROUTER_SIGNER} --url=${aptos.config.getRequestUrl( - AptosApiType.FULLNODE, - )}`, + `aptos move publish --package-dir ${tempDir}/${contract} --assume-yes --private-key=${LOCAL_ANS_ACCOUNT_PK} --named-addresses aptos_names=${LOCAL_ANS_ACCOUNT_ADDRESS},router=${LOCAL_ANS_ACCOUNT_ADDRESS},aptos_names_v2_1=${LOCAL_ANS_ACCOUNT_ADDRESS},aptos_names_admin=${LOCAL_ANS_ACCOUNT_ADDRESS},aptos_names_funds=${LOCAL_ANS_ACCOUNT_ADDRESS},router_signer=${ROUTER_SIGNER} --url=${aptos.config.getRequestUrl(AptosApiType.FULLNODE)}`, ); } console.log("---module published---");