Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to execute a publish transaction using TS SDK #20703

Closed
jdbertron opened this issue Dec 20, 2024 · 5 comments
Closed

How to execute a publish transaction using TS SDK #20703

jdbertron opened this issue Dec 20, 2024 · 5 comments
Assignees
Labels
doc-issue Issue submitted using the Doc issue template ts-sdk

Comments

@jdbertron
Copy link

jdbertron commented Dec 20, 2024

Ever since RawSigner got deprecated and TransactionBlock got renamed, I can't figure out how to deploy a contract. I have new code that seems to execute, but fails with Error: No sui client passed to Transaction#build, but transaction data was not sufficient to build offline.

Please provide a working example.

import "dotenv/config"
import  { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
import { fromB64 } from "@mysten/sui.js/utils";
import { SuiClient } from "@mysten/sui.js/client";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import path, { dirname } from "path";
import { execSync } from "child_process";
import { fileURLToPath } from "url";
import { Transaction } from "@mysten/sui/transactions";

const priv_key = process.env.PRIVATE_KEY;
if (!priv_key) {
    console.log("Error: Priv Key not set in env");
    process.exit(1);
}

const keypair = Ed25519Keypair.fromSecretKey(fromB64(priv_key).slice(1));

const node_url = process.env.NODE_URL ;
if (!node_url) {
    console.log("Error: Node url not found in env");
    process.exit(1);
}

const client = new SuiClient({ url: node_url });
const path_to_contract = path.join(dirname(fileURLToPath(import.meta.url)), "./contracts/sources");
console.log("Building contract")
const { dependencies, modules } = JSON.parse(execSync(
    `sui move build --dump-bytecode-as-base64  --path ${path_to_contract}`, 
    { encoding: "utf-8" }
));
console.log("Deploying contract");
console.log(`Deploying from: ${keypair.toSuiAddress()}`);

const deploy_trx = new Transaction();
deploy_trx.setSender(keypair.getPublicKey().toSuiAddress());

const [upgrade_cap] = deploy_trx.publish({
    modules, dependencies
});

const signedTransaction = await deploy_trx.sign({ signer: keypair });

function stringToUint8Array(str: string): Uint8Array {
    const buf = new ArrayBuffer(str.length);
    const bufView = new Uint8Array(buf);
    for (let i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i);
    }
    return bufView;
}
const addressBytes = stringToUint8Array(keypair.getPublicKey().toSuiAddress());
deploy_trx.transferObjects([upgrade_cap], deploy_trx.pure(addressBytes));


const { objectChanges, balanceChanges } = await client.executeTransactionBlock({
    transactionBlock: signedTransaction.signature,
    options: {
        showBalanceChanges: true,
        showEffects: true,
        showEvents: true,
        showInput: true,
        showObjectChanges: true,
        showRawInput: false
    },
    signature: signedTransaction.bytes
});

console.log(objectChanges, balanceChanges);
@jdbertron jdbertron added the doc-issue Issue submitted using the Doc issue template label Dec 20, 2024
@hayes-mysten
Copy link
Contributor

hayes-mysten commented Dec 20, 2024

import 'dotenv/config';
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { fromBase64 } from '@mysten/sui/utils';
import { SuiClient } from '@mysten/sui/client';
import path, { dirname } from 'path';
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';
import { Transaction } from '@mysten/sui/transactions';

const priv_key = process.env.PRIVATE_KEY;
if (!priv_key) {
	console.log('Error: Priv Key not set in env');
	process.exit(1);
}

const keypair = Ed25519Keypair.fromSecretKey(fromBase64(priv_key).slice(1));

const node_url = process.env.NODE_URL;
if (!node_url) {
	console.log('Error: Node url not found in env');
	process.exit(1);
}

const client = new SuiClient({ url: node_url });
const path_to_contract = path.join(dirname(fileURLToPath(import.meta.url)), './contracts/sources');
console.log('Building contract');
const { dependencies, modules } = JSON.parse(
	execSync(`sui move build --dump-bytecode-as-base64  --path ${path_to_contract}`, {
		encoding: 'utf-8',
	}),
);
console.log('Deploying contract');
console.log(`Deploying from: ${keypair.toSuiAddress()}`);

const deploy_trx = new Transaction();
deploy_trx.setSender(keypair.getPublicKey().toSuiAddress());

const [upgrade_cap] = deploy_trx.publish({
	modules,
	dependencies,
});

deploy_trx.transferObjects([upgrade_cap], keypair.getPublicKey().toSuiAddress());

const { objectChanges, balanceChanges } = await client.signAndExecuteTransaction({
	signer: keypair,
	transaction: deploy_trx,
	options: {
		showBalanceChanges: true,
		showEffects: true,
		showEvents: true,
		showInput: true,
		showObjectChanges: true,
		showRawInput: false,
	},
});

// If you want to sign and execute separately:
const { bytes, signature } = await keypair.signTransaction(await deploy_trx.build({ client }));
const { objectChanges, balanceChanges } = await client.executeTransactionBlock({
	transactionBlock: bytes,
	signature,
	options: {
		showBalanceChanges: true,
		showEffects: true,
		showEvents: true,
		showInput: true,
		showObjectChanges: true,
		showRawInput: false,
	},
});

console.log(objectChanges, balanceChanges);

@hayes-mysten
Copy link
Contributor

the migration guide here might also be helpful: https://sdk.mystenlabs.com/typescript/migrations/sui-1.0

@stefan-mysten
Copy link
Contributor

@jdbertron hope that the updated example from @hayes-mysten and the migration guide will help you here. Feel free to let us know otherwise.

@jdbertron
Copy link
Author

jdbertron commented Dec 20, 2024 via email

@stefan-mysten stefan-mysten changed the title Sui doc content issue or request How to execute a publish transaction using TS SDK Dec 20, 2024
@jdbertron
Copy link
Author

That worked. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-issue Issue submitted using the Doc issue template ts-sdk
Projects
None yet
Development

No branches or pull requests

4 participants