Write contract - web3 - interact web site - Moralis.executeFunction - api / abi

Probably you already saw this:

You can also use directly Moralis.executeFunction in react:
https://docs.moralis.io/moralis-dapp/web3/web3#executefunction

You can look into this too https://youtu.be/7TKqLJd_aYI

Yes, thanks, I started doing according to your video from 3 minutes but changed to ExecuteFunction
I think I correctly compiled the deposit function from smart contract and ABI: https://testnet.bscscan.com/address/0xec08f00fe90fcf5e205b45ac067f22c6f0e0854b#code

async function transferNative() {
    const amount = document.getElementById('#Amount-Payable').value;
    const address = document.getElementById('#Uplline-Address').value;
const ABI = [
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "_upline",
                "type": "address"
            }
        ],
        "name": "deposit",
        "outputs": [],
        "stateMutability": "payable",
        "type": "function"
    },
    
    {
        "inputs": [],
        "name": "withdraw",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
  ];
  const sendOptions = {
    contractAddress: "0xEc08f00FE90fcf5e205b45Ac067f22C6F0e0854B",
    functionName: "deposit",
    abi: ABI,
    params: {
        _upline: "#Uplline_Address", 
    },
    msgValue: "#Amount_Payble"
  };
  await Moralis.executeFunction(sendOptions);
}

document.getElementById('#transfer-native').onclick = transferNative;

async function Withdraw() {
const ABI = [
    {
        "inputs": [],
        "name": "withdraw",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
  ];
  const sendOptions = {
    contractAddress: "0xEc08f00FE90fcf5e205b45Ac067f22C6F0e0854B",
    functionName: "withdraw",
    abi: ABI,
    params: {
        , ????
    },
  };
  await Moralis.executeFunction(sendOptions);
}

document.getElementById('#withdraw').onclick = Withdraw;

What do you withdraw there? It doesn’t look like there are parameters in the abi and also it doesn’t look like it is payable.

first function is send deposit amount (bnb) and referral address

the second function is just the function of outputting to the address of the sender of the request and the smart contract itself will pay the accrued interest

If the function doesn’t have parameters then you don’t have to send it parameters.

  1. function: 1- amount payable 2- uplline addres
  2. function: withdraw
async function deposit() {
    const amount = document.getElementById('#Amount-Payable').value;
    const address = document.getElementById('#Uplline-Address').value;
const ABI = [
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "_upline",
                "type": "address"
            }
        ],
        "name": "deposit",
        "outputs": [],
        "stateMutability": "payable",
        "type": "function"
    },
    
  ];
  const sendOptions = {
    contractAddress: "0xEc08f00FE90fcf5e205b45Ac067f22C6F0e0854B",
    functionName: "deposit",
    abi: ABI,
    params: {
        _upline: "#Uplline_Address", 
    },
    msgValue: "#Amount_Payble"
  };
  await Moralis.executeFunction(sendOptions);
}

And this one doesn’t work?

Still trying to figure out if I’m on the right track?))

