import { ec } from "starknet";
// Replace with your StarkNet private key
const walletPrivateKey = "your-starknet-private-key";
// Replace with your Adamik API key from https://dashboard.adamik.io
const ADAMIK_API_KEY = "your-adamik-api-key";
async function main() {
try {
console.log("Creating wallet...");
const pubKey = ec.starkCurve.getStarkKey(walletPrivateKey);
// First, let's get our wallet address
const requestBodyAddressEncode = {
pubkey: pubKey,
};
// Fetch the wallet address from Adamik API
const responseAddressEncode = await fetch(
"https://api.adamik.io/api/starknet/address/encode",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: ADAMIK_API_KEY,
},
body: JSON.stringify(requestBodyAddressEncode),
}
);
const addressEncode = await responseAddressEncode.json();
// Get the ArgentX wallet address
const senderAddress = addressEncode.addresses.find(
(address) => address.type === "argentx"
).address;
console.log("Sender address:", senderAddress);
// Prepare the transfer transaction
const requestBody = {
transaction: {
data: {
mode: "transfer", // Transaction type
senderAddress, // Our wallet address
recipientAddress: senderAddress, // Where we're sending to (self-transfer in this example)
amount: "20000000000", // Amount in wei (0.00002 STRK in this example)
},
},
};
console.log("Encoding transaction...");
const response = await fetch(
"https://api.adamik.io/api/starknet/transaction/encode",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: ADAMIK_API_KEY,
},
body: JSON.stringify(requestBody),
}
);
let encodedData = await response.json();
// Check for encoding errors
if (
encodedData.status &&
encodedData.status.errors &&
encodedData.status.errors.length > 0
) {
if (
encodedData.status.errors[0].message === "Sender account does not exist"
) {
console.log(
"Account never deployed... Creating deployment transaction"
);
// We need to deploy the account because it hasn't been deployed yet
const requestBodyDeploy = {
transaction: {
data: {
mode: "deployAccount", // Transaction type
senderPubKey: pubKey,
type: "argentx",
},
},
};
console.log("Encoding deployment transaction...");
const responseDeploy = await fetch(
"https://api.adamik.io/api/starknet/transaction/encode",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: ADAMIK_API_KEY,
},
body: JSON.stringify(requestBodyDeploy),
}
);
encodedData = await responseDeploy.json();
console.log(
"Deployment transaction encoded:",
JSON.stringify(encodedData, null, 2)
);
} else {
throw new Error(encodedData.status.errors[0].message);
}
}
console.log("Signing transaction...");
// Sign the encoded transaction using StarkNet curve
const signature = ec.starkCurve.sign(
encodedData.transaction.encoded,
walletPrivateKey
);
const signatureHex = signature.toDERHex();
console.log("Transaction signed");
// Prepare the broadcast request
const broadcastTransactionBody = {
transaction: {
...encodedData.transaction,
signature: signatureHex,
},
};
console.log("Broadcasting transaction...");
// Broadcast the signed transaction
const broadcastResponse = await fetch(
"https://api.adamik.io/api/starknet/transaction/broadcast",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: ADAMIK_API_KEY,
},
body: JSON.stringify(broadcastTransactionBody),
}
);
const responseData = await broadcastResponse.json();
console.log("Transaction Result:", JSON.stringify(responseData, null, 2));
console.log("Transaction hash:", responseData.hash);
} catch (error) {
console.error("Error:", error);
}
}
main();