🌐
Typescript / Javascript SDK
  • 👋Phantasma - TS
  • 🎆Quick Start
  • Setup
    • 🛠️Setup
  • Shared Methods
    • ☁️PhantasmaAPI
    • 🔗PhantasmaLink
    • 📝ScriptBuilder
      • Create a Script
  • Backend
    • ⚙️Backend
    • 📄Examples
      • Creating a new Address
      • Importing a Wallet
      • Create Script Call
      • Create a Transaction
      • Sign a Transaction
      • Send a Transaction
      • Get a Transaction
      • Invoke a Script
      • Get User Balances
      • Transfer Tokens
      • Decode Transfer Events
      • Get a Block by Height
      • Get data from new Blocks
  • Frontend
    • 🖥️Frontend
    • 📄Examples
      • Connect to the wallet
      • Invoking a Script
      • Sending a transaction
  • Resources
    • 👻Getting Started
    • 👻About Phantasma
    • 🧑‍🤝‍🧑For new Soldiers
    • 👨‍💻For Developers
Powered by GitBook
On this page
  • Allow Gas
  • Spend Gas
  • Call Interop
  • Call Contract
  1. Shared Methods
  2. ScriptBuilder

Create a Script

To execute code in the blockchain, you need to create a Script, using ScriptBuilder.

First, let's instantiate the ScriptBuilder

const {  ScriptBuilder } = require('phantasma-ts');
let scriptBuilder = new ScriptBuilder();

Allow Gas

This method is used to allow a dApp or a wallet to make use some GAS ( KCAL to execute a function that will change something in the blockchain. )

Parameters

  • From: Address or String

  • Target: Address or String, Normally used a Address.Null or a SmartContract Address.

  • GasPrice: Number, Normally used the default Gas Price: 100000

  • GasLimit: Number, This is the max Gas that can be used to execute this transaction.

When you have a AllowGas, you will always need to have a SpendGas

let fromAddress = "";
let targetAddress = Address.Null;
let gasPrice = 100000;
let gasLimit = 210000;
let tokenSymbol = "SOUL";

// This method belong to the ScriptBuilder.
AllowGas(fromAddress, targetAddress, gasPrice, gasLimit)

It can be used like this.

let script = scriptBuilder
    .AllowGas(fromAddress.Text, Address.Null, gasPrice, gasLimit)
    .CallInterop("Runtime.TransferTokens", [fromAddress.Text, toAddress, tokenSymbol, amount]) 
    .SpendGas(fromAddress.Text)
    .EndScript();

Spend Gas

This method is used to Spend the Gas a dApp or a wallet to used for the transaction.

Parameters

  • From: Address or String, the Address that will pay for the transaction.

When you have a AllowGas, you will always need to have a SpendGas

let fromAddress = "";
let targetAddress = Address.Null;
let gasPrice = 100000;
let gasLimit = 210000;
let tokenSymbol = "SOUL";

// This method belong to the ScriptBuilder.
SpendGas(fromAddress);

An example on how it could be used.

let script = scriptBuilder
    .AllowGas(fromAddress.Text, Address.Null, gasPrice, gasLimit)
    .CallInterop("Runtime.TransferTokens", [fromAddress.Text, toAddress, tokenSymbol, amount]) 
    .SpendGas(fromAddress.Text)
    .EndScript();

Call Interop

Parameters

  • Method Name: String, Check the list for more details.

  • An array with the arguments needed.

In this example we'll be checking the Runtime.TransferTokens

let script = scriptBuilder
    .AllowGas(fromAddress.Text, Address.Null, gasPrice, gasLimit)
    .CallInterop("Runtime.TransferTokens", [fromAddress.Text, toAddress, tokenSymbol, amount]) 
    .SpendGas(fromAddress.Text)
    .EndScript();

Call Contract

This method is used to Call a Contract Method that is deployed in the blockchain.

Parameters

  • Contract Name: String

  • Method Name: String, the name of the method inside of the contract.

  • An array with the arguments needed to call that method.

In this example we will be calling the Stake contract the stake Method.

A normal contract will be always lowercased and a Token contract will be always Uppercased.

Example:

  • "stake" -> Stake Contract

  • "SOUL" -> SOUL Token Contract.

let script = scriptBuilder
    .AllowGas(fromAddress.Text, Address.Null, gasPrice, gasLimit)
    .CallContract("stake", "Stake", [fromAddress.Text, amount]) 
    .SpendGas(fromAddress.Text)
    .EndScript();
PreviousScriptBuilderNextBackend

Last updated 1 year ago

This method is used to Call an Internal Operation in the blockchain, you can check the list of possible Internal Operations that you can call .

📝
here