async function Withdraw() {
const ABI = [
    {
        "inputs": [],
        "name": "withdraw",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
  ];
  const sendOptions = {
    contractAddress: "0xEc08f00FE90fcf5e205b45Ac067f22C6F0e0854B",
    functionName: "withdraw",
    abi: ABI,
  };
  await Moralis.executeFunction(sendOptions);
}

document.getElementById('#withdraw').onclick = Withdraw;

It seems fine to me, I think that you are on the right track.

I made logic from this video
https://www.youtube.com/watch?v=jdx2H1alijQ&t=2739s 37.min.
maybe it’s outdated?

did with another video
https://www.youtube.com/watch?v=7TKqLJd_aYI 2 min.

I can’t figure out what I did wrong?

  1. the shutdown button does not work for me
  2. the form is not written and does not go into a smart contract
  3. and the exit button does not work

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
  <meta name="generator" content="Hugo 0.88.1">
  <title>SMART CONTRACT</title>

  <link rel="canonical" href="https://getbootstrap.com/docs/5.1/examples/headers/">
  <link href="css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="headerss.css">
  <link rel="stylesheet" href="style.css">

  <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
  <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
</head>

<body>
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-patch-check-fill"
    viewBox="0 0 16 16">
    <symbol id="bi-patch-check-fill" viewBox="0 0 18 18">
      <title>Smart Contract</title>
      <path fill-rule="evenodd" clip-rule="evenodd" fill="dodgerblue"
        d="M10.067.87a2.89 2.89 0 0 0-4.134 0l-.622.638-.89-.011a2.89 2.89 0 0 0-2.924 2.924l.01.89-.636.622a2.89 2.89 0 0 0 0 4.134l.637.622-.011.89a2.89 2.89 0 0 0 2.924 2.924l.89-.01.622.636a2.89 2.89 0 0 0 4.134 0l.622-.637.89.011a2.89 2.89 0 0 0 2.924-2.924l-.01-.89.636-.622a2.89 2.89 0 0 0 0-4.134l-.637-.622.011-.89a2.89 2.89 0 0 0-2.924-2.924l-.89.01-.622-.636zm.287 5.984-3 3a.5.5 0 0 1-.708 0l-1.5-1.5a.5.5 0 1 1 .708-.708L7 8.793l2.646-2.647a.5.5 0 0 1 .708.708z" />
      </path>
    </symbol>
  </svg>
  <main>
    <h1 class="visually-hidden">Headers</h1>
    <div class="container">
      <header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">

        <ul class="nav nav-pills">
          <li onclick="login()" id="btn-login" class="nav-item"><a href="#" class="nav-link active" aria-current="page">Join to Wallet</a>
              <script>
                  const serverUrl = "https://1jjca2c1r4ab.usemoralis.com:2053/server";
                  const appId = "f21REKbxRDZtrz0D7O7zcVqx7GjVy7cbbYHOdxd8";
                  Moralis.start({ serverUrl, appId });        
                  async function login() {
                      const user = await Moralis.authenticate()
                      document.getElementById('btn-login').innerHTML = user.get('ethAddress')
                    }
              </script>
          </li>

          <li onclick="logout()" id="btn-logout" class="nav-item"><a href="#" class="nav-link" aria-current="page">Disconnect</a>
            <script>
              async function logout() {
                  const user = await Moralis.User.logOut();
                  document.getElementById('btn-logout')
                }
          </script>
          </li>
        </ul>
      </header>
    </div>
  </main>
</body>
</html>
<main class="col-md-9 ms-sm-4 col-lg-10 px-md-4">
  </div>
  <p>
  <h2>Deposit</h2>
  <div class="table-responsive">
    </p>
    <p>
    <div class="form-floating">


      <input type="amount" class="form-control" id="Amount-Payable" placeholder="Amount-Payable">
      <label for="Amount-Payable">
        <spann>*</spann>BNB Amount Payable (Min 0.1 BNB)
      </label>
    </div>
    </p>
    <p>

    <div class="form-floating">


      <input type="address" class="form-control" id="Uplline-Address" placeholder="Uplline-Address">
      <label for="Uplline-Address">
        <spann>*</spann>Uplline Address Ref Partners (0x0123456789...Abс)
      </label>
    </div>
    </p>
    <button onclick="deposit()" id="deposit" class="w-100 btn btn-lg btn-primary" type="submit">Send BNB and create a deposit</button>
  </div>
  <p>
  <h2>Withdraw</h2>
  <div class="table-responsive">
  </div>
  </p>

  <button onclick="withdraw()" id="withdraw" class="w-100 btn btn-lg btn-primary" type="submit">Withdraw interest in BNB</button>
  </div>
  
</main>
<script src="main.js"></script>
</body>

</html>

console.log('Hello world');

// connect to Moralis server
const serverUrl = "https://1jjca2c1r4ab.usemoralis.com:2053/server";
const appId = "f21REKbxRDZtrz0D7O7zcVqx7GjVy7cbbYHOdxd8";
Moralis.start({ serverUrl, appId });

// Кпопка ΠŸΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ мСтамаск https://docs.moralis.io/moralis-dapp/users/web3-login/metamask
login = async () => {
    await Moralis.authenticate().then(async function (user) {
        console.log('logged in');
        window.location.href = "indexx.html";
    })
}
// Ѐункция отобраТСния адрСса кошСлька Ρƒ ΡŽΠ·Π΅Ρ€Π° послС ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ
async function login() {
  const user = await Moralis.authenticate()
  document.getElementById('login').innerhtml = user.get('ethAddress')
}
if (document.querySelector('#btn-login') != null) {
    document.querySelector('#btn-login').onclick = login;
}
// Кнопка ΠžΡ‚ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ мСтамаск (Π½Π°Π΄ΠΎ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ Π½Π΅ срабатываСт)
logout = async () => {
    await Moralis.User.logout();
    window.location.href = "indexx.html";
}
async function logout() {
    await Moralis.User.logout();
    window.location.href = "indexx.html";
}
if (document.querySelector('#btn-logout') != null) {
    document.querySelector('#btn-logout').onclick = logout;
}

// Π€ΠΎΡ€ΠΌΠ° ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ Π΄Π°Π½Π½Ρ‹Ρ… Π² смарт ΠΊΠΎΠ½Ρ‚Ρ€Π°ΠΊΡ‚ Ρ„ΠΎΡ€ΠΌΠ° Π΄Π΅ΠΏΠΎΠ·ΠΈΡ‚ (Π½Π°Π΄ΠΎ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ) https://docs.moralis.io/moralis-dapp/web3/web3#example-of-calling-a-write-contract-method
async function deposit() {
    await Moralis.executeFunction(sendOptions)
    amount.set("amount", document.getElementById('Amount-Payable').value);
    address.set("address",document.getElementById('Uplline-Address').value);
    const amount = document.getElementById('Amount-Payable').value;
    const address = document.getElementById('Uplline-Address').value;
const ABI = [
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "_upline",
                "type": "address"
            }
        ],
        "name": "deposit",
        "outputs": [],
        "stateMutability": "payable",
        "type": "function"
    },
  ];
  const sendOptions = {
    contractAddress: "0xEc08f00FE90fcf5e205b45Ac067f22C6F0e0854B",
    functionName: "deposit",
    abi: ABI,
    params: {
        _upline: "Uplline_Address", 
    },
    msgValue: Motalis.units.Eth("Amount_Payble")
  };
  await Moralis.executeFunction(sendOptions);
}
if (document.querySelector('#deposit') != null) {
    document.getElementById('#deposit').onclick = deposit
}

