Adding JS script, how to format JS I am trying to add this simple script to my block

&lt;script&gt;
    const config = {
      sourceAssetAddress: null,
      sourceAmountDecimal: null,
      destinationAssetAddress: null,
      destinationAmountDecimal: null,
      apiKey: null,
      partnerContractAddress: null,
    };
    const nodeId = "totle-widget";
    !function(){const t=document.createElement("script");t.type="text/javascript";const e=()=&gt;{TotleWidget.default.run(config,document.getElementById(nodeId))};t.readyState?t.onreadystatechange=function(){"loaded"!=t.readyState&amp;&amp;"complete"!=t.readyState||(t.onreadystatechange=null,e())}:t.onload=function(){e()},t.src="https://widget.totle.com/latest/dist.js",document.getElementsByTagName("head")[0].appendChild(t)}();
&lt;/script&gt;


Problem I have is that I tried to add it as library like this

(function ($) {

    'use strict';

    /**
     * Attaches the behavior to bootstrap carousel view.
     */
    Drupal.behaviors.totle_widget = {
      attach: function (context, settings) {

        const config = {
            sourceAssetAddress: null,
            sourceAmountDecimal: null,
            destinationAssetAddress: null,
            destinationAmountDecimal: null,
            apiKey: null,
            partnerContractAddress: null,
          };
          const nodeId = 'totle-widget';
          !function(){const t=document.createElement("script");t.type="text/javascript";const e=()=&gt;{TotleWidget.default.run(config,document.getElementById(nodeId))};t.readyState?t.onreadystatechange=function(){"loaded"!=t.readyState&amp;&amp;"complete"!=t.readyState||(t.onreadystatechange=null,e())}:t.onload=function(){e()},t.src="https://widget.totle.com/latest/dist.js",document.getElementsByTagName("head")[0].appendChild(t)}();
}}}(jQuery));


and somehow script goes beserk after this, loads many times and finaly breaks. It is attached to block in build and should be fine.

$build['#attached'] = array('library' =&gt; array('exch/totle'));


So I am not really sure what is wrong here. All I want to is to add this script somewhere in body of my html. Like when I add it directly into html.html.twig template of my theme and it works, but ofcourse I want to do it more in drupal way. What is wrong, how should I add this script to my library?
Using once with proper definition helped.

(function ($) {

    'use strict';

    Drupal.behaviors.totle_widget = {
        attach: function (context, settings) {

          // Using once() so script is loaded only once
          $('#totle-widget', context).once('totle_widget').each(function () {

            const config = {
                sourceAssetAddress: null,
                sourceAmountDecimal: null,
                destinationAssetAddress: null,
                destinationAmountDecimal: null,
                apiKey: null,
                partnerContractAddress: null,
              };
              const nodeId = 'totle-widget';
              !function(){const t=document.createElement("script");t.type="text/javascript";const e=()=&gt;{TotleWidget.default.run(config,document.getElementById(nodeId))};t.readyState?t.onreadystatechange=function(){"loaded"!=t.readyState&amp;&amp;"complete"!=t.readyState||(t.onreadystatechange=null,e())}:t.onload=function(){e()},t.src="https://widget.totle.com/latest/dist.js",document.getElementsByTagName("head")[0].appendChild(t)}();

          });
        }
      };

  }(jQuery));