Publish a Package
How to publish a package using the TypeScript SDK.
publishPackage function
The function lives on the client’s transaction module and takes a single named-argument object.
await supra.transaction.publishPackage({ senderAccount, packageMetadata, modulesCode })Parameters
| Name | Type |
|---|---|
| senderAccount | SupraAccount |
| packageMetadata | Uint8Array |
| modulesCode | Uint8Array[] |
| optionalTransactionArgs? | OptionalTransactionArgs |
Returns
| Name | Type |
|---|---|
| Promise<TransactionResponse> | TransactionResponse |
{
"txHash": "",
"result": ""
}Example
The publishPackage function requires the package metadata and module bytecode as the packageMetadata and modulesCode arguments. These are obtained through the build-publish-payload command of the Supra CLI. Once obtained, parse the JSON file for the data and pass it to the publishPackage function.
Build the publish payload with the Supra CLI
supra move tool build-publish-payload --package-dir /supra/configs/move_workspace/{YOUR_PROJECT_NAME} --json-output-file ./configs/move_workspace/{YOUR_PROJECT_NAME}/publish-payload.jsonInitialize a new project
npm init && npm add -D typescript @types/node ts-node && npx tsc --initInstall Supra’s TypeScript SDK
npm install supra-ts-sdkCreate publish_example.ts and import dependencies
publish_example.ts
import fs from 'fs';
import { HexString, SupraAccount, SupraClient, Network } from 'supra-ts-sdk';Parse the JSON file and extract data
publish_example.ts
function getPackageData(filePath: string) {
const jsonData = JSON.parse(fs.readFileSync(filePath, "utf8"));
const packageMetadata = new HexString(jsonData.args[0].value).toUint8Array();
const modulesCode = [];
for(let e of jsonData.args[1].value){
modulesCode.push(new HexString(e).toUint8Array())
}
return { packageMetadata, modulesCode };
}Call the publishPackage function
publish_example.ts
async function main(){
const { packageMetadata, modulesCode } = getPackageData("YOUR_PATH_TO_JSON_FILE");
const supra = new SupraClient({ network: Network.TESTNET });
const senderAccount = new SupraAccount(
new HexString("YOUR_PRIVATE_KEY").toUint8Array()
);
const publishTxn = await supra.transaction.publishPackage({
senderAccount,
packageMetadata,
modulesCode,
})
console.log(publishTxn);
}Complete code
publish_example.ts
import fs from "fs";
import { HexString, SupraAccount, SupraClient, Network } from "supra-ts-sdk";
function getPackageData(filePath: string) {
const jsonData = JSON.parse(fs.readFileSync(filePath, "utf8"));
const packageMetadata = new HexString(jsonData.args[0].value).toUint8Array();
const modulesCode = [];
for(let e of jsonData.args[1].value){
modulesCode.push(new HexString(e).toUint8Array())
}
return { packageMetadata, modulesCode };
}
async function main(){
const { packageMetadata, modulesCode } = getPackageData("YOUR_PATH_TO_JSON_FILE");
const supra = new SupraClient({ network: Network.TESTNET });
const senderAccount = new SupraAccount(
new HexString("YOUR_PRIVATE_KEY").toUint8Array()
);
const publishTxn = await supra.transaction.publishPackage({
senderAccount,
packageMetadata,
modulesCode,
})
console.log(publishTxn);
}
main()Execute code npx ts-node publish_example.ts
Last updated on