Publish a Package
How to publish a package using the TypeScript SDK.
publishPackage
function
publishPackage
functionawait supraClient.publishPackage(senderAccount, packageMetadata, modulesCode)
Parameters
Name
Type
senderAccount
packageMetadata
Uint8Array
modulesCode
Uint8Array[]
optionalTransactionArgs
Returns
Name
Type
Promise<TransactionResponse>
{
txHash: '',
result: ''
}
Example
The publishPackage
function requires the package metadata and module bytecode as the second and third parameter. 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.
1
4
5
Parse the JSON file and extract data
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 };
}
6
Call the publishPackage
function
publishPackage
functionasync function main(){
const { packageMetadata, modulesCode } = getPackageData("YOUR_PATH_TO_JSON_FILE");
let supraClient = await SupraClient.init(
"https://rpc-testnet.supra.com/"
);
let senderAccount = new SupraAccount(
new HexString("YOUR_PRIVATE_KEY").toUint8Array()
);
const publishTxn = await supraClient.publishPackage(senderAccount, packageMetadata, modulesCode)
console.log(publishTxn);
}
7
Complete code
import fs from "fs";
import { HexString, SupraAccount, SupraClient } from "supra-l1-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");
let supraClient = await SupraClient.init(
"https://rpc-testnet.supra.com/"
);
let senderAccount = new SupraAccount(
new HexString("YOUR_PRIVATE_KEY").toUint8Array()
);
const publishTxn = await supraClient.publishPackage(senderAccount, packageMetadata, modulesCode)
console.log(publishTxn);
}
main()