TypeScript SDK
Learn how to interact with Movement using the Movement TypeScript SDK.
Movement TypeScript SDK
This Movement TS SDK repo is forked from github.com/aptos-labs prior to the date on which the Aptos Foundation implemented its Innovation-Enabling Source Code License in substitution for the prior Apache License, Version 2.0 governing this repository.
Move Industries continues to maintain, develop, modify, and distribute this repository solely under the Apache License, Version 2.0, as existed at the time of the fork and without application of the license instituted by the Aptos Foundation.
The Movement TypeScript SDK allows you to interact with the Movement Network. The guide below will walk you through the process of setting up the SDK and interacting with the chain.
Installation
npm install @moveindustries/ts-sdkImports
To get started with the SDK, import the necessary components:
const { Account, Movement, MovementConfig, Network, Ed25519PrivateKey } = require("@moveindustries/ts-sdk");Configuration
Configure the SDK to connect to the Movement Bardock Testnet (Please refer to Network Endpoints for more endpoints):
const config = new MovementConfig({
network: Network.CUSTOM,
fullnode: 'https://testnet.movementnetwork.xyz/v1',
faucet: 'https://faucet.testnet.movementnetwork.xyz/',
});
// Initialize the Movement client
const movement = new Movement(config);Interacting with the Chain
Account Setup
// Create an account from a private key
const privateKey = new Ed25519PrivateKey("YOUR_PRIVATE_KEY")
const account = Account.fromPrivateKey({ privateKey })Reading Data
// Example of reading data using view function
const viewPayload = {
function: "0x1::message::get_message",
functionArguments: [accountAddress]
};
const result = await movement.view({ payload: viewPayload });Sending Transactions
// Build the transaction
const transaction = await movement.transaction.build.simple({
sender: accountAddress,
data: {
function: "0x1::message::set_message",
functionArguments: ["Hello Movement!"]
},
});
// Sign the transaction
const signature = movement.transaction.sign({
signer: account,
transaction
});
// Submit the transaction
const committedTxn = await movement.transaction.submit.simple({
transaction,
senderAuthenticator: signature,
});
// Wait for transaction completion
const response = await movement.waitForTransaction({
transactionHash: committedTxn.hash
});Remember to replace YOUR_PRIVATE_KEY with your actual private key and never share or commit private keys to version control.
Example Code
The Code below is based on the Your First Move Contract tutorial.
const { Account, Movement, MovementConfig, Network, Ed25519PrivateKey } = require("@moveindustries/ts-sdk");
// Define the custom network configuration
const config = new MovementConfig({
network: Network.CUSTOM,
fullnode: 'https://testnet.bardock.movementnetwork.xyz/v1',
faucet: 'https://faucet.testnet.bardock.movementnetwork.xyz/',
});
// Define the module address and functions
const MODULE_ADDRESS = "";
const SET_MESSAGE_FUNCTION = `${MODULE_ADDRESS}::message::set_message`;
const GET_MESSAGE_FUNCTION = `${MODULE_ADDRESS}::message::get_message`;
const PRIVATE_KEY = "YOUR_PRIVATE_KEY"; // Replace with your private key
const MESSAGE = "gmove";
const setMessage = async () => {
// Setup the client
const movement = new Movement(config);
// Create an account from the provided private key
console.log("creating")
const privateKey = new Ed25519PrivateKey(PRIVATE_KEY)
const account = Account.fromPrivateKey({ privateKey })
const accountAddress = account.accountAddress
console.log(`address: ${account.accountAddress}`)
console.log(`Using account: ${accountAddress}`);
// Build the transaction payload
const payload = {
function: SET_MESSAGE_FUNCTION,
type_arguments: [],
arguments: [MESSAGE],
};
console.log("\n=== Reading Message ===\n");
const viewPayload = {
function: GET_MESSAGE_FUNCTION,
functionArguments: [accountAddress]
}
try {
const message = await movement.view({ payload: viewPayload });
console.log("Message:", message);
} catch (error) {
console.error("Error reading message:", error);
}
// Submit the transaction
console.log("\n=== Submitting Transaction ===\n");
const transaction = await movement.transaction.build.simple({
sender: accountAddress,
data: {
function: SET_MESSAGE_FUNCTION,
functionArguments: [MESSAGE]
},
});
// Sign the transaction
const signature = movement.transaction.sign({ signer: account, transaction });
// Submit the transaction to chain
const committedTxn = await movement.transaction.submit.simple({
transaction,
senderAuthenticator: signature,
});
console.log(`Submitted transaction: ${committedTxn.hash}`);
const response = await movement.waitForTransaction({ transactionHash: committedTxn.hash });
console.log({ response })
// Read the message after it has been set
console.log("\n=== Reading Message ===\n");
const newMessage = await movement.view({ payload: viewPayload });
console.log("Message:", newMessage);
};
setMessage().catch((err) => {
console.error("Error setting message:", err);
});