Skip to content

Commit b928c39

Browse files
committed
cli: check number of decimals
1 parent a4bcd75 commit b928c39

File tree

1 file changed

+79
-4
lines changed

1 file changed

+79
-4
lines changed

cli/src/index.ts

+79-4
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,11 @@ yargs(hideBin(process.argv))
655655

656656
const missing = await missingConfigs(deps, verbose);
657657

658+
if (checkConfigErrors(deps)) {
659+
console.error("There are errors in the config file. Please fix these before continuing.");
660+
process.exit(1);
661+
}
662+
658663
for (const [chain, missingConfig] of Object.entries(missing)) {
659664
assertChain(chain);
660665
const ntt = deps[chain]!.ntt;
@@ -733,10 +738,15 @@ yargs(hideBin(process.argv))
733738

734739
let deps: Partial<{ [C in Chain]: Deployment<Chain> }> = await pullDeployments(deployments, network, verbose);
735740

736-
let errors = 0;
741+
let fixable = 0;
737742

738743
const extraInfo: any = {};
739744

745+
if (checkConfigErrors(deps)) {
746+
console.error("There are errors in the config file. Please fix these before continuing.");
747+
process.exit(1);
748+
}
749+
740750
// diff remote and local configs
741751
for (const [chain, deployment] of Object.entries(deps)) {
742752
assertChain(chain);
@@ -748,7 +758,7 @@ yargs(hideBin(process.argv))
748758
const diff = diffObjects(a, b);
749759
if (Object.keys(diff).length !== 0) {
750760
console.error(chalk.reset(colorizeDiff(diff)));
751-
errors++;
761+
fixable++;
752762
}
753763

754764
if (verbose) {
@@ -771,7 +781,7 @@ yargs(hideBin(process.argv))
771781
const missing = await missingConfigs(deps, verbose);
772782

773783
if (Object.keys(missing).length > 0) {
774-
errors++;
784+
fixable++;
775785
}
776786

777787
for (const [chain, missingConfig] of Object.entries(missing)) {
@@ -796,7 +806,7 @@ yargs(hideBin(process.argv))
796806
}
797807
}
798808

799-
if (errors > 0) {
809+
if (fixable > 0) {
800810
console.error("Run `ntt pull` to pull the remote configuration (overwriting the local one)");
801811
console.error("Run `ntt push` to push the local configuration (overwriting the remote one) by executing the necessary transactions");
802812
process.exit(1);
@@ -834,6 +844,34 @@ yargs(hideBin(process.argv))
834844
const tokenAuthority = NTT.pdas(programId).tokenAuthority();
835845
console.log(tokenAuthority.toBase58());
836846
})
847+
.command("ata <mint> <owner> <tokenProgram>",
848+
"print the token authority address for a given program ID",
849+
(yargs) => yargs
850+
.positional("mint", {
851+
describe: "Mint address",
852+
type: "string",
853+
demandOption: true,
854+
})
855+
.positional("owner", {
856+
describe: "Owner address",
857+
type: "string",
858+
demandOption: true,
859+
})
860+
.positional("tokenProgram", {
861+
describe: "Token program ID",
862+
type: "string",
863+
choices: ["legacy", "token22"],
864+
demandOption: true,
865+
}),
866+
(argv) => {
867+
const mint = new PublicKey(argv["mint"]);
868+
const owner = new PublicKey(argv["owner"]);
869+
const tokenProgram = argv["tokenProgram"] === "legacy"
870+
? spl.TOKEN_PROGRAM_ID
871+
: spl.TOKEN_2022_PROGRAM_ID
872+
const ata = spl.getAssociatedTokenAddressSync(mint, owner, true, tokenProgram);
873+
console.log(ata.toBase58());
874+
})
837875
.demandCommand()
838876
}
839877
)
@@ -857,6 +895,31 @@ type MissingImplicitConfig = {
857895
solanaUpdateLUT: boolean;
858896
}
859897

898+
function checkConfigErrors(deps: Partial<{ [C in Chain]: Deployment<Chain> }>): number {
899+
let fatal = 0;
900+
for (const [chain, deployment] of Object.entries(deps)) {
901+
assertChain(chain);
902+
const config = deployment.config.local!;
903+
if (!checkNumberFormatting(config.limits.outbound, deployment.decimals)) {
904+
console.error(`ERROR: ${chain} has an outbound limit (${config.limits.outbound}) with the wrong number of decimals. The number should have ${deployment.decimals} decimals.`);
905+
fatal++;
906+
}
907+
if (config.limits.outbound === formatNumber(0n, deployment.decimals)) {
908+
console.warn(chalk.yellow(`${chain} has an outbound limit of 0`));
909+
}
910+
for (const [c, limit] of Object.entries(config.limits.inbound)) {
911+
if (!checkNumberFormatting(limit, deployment.decimals)) {
912+
console.error(`ERROR: ${chain} has an inbound limit with the wrong number of decimals for ${c} (${limit}). The number should have ${deployment.decimals} decimals.`);
913+
fatal++;
914+
}
915+
if (limit === formatNumber(0n, deployment.decimals)) {
916+
console.warn(chalk.yellow(`${chain} has an inbound limit of 0 from ${c}`));
917+
}
918+
}
919+
}
920+
return fatal;
921+
}
922+
860923
function createWorkTree(platform: Platform, version: string): string {
861924
const tag = getGitTagName(platform, version);
862925
if (!tag) {
@@ -1737,6 +1800,18 @@ function formatNumber(num: bigint, decimals: number) {
17371800
return formatted;
17381801
}
17391802

1803+
function checkNumberFormatting(formatted: string, decimals: number): boolean {
1804+
// check that the string has the correct number of decimals
1805+
const parts = formatted.split(".");
1806+
if (parts.length !== 2) {
1807+
return false;
1808+
}
1809+
if (parts[1].length !== decimals) {
1810+
return false;
1811+
}
1812+
return true;
1813+
}
1814+
17401815
function cargoNetworkFeature(network: Network): string {
17411816
switch (network) {
17421817
case "Mainnet":

0 commit comments

Comments
 (0)