algo-builder icon indicating copy to clipboard operation
algo-builder copied to clipboard

Overwritting parameter from algobuilder script to Pyteal programe with deployer.mkDelegatedLsig

Open ayoubomari opened this issue 3 years ago • 0 comments

I use this method in algobuilder :

deployer.mkDelegatedLsig(lsigName: string, fileName: string, signer: runtime.rtypes.Account, scTmplParams?: types.SCParams): Promise<LsigInfo> 

But the PyTeal program always compiled with the default arguments even if I use "scTmplParams" to pass my arguments from the script to Pyteal, my arguments don't passed to the Pyteal program because args length equal to 1.

Script

const { executeTransaction } = require("@algo-builder/algob");
const { types } = require("@algo-builder/web");

async function run (runtimeEnv, deployer) {
  // accounts involved
  const master = deployer.accountsByName.get("master");
  const acc1 = deployer.accountsByName.get("acc1");
  const acc2 = deployer.accountsByName.get("acc2");

  // supply params to program during compilation - not the same as passing arguments
  const templateParams = {
    RECEVIER_1: acc1.addr,
    RECEIVER_2: acc2.addr
  }
  
  // create logic sig for sender account
  const masterLogicSig = await deployer.mkDelegatedLsig(
    "pyteal_program.py",
    master,
    templateParams
  );

  // send Algos to acc1
  console.log("send algos to acc1...")
  await executeTransaction(deployer, {
      type: types.TransactionType.TransferAlgo,
      sign: types.SignType.LogicSignature,
      lsig: masterLogicSig.lsig,
      fromAccountAddr: masterLogicSig.contractAddress,
      toAccountAddr: acc1.addr,
      amountMicroAlgos: 1e6, //1 algo
      payFlags: { totalFee: 1000 },
      args: ["rcv1password"],
  });

  // send Algos to acc2
  console.log("send algos to acc2...")
  await executeTransaction(deployer, {
      type: types.TransactionType.TransferAlgo,
      sign: types.SignType.LogicSignature,
      lsig: masterLogicSig.lsig,
      fromAccountAddr: masterLogicSig.contractAddress,
      toAccountAddr: acc2.addr,
      amountMicroAlgos: 1e6, //1 algo
      payFlags: { totalFee: 1000 },
      args: ["rcv2password"]
  });

}

module.exports = { default: run };

Pyteal

# Add directory to path so that algobpy can be imported
import sys
sys.path.insert(0,'.')

from algobpy.parse import parse_params
from pyteal import *

def main(RECEIVER_1, RECEIVER_2):

    receiver_1_checks = And(
        Arg(0) == Bytes("rcv1password"), #rcv1 password
        Txn.amount() <= Int(5000000) #5 Algos 
    )

    receiver_2_checks = And(
        Arg(0) == Bytes("rcv2password"), #rcv2 password
        Txn.amount() <= Int(10000000) #10 Algos 
    )

    receiver_checks = Cond(
        [Txn.receiver() == Addr(RECEIVER_1), receiver_1_checks],
        [Txn.receiver() == Addr(RECEIVER_2), receiver_2_checks]
    )

    program = And(
        Txn.rekey_to() == Global.zero_address(),
        Txn.close_remainder_to() == Global.zero_address(),
        receiver_checks
    )

    return program

if __name__ == "__main__":
    # Default receiver address used if params are not supplied when deploying this contract
    params = {
        "RECEIVER_1": "R4VDREHBHVETKRPBZT6IDOQQL4FBHLBYQBQQJPIBXLTCVXYJX7Z5WLDSZY",
        "RECEIVER_2": "WRBVLPUHQZ5O2UIZAKYKKMOUSNPOFIL6ALUZQZLHBDUSIKXHAEEIELWBFQ",
    }

    # Overwrite params if sys.argv[1] is passed
    if(len(sys.argv) > 1):
        params = parse_params(sys.argv[1], params)

    print(compileTeal(main(params["RECEIVER_1"], params["RECEIVER_2"]), Mode.Signature, version=6))

Environment

  • OS: Linux
  • Node.js version: 16.13.2
  • algob version: 3.2.0

ayoubomari avatar Aug 12 '22 20:08 ayoubomari