Im noob in the smart contracts development. I have a .js script where every time a button of my app is pressed a smart contract is displayed with some parameters obtained through a form. I deployed my first contract “OpcionCompra” through the truffle terminal. Now I want to deploy another contract with different parameters but also “OpcionCompra”. However, in garnache I only get one contract, and, moreover, with the parameters of the one I gave at the beginning.
I want that every time I fill in a form and click on the corresponding button, a new contract is generated, each one with different clauses.
This is my .js script. Note that now im not taking the variables from the form, but this is only a draft:
const fs =require('fs')
const comprador = '0x1111111111111111111111111111111111111111'; // Dirección del comprador
const vendedor = '0x3333333333333333333333333333333333333333'; // Dirección del vendedor
const referenciaCatastral = '3231e13a';
const idParcela = 'parcela1';
const prima = 2324;
const strike = 67675;
const fechaVenc = 123432;
const bytecode=fs.readFileSync('contracts_OpcionCompra_sol_OpcionCompra.bin').toString()
const abi=JSON.parse(fs.readFileSync('contracts_OpcionCompra_sol_OpcionCompra.abi').toString())
var {Web3} = require('web3')
const web3 = new Web3("http://localhost:7545")
async function deployContract(){
try{
const accounts = await web3.eth.getAccounts();
//console.log('Cuentas:', accounts);
// Crea una instancia del contrato
const opcionCompraContract = new web3.eth.Contract(abi);
//const opcionCompraContract = artifacts.require("OpcionCompra")
opcionCompraContract.deploy({
data: bytecode,
arguments: [comprador, vendedor, referenciaCatastral, idParcela, prima, strike, fechaVenc]
})
.send({
from: accounts[1],
gas: 1500000,
gasPrice: '30000000000000'
})
.on('error')
.on('transactionHash')
.on('receipt', function(receipt){
console.log(receipt.contractAddress) // contains the new contract address
})
.on('confirmation')
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // instance with the new contract address
});
/*
catch(error){
console.error("Error al desplegar el contrato:", error);
}
}
deployContract();
This is my solidity Contract:
// SPDX-License-Identifier: MIT
//pragma solidity ^0.8.25;
pragma solidity ^0.5.16;
//Variables de estado, Modificadores, Constructor y Eventos
contract OpcionCompra {
address public comprador;
address payable public vendedor;
address public intermediario;
string public referenciaCatastral;
string public idParcela;
uint public prima;
uint public strike;
uint256 public fechaVenc;
bool public ejecutar;
bool public firmaComprador;
bool public firmaVendedor;
bool firmado;
bool ejecutado;
//string public log;
//MODIFICADORES
modifier estaFirmado{
require(firmaVendedor==true && firmaComprador == true);
_;
}
modifier onlyComprador{
require(msg.sender == comprador);
_;
}
constructor(address _comprador,address payable _vendedor,string memory _referenciaCatastral,string memory _idParcela,
uint _prima,uint _strike, uint256 _fechaVenc) public {
intermediario = msg.sender;
comprador=_comprador;
vendedor = _vendedor;
referenciaCatastral = _referenciaCatastral;
idParcela = _idParcela;
prima = _prima;
strike = _strike;
fechaVenc = _fechaVenc;
ejecutar = false;
firmaComprador = false;
firmaVendedor = false;
firmado = false;
ejecutado = false;
}
event firmaParte(address firmante);
function firmarContrato() public {
require(msg.sender == comprador || msg.sender==vendedor,"Solo comprador o vendedor puede firmar");
if(msg.sender == comprador){
firmaComprador = true;
} else {
firmaVendedor = true;
}
emit firmaParte(msg.sender);
if(firmaComprador && firmaVendedor){
firmado=true;
}
}
function comprobarFirmas() public view returns(string memory log) {
//require(firmaVendedor=true && firmaComprador = true);
if(firmado){log= "Las dos partes han firmado";}
else if(firmaComprador && !firmaVendedor){log= "Falta la firma del vendedor";}
else if(!firmaComprador && firmaVendedor){log= "Falta la firma del comprador";}
else if(!firmaComprador && !firmaVendedor){log= "Faltan las dos firmas";}
return log;
}
function pagarPrima() public payable onlyComprador{
require(block.timestamp <= fechaVenc, "La opcion ha vencido.");
require(firmado,"El contrato no esta firmado por ambas partes");
// Verificar que el comprador tiene suficientes fondos
require(msg.sender.balance >= prima, "Fondos insuficientes en el contrato");
vendedor.transfer(prima);
ejecutado=true;
}
}