Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update publishANSContracts.ts #329

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 17 additions & 48 deletions tests/e2e/ans/publishANSContracts.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,71 @@
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";

/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets keep this block explaining how we fetch and publish ANS contract?

* 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 */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need this lint disabled, if not it complains

/* 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}'`);

Check warning on line 7 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
return execSync(command, { encoding: "utf8" });
}

// Function to execute a command with inheriting stdio
function execCmdBuffer(command: string): Buffer {
console.log(`Executing '${command}'`);

Check warning on line 13 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
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");

Check warning on line 30 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
// 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---");

Check warning on line 38 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
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}---`);

Check warning on line 42 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
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`),

Check failure on line 47 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

This line has a length of 149. Maximum allowed is 130
).Result}`;
console.log(`Resource account ${ROUTER_SIGNER}`);

Check warning on line 49 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement

// 2. Fund ANS account.
// Fund ANS account
console.log("---funding account---");

Check warning on line 52 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
const fundTxn = await aptos.fundAccount({
accountAddress: LOCAL_ANS_ACCOUNT_ADDRESS.toString(),
amount: 100_000_000_000,
});
await aptos.waitForTransaction({ transactionHash: fundTxn.hash });
console.log(`Test account funded ${LOCAL_ANS_ACCOUNT_ADDRESS}`);

Check warning on line 58 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement

// 3. Publish the ANS modules under the ANS account.
// Publish the ANS modules under the ANS account
console.log("---publishing ans modules---");

Check warning on line 61 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
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)}`,

Check failure on line 65 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

This line has a length of 442. Maximum allowed is 130
);
}
console.log("---module published---");

Check warning on line 68 in tests/e2e/ans/publishANSContracts.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement

return ret;
} catch (error: any) {
Expand Down
Loading