-
Notifications
You must be signed in to change notification settings - Fork 52
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
||
/** | ||
* 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 */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}'`); | ||
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(), | ||
amount: 100_000_000_000, | ||
}); | ||
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---"); | ||
|
||
return ret; | ||
} catch (error: any) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?