massalabs / massa-web3

Web3 libraries for Massa
MIT License
29 stars 74 forks source link

JavaScript for MAS/PUR transfer #641

Closed ckorodenis closed 1 month ago

ckorodenis commented 1 month ago

This is a simple script to convert tokens from a defined wallet to the wallet passed in the trigger command

node send.js <amount> <wallet>

The script failed to run, we can't find the bug.

Complete script code

ClientFactory,
  WalletClient,
  DefaultProviderUrls,
  CHAIN_ID,
  Args,
  fromMAS,
  MAX_GAS_CALL
} = require("@massalabs/massa-web3");

const secretKey = "S1zN1YEnNeEws7nfULuCmZsi"; // Nahraď svým vlastním tajným klíčem
const mainnetRpc = DefaultProviderUrls.MAINNET; // URL pro hlavní síť

async function initializeClient() {
  try {
    const deployedAccount = await WalletClient.getAccountFromSecretKey(secretKey);

    const client = await ClientFactory.createDefaultClient(
      mainnetRpc,
      CHAIN_ID.MAINNET,
      false,
      deployedAccount
    );

    return client;
  } catch (error) {
    console.error("Error initializing client:", error);
    throw error;
  }
}

async function handleTransferPUR(amount, to) {
  if (!amount || !to) {
    console.error("Amount or address is missing");
    return;
  }

  try {
    const client = await initializeClient();
    const parsedAmount = BigInt(amount) * 10n ** BigInt(18);

    const args = new Args()
      .addString(to) // Adresa příjemce
      .addU256(parsedAmount);

    const tx = await client.smartContracts().callSmartContract({
      fee: fromMAS(0.01),
      maxGas: MAX_GAS_CALL,
      coins: fromMAS(0),
      targetAddress: "AS133eqPPaPttJ6hJnk3sfoG5cjFFqBDi1VGxdo2wzWkq8AfZnan", // Adresa smart contractu pro převod
      functionName: "transfer",
      parameter: args,
    });

    console.log(tx);
  } catch (e) {
    console.error("Error in handleTransferPUR function:", e);
  }
}

const amountToTransfer = process.argv[2];
const targetWalletAddress = process.argv[3];
console.log("Amount to transfer:", amountToTransfer);
console.log("Target wallet address:", targetWalletAddress);
handleTransferPUR(amountToTransfer, targetWalletAddress);

Statement when correct amount and wallet value is passed

E:\VS Code\MW0rld - Gama - 0.2.3>node dusa-look.js 1 AU127T8A4k5aqirooNiFRcrqLFpGQBPf8LncFmUEEf5CGj3mVjq5CAmount to transfer: 1
Target wallet address: AU127T8A4k5aqirooNiFRcrqLFpGQBPf8LncFmUEEf5CGj3mVjq5CError initializing client: TypeError: Cannot read properties of undefined (reading 'toString')
    at ClientFactory.createDefaultClient (E:\VS Code\MW0rld - Gama - 0.2.3\node_modules\@massalabs\massa-web3\dist\cmd\web3\ClientFactory.js:66:78)    at initializeClient (E:\VS Code\MW0rld - Gama - 0.2.3\dusa-look.js:18:40)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)    at async handleTransferPUR (E:\VS Code\MW0rld - Gama - 0.2.3\dusa-look.js:39:20)
Error in handleTransferPUR function: TypeError: Cannot read properties of undefined (reading 'toString')    at ClientFactory.createDefaultClient (E:\VS Code\MW0rld - Gama - 0.2.3\node_modules\@massalabs\massa-web3\dist\cmd\web3\ClientFactory.js:66:78)
    at initializeClient (E:\VS Code\MW0rld - Gama - 0.2.3\dusa-look.js:18:40)    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async handleTransferPUR (E:\VS Code\MW0rld - Gama - 0.2.3\dusa-look.js:39:20)