// Π€ΠΎΡ€ΠΌΠ° ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ Π΄Π°Π½Π½Ρ‹Ρ… Π² смарт ΠΊΠΎΠ½Ρ‚Ρ€Π°ΠΊΡ‚ ΠΊΠ½ΠΎΠΏΠΊΠ° Π’Ρ‹Π²ΠΎΠ΄ (Π½Π°Π΄ΠΎ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ) https://docs.moralis.io/moralis-dapp/web3/web3#example-of-calling-a-write-contract-method
async function withdraw() {
    await Moralis.executeFunction(sendOptions)
    const ABI = [
    {
        "inputs": [],
        "name": "withdraw",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
  ];
  const sendOptions = {
    contractAddress: "0xEc08f00FE90fcf5e205b45Ac067f22C6F0e0854B",
    functionName: "withdraw",
    abi: ABI,
  };
  await Moralis.executeFunction(sendOptions);
}
if (document.querySelector('#withdraw') != null) {
    document.getElementById('#withdraw').onclick = withdraw;
}
  const transaction = await Moralis.executeFunction(sendOptions);
  console.log(transaction.hash);

  // --> "0x39af55979f5b690fdce14eb23f91dfb0357cb1a27f387656e197636e597b5b7c"
  
  // Wait until the transaction is confirmed
  await transaction.wait();
  
  // Read new value
  const message = await Moralis.executeFunction(readOptions);
  console.log(message);
  // --> "Hello Moralis"

  // источники: https://forum.moralis.io/t/write-contract-web3-interact-web-site-moralis-executefunction-api-abi/15096
  // смарт ΠΊΠΎΠ½Ρ‚Ρ€Π°ΠΊΡ‚ бск https://testnet.bscscan.com/address/0xec08f00fe90fcf5e205b45ac067f22c6f0e0854b#code
  // функция написания Π²Ρ‹Π·ΠΎΠ²Π° https://docs.moralis.io/moralis-dapp/web3/web3#example-of-calling-a-write-contract-method
//

try to run the code directly in the browser console, for Moralis.executeFunction, with the values hardcoded for the parameters, then you can try to fix the html and the form

Yes, every time I look at the console, but I can not understand what is wrong

Uploading: Π‘Π½ΠΈΠΌΠΎΠΊ экрана 2022-05-22 Π² 15.25.31.png…

what I wanted to say it to try to run directly in the browser console code like this, where you replace that line where msgValue is used with a constant value for msgValue

try to add some logging with console.log, it looks like there are some javascript syntax problems there, like that line with msgValue where Motalis.units.Eth("Amount_Payble") has more than one problem and this is not going to execute

Yes, I understand, I followed the video instructions, but I need another function

i fixed some bugs and simplified the code

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
  <meta name="generator" content="Hugo 0.88.1">
  <title>SMART CONTRACT</title>

  <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
  <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
</head>

<body>
  <main>
    <h1 class="visually-hidden">Headers</h1>
    <div class="container">
      <header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">

        <ul class="nav nav-pills">
          <li onclick="login()" id="btn-login" class="nav-item"><a href="#" class="nav-link active"
              aria-current="page">Join to Wallet</a>
            <script>
              const serverUrl = "https://1jjca2c1r4ab.usemoralis.com:2053/server";
              const appId = "f21REKbxRDZtrz0D7O7zcVqx7GjVy7cbbYHOdxd8";
              Moralis.start({ serverUrl, appId });
              async function login() {
                const user = await Moralis.authenticate()
                document.getElementById('btn-login').innerHTML = user.get('ethAddress')
              }
            </script>
          </li>

          <li onclick="logout()" id="btn-logout" class="nav-item"><a href="#" class="nav-link"
              aria-current="page">Disconnect</a>
            <script>
              async function logout() {
                const user = await Moralis.User.logout(); // logOut ?
                document.getElementById('btn-logout')
              }
            </script>
          </li>
        </ul>
      </header>
    </div>
  </main>
</body>

</html>
<main class="col-md-9 ms-sm-4 col-lg-10 px-md-4">
  </div>
  <p>
  <h2>Deposit</h2>
  <div class="table-responsive">
    </p>
    <p>
    <div class="form-floating">


      <input type="amount" class="form-control" id="Amount-Payable" placeholder="Amount-Payable">
      <label for="Amount-Payable">
        <spann>*</spann>BNB Amount Payable (Min 0.1 BNB)
      </label>
    </div>
    </p>
    <p>

    <div class="form-floating">


      <input type="address" class="form-control" id="Uplline-Address" placeholder="Uplline-Address">
      <label for="Uplline-Address">
        <spann>*</spann>Uplline Address Ref Partners (0x0123456789...Abс)
      </label>
    </div>
    </p>
    <button onclick="deposit()" id="deposit" class="w-100 btn btn-lg btn-primary" type="submit">Send BNB and create a
      deposit</button>
  </div>
  <p>
  <h2>Withdraw</h2>
  <div class="table-responsive">
  </div>
  </p>

  <button onclick="withdraw()" id="withdraw" class="w-100 btn btn-lg btn-primary" type="submit">Withdraw interest in
    BNB</button>
  </div>

</main>
<script src="main.js"></script>
</body>

</html>
console.log('Hello world');

// connect to Moralis server
const serverUrl = "https://1jjca2c1r4ab.usemoralis.com:2053/server";
const appId = "f21REKbxRDZtrz0D7O7zcVqx7GjVy7cbbYHOdxd8";
Moralis.start({ serverUrl, appId });

// Кпопка ΠŸΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ мСтамаск https://docs.moralis.io/moralis-dapp/users/web3-login/metamask
login = async () => {
    await Moralis.authenticate().then(async function (user) {
        console.log('logged in');
        window.location.href = "indexx.html";
    })
}
// Ѐункция отобраТСния адрСса кошСлька Ρƒ ΡŽΠ·Π΅Ρ€Π° послС ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ
async function login() {
  const user = await Moralis.authenticate()
  document.getElementById('login').innerhtml = user.get('ethAddress')
}
if (document.querySelector('#btn-login') != null) {
    document.querySelector('#btn-login').onclick = login;
}
// Кнопка ΠžΡ‚ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ мСтамаск (Π½Π°Π΄ΠΎ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ Π½Π΅ срабатываСт)
logout = async () => {
    await Moralis.User.logout();
    console.log('logout')
    window.location.href = "indexx.html";
}
async function logout() {
    await Moralis.User.logout();
    console.log('logout')
    window.location.href = "indexx.html";
}
if (document.querySelector('#btn-logout') != null) {
    document.querySelector('#btn-logout').onclick = logout;
}

// Π€ΠΎΡ€ΠΌΠ° ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ Π΄Π°Π½Π½Ρ‹Ρ… Π² смарт ΠΊΠΎΠ½Ρ‚Ρ€Π°ΠΊΡ‚ Ρ„ΠΎΡ€ΠΌΠ° Π΄Π΅ΠΏΠΎΠ·ΠΈΡ‚ (Π½Π°Π΄ΠΎ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ) https://docs.moralis.io/moralis-dapp/web3/web3#example-of-calling-a-write-contract-method
async function deposit() {
    await Moralis.executeFunction(sendOptions)
    console.log('deposit')
    amount.set("amount", document.getElementById('Amount-Payable').value);
    address.set("address",document.getElementById('Uplline-Address').value);
    const amount = document.getElementById('Amount-Payable').value;
    const address = document.getElementById('Uplline-Address').value;
const ABI = [
    {
        "inputs": [
            {
                "internalType": "address",
                "name": "_upline",
                "type": "address"
            }
        ],
        "name": "deposit",
        "outputs": [],
        "stateMutability": "payable",
        "type": "function"
    },
  ];
  const sendOptions = {
    contractAddress: "0xEc08f00FE90fcf5e205b45Ac067f22C6F0e0854B",
    functionName: "deposit",
    abi: ABI,
    params: {
        _upline: "Uplline_Address", 
    },
    msgValue: "Amount_Payble",
  };
  await Moralis.executeFunction(sendOptions);
  console.log('deposit')
}
if (document.querySelector('#deposit') != null) {
    document.getElementById('#deposit').onclick = deposit
}

// Π€ΠΎΡ€ΠΌΠ° ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ Π΄Π°Π½Π½Ρ‹Ρ… Π² смарт ΠΊΠΎΠ½Ρ‚Ρ€Π°ΠΊΡ‚ ΠΊΠ½ΠΎΠΏΠΊΠ° Π’Ρ‹Π²ΠΎΠ΄ (Π½Π°Π΄ΠΎ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ) https://docs.moralis.io/moralis-dapp/web3/web3#example-of-calling-a-write-contract-method
async function withdraw() {
    await Moralis.executeFunction(sendOptions)
    console.log('withdraw')
    const ABI = [
    {
        "inputs": [],
        "name": "withdraw",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
  ];
  const sendOptions = {
    contractAddress: "0xEc08f00FE90fcf5e205b45Ac067f22C6F0e0854B",
    functionName: "withdraw",
    abi: ABI,
  };
  await Moralis.executeFunction(sendOptions);
  console.log('withdraw')
}
if (document.querySelector('#withdraw') != null) {
    document.getElementById('#withdraw').onclick = withdraw;
}

you can not use strings as placeholders here, you have to use values for msgValue for example

I didn’t quite understand, that is, I won’t be able to send the data of the completed form for writing to the smart contract?

Uploading: Π‘Π½ΠΈΠΌΠΎΠΊ экрана 2022-05-22 Π² 17.48.25.png…

I mean, to test with real values, not with a string as placeholder.

msgValue: "Amount_Payble", this is not going to work with a string for msgValue.

msgValue: Moralis.Units.ETH(β€œ0.1”), instead of this value can i do this?
msgValue: Moralis.Units.ETH(β€œAmount_Payable”),