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();

Last updated