Join our
Discord!
LogoLogo
SupraScan ExplorerStarKey WalletDiscord
MoveVM
  • Network
  • Oracles
  • Automation
  • Guides
MoveVM
  • Overview
  • Getting Started
    • Install Supra CLI with Docker
    • Create a Supra Account
    • Create a Move Package
      • Initialize a Package
      • Write a Module
      • Compile and Publish
      • Interact with a Package
    • Create a dApp with StarKey
  • Network Information
  • Token Standards
  • Learn Move 101
    • Getting Started with Move
    • Unsigned Integers in Move
    • Math Operations in Move
    • Using Vectors in Move
    • Reading Resource Data with borrow_global
    • Passing Data in Move: Value vs. Reference
    • Adding Elements with vector::push_back
    • Emitting Events with event::emit
  • Move Book
    • Getting Started
      • Modules and Scripts
      • Move Tutorial
    • Primitive Types
      • Integers
      • Bool
      • Address
      • Vector
      • Signer
      • References
      • Tuples and Unit
    • Basic Concepts
      • Local Variables and Scope
      • Equality
      • Abort and Assert
      • Conditionals
      • While, For, and Loop
      • Functions
      • Structs and Resources
      • Constants
      • Generics
      • Type Abilities
      • Uses and Aliases
      • Friends
      • Packages
      • Package Upgrades
      • Unit Tests
    • Global Storage
      • Structure
      • Operators
    • Reference
      • Standard Library
      • Coding Conventions
  • TypeScript SDK
    • Guides
      • Create Supra Accounts
      • Publish a Package
    • Documentation
    • Repository
  • Rest API
    • Mainnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Consensus
      • Events
      • Tables
    • Testnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Events
      • Tables
  • Developer Resources
    • Supra Dapp Templates
    • Supra Move VS Code Extension
  • Links
    • Supra DevHub
    • SupraScan Block Explorer
    • StarKey Wallet
    • Live Data Feeds
    • Whitepapers
    • Security Audits
    • Supra's Official GitHub
Powered by GitBook
On this page
  • publishPackage function
  • Example
Edit on GitHub
  1. TypeScript SDK
  2. Guides

Publish a Package

How to publish a package using the TypeScript SDK.

PreviousCreate Supra AccountsNextRest API

function

await supraClient.publishPackage(senderAccount, packageMetadata, modulesCode)

Parameters

Name
Type

senderAccount

packageMetadata

Uint8Array

modulesCode

Uint8Array[]

optionalTransactionArgs

Returns

Name
Type

Promise<TransactionResponse>

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

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.json
2

Initialize a new project

npm init && npm add -D typescript @types/node ts-node && npx tsc --init
3

Install Supra's TypeScript SDK

npm install supra-l1-sdk
4

Create publish_example.ts and import dependencies

publish_example.ts
import fs from "fs";
import { HexString, SupraAccount, SupraClient } from "supra-l1-sdk";
5

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 };
}
6

Call the publishPackage function

publish_example.ts
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);

}
7

Complete code

publish_example.ts
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()
8

Execute code npx ts-node publish_example.ts

publishPackage
SupraAccount
OptionalTransactionArgs
TransactionResponse