Created
October 15, 2024 09:36
-
-
Save helderjnpinto/2b7af6ff1832ad47988ab9ed2627b079 to your computer and use it in GitHub Desktop.
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
import { ArgumentsCamelCase, Argv } from "yargs"; | |
import { logger } from "../../logger"; | |
import { BigNumber } from "@polymeshassociation/polymesh-sdk"; | |
import { polymesh } from "../../helpers/polymesh"; | |
import { green, red } from 'picocolors'; | |
import { sleep } from "../../helpers/utils"; | |
interface IssueTokensArgv { | |
name: string; | |
ticker: string; | |
amount: string; // Amount to issue, default to "1000" (we'll parse this to a number) | |
} | |
export const issueTokensCommand = { | |
command: "issue-tokens", | |
describe: "Issue tokens for a given asset", | |
aliases: ["it"], | |
builder: (yargs: Argv): Argv<IssueTokensArgv> => { | |
return yargs | |
.option("name", { | |
type: "string", | |
alias: "n", | |
describe: | |
"Name of the issuer (distributor agent or owner) in the keystore", | |
demandOption: true, | |
}) | |
.option("ticker", { | |
type: "string", | |
alias: "t", | |
describe: "The ticker of the asset for which tokens will be issued", | |
demandOption: true, | |
}) | |
.option("amount", { | |
type: "string", | |
alias: "a", | |
describe: "The amount of tokens to issue", | |
default: "1000", // Default to 1000 tokens | |
}); | |
}, | |
handler: async (argv: ArgumentsCamelCase<IssueTokensArgv>) => { | |
try { | |
const { name: keyStoreName, ticker, amount } = argv; | |
if (!keyStoreName || !ticker) { | |
throw new Error("Invalid arguments: name and ticker are required"); | |
} | |
// Load Polymesh client using the keystore | |
const { polyClient: api, userDID } = await polymesh(keyStoreName, false); | |
logger.info(`Connected as: ${userDID}`); | |
// Fetch asset by ticker | |
logger.info(`Fetching asset with ticker: ${ticker}`); | |
const asset = await api.assets.getFungibleAsset({ ticker }); | |
// Issue tokens | |
logger.info(`Issuing ${amount} tokens for asset ${ticker}...`); | |
const issueTokensProcedure = await asset.issuance.issue({ | |
amount: new BigNumber(amount), | |
}); | |
issueTokensProcedure.onProcessedByMiddleware(async () => { | |
console.log( | |
"Retrieving transaction for hash:", | |
issueTokensProcedure.receipt?.txHash.toString(), | |
); | |
const transaction = await api.network.getTransactionByHash({ | |
// @ts-ignore | |
txHash: issueTokensProcedure.receipt?.txHash.toString(), | |
}); | |
console.log(transaction); | |
}); | |
// Run the issuance transaction | |
await issueTokensProcedure.run(); | |
logger.info(green(`Tokens issued successfully!`)); | |
logger.info(`TX Status: ${issueTokensProcedure.status}`); | |
logger.info(`Block Hash: ${issueTokensProcedure.blockHash}`); | |
logger.info(`Transaction Hash: ${issueTokensProcedure.receipt?.txHash}`); | |
await sleep(100000) | |
} catch (error) { | |
logger.error(red("An error occurred while issuing tokens:"), error); | |
} finally { | |
process.exit(0); | |
} | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment