zoukankan      html  css  js  c++  java
  • 135孤荷凌寒自学第0221天_区块链第135天NFT032

    【主要内容】

    今天继续自己开始写一个前端html页来与合约进行交互,共耗时32分钟。

    (此外整理作笔记花费了约13分钟)

    详细学习过程见文末学习过程屏幕录像。

    【新的Html前端页面】

    ```

    <!DOCTYPE html>

    <html>

    <head>

        <meta charset="UTF-8">

        <title>非同质化资产</title>

        <script language="javascript" type="text/javascript" src="jquery.min.js"></script>

        <script language="javascript" type="text/javascript" src="web3.min.js"></script>

        <!-- 1. Include cryptozombies_abi.js here -->

        <script language="javascript" type="text/javascript" src="nft_abi.js"></script>

        <script language="javascript" type="text/javascript" src="nft_main.js"></script>

     

           

    </head>

    <body>

        <div>

            <p>

                nft资产合约地址:

            </p>

            <p id="contractaddress" name="contractaddress">

     

            </p>

            <p id="goal" name="goal">

     

            </p>

     

            <p id="firstinfo" name="firstinfo">

     

            </p>

     

            <p id="accountsinfo" name="accountsinfo">

     

            </p>

            <p id="curinfo" name="curinfo">

     

            </p>

       

        </div>

        <div>

            <input type="text" value="" id="txtaddressforone" name="txtaddressforone" />

            <input type="button" value="查询指定节点资产总量" id="cmdone" name="cmdone" onclick="cmdone_click();" />

           

            <span id="total" name="total"></span>

            <br /><br />

     

            <input type="text" value="" id="txtnftidfortwo" name="txtnftidfortwo" />

            <input type="button" value="查询指定id的NFT是否存在" id="cmdone2" name="cmdone2" onclick="cmdone2_click();" />

            <span id="total2" name="total2"></span>

            <br /><br />

     

            <input type="text" value="" id="txtnftidfortwo2" name="txtnftidfortwo2" />

            <input type="button" value="查询指定id的NFT已授权给哪个节点地址" id="cmdtwo" name="cmdtwo" onclick="cmdtwo_click();" />

            <span id="spanapprove" name="spanapprove"></span>

            <br /><br />

     

            <span>查询所有资产的授权关系是否存在</span>

            <br />

            <span>授权方节点地址:</span>

            <input type="text" value="" id="txtaddressforthreeholder" name="txtaddressforthreeholder" />

            <br />

            <span>受托方节点地址:</span>

            <input type="text" value="" id="txtaddressforthreeoperator" name="txtaddressforthreeoperator" />

            <br />      

            <input type="button" value="查询所有资产完全授权状态是否存在" id="cmdthree" name="cmdthree" onclick="cmdthree_click();" />

            <br /> 

            <span id="allapproveaddress" name="allapproveaddress"></span>

            <br /><br />

     

            <span>查询指定节点是否拥有对指定资产的控制权</span>

            <br />

            <span>要查询的节点地址:</span>

            <input type="text" value="" id="txtaddressforfoure" name="txtaddressforfoure" />

            <br />

            <span>要查询的NFT资产ID:</span>  

            <input type="text" value="" id="txtnftforfoure" name="txtnftforfoure" />

            <br />             

            <input type="button" value="查询指定节点是否拥有对指定资产的控制权" id="cmdfoure" name="cmdfoure" onclick="cmdfoure_click();" />

            <span id="spanforfoure" name="spanforfoure"></span>

            <br /><br />

     

     

            <input type="text" value="" id="txtaddressforfive" name="txtaddressforfive" />

            <input type="button" value="查询节点地址是不是一个合约地址" id="cmdfive" name="cmdfive" onclick="cmdfive_click();" />

            <br />

            <span id="spanforfive" name="spanforfive"></span>

     

     

            <input type="text" value="" id="txtnftidforsix" name="txtnftidforsix" />

            <input type="button" value="查询指定id的NFT是哪个节点的" id="cmdsix" name="cmdsix" onclick="cmdsix_click();" />

            <span id="spanforsix" name="spanforsix"></span>

            <br /><br />

     

     

            <input type="text" value="" id="txtnftidforseven" name="txtnftidforseven" />

            <input type="button" value="查询指定id的NFT的具体数据" id="cmdseven" name="cmdseven" onclick="cmdseven_click();" />

            <span id="spanforseven" name="spanforseven"></span>

            <br /><br />

     

            <span>查询指定在所属者自己资产库的排序INDEX值NFT资产的ID</span>

            <br />

            <span>要查询的节点地址:</span>

            <input type="text" value="" id="txtaddressforeight" name="txtaddressforeight" />

            <br />

            <span>要查询的NFT资产index(从零开始计数):</span>  

            <input type="text" value="" id="txtnftforeight" name="txtnftforeight" />

            <br />             

            <input type="button" value="查询id" id="cmdeight" name="cmdeight" onclick="cmdeight_click();" />

            <span id="spanforeight" name="spanforeight"></span>

            <br /><br />

     

            <input type="text" value="" id="txtaddressfornine" name="txtaddressfornine" />

            <input type="button" value="查询指定节点地址的资产列表" id="cmdnine" name="cmdnine" onclick="cmdnine_click();" />

            <br />

            <span id="spanfornine" name="spanfornine"> </span>

     

            <!--============================================-->

            <hr />

            <span>生成资产:</span>

            <br />

            <span>为哪个节点地址生成资产:</span>

            <input type="text" value="" id="addressgenerate" name="addressgenerate" />

            <br />

            <span>要生成的资产数据:</span>  

            <input type="text" value="" id="datagenerate" name="datagenerate" />

            <br />             

            <input type="button" value="开始生成" id="cmdgenerate" name="cmdgenerate" onclick="cmdgenerate_click();" />

            <br />

            <span id="spanforgenerate" name="spanforgenerate"></span>

            <br /><br />       

     

           <!--============================================-->

            <hr />

            <span>授权另一个节点操作单个资产:</span>

            <br />

            <span>授权给哪个节点权限:</span>

            <input type="text" value="" id="addressapprove" name="addressapprove" />

            <br />

            <span>要授权的资产ID:</span>  

            <input type="text" value="" id="idapprove" name="idapprove" />

            <br />             

            <input type="button" value="确认授权" id="cmdapprove" name="cmdapprove" onclick="cmdapprove_click();" />

            <br />

            <span id="spanforapprove" name="spanforapprove"></span>

            <br /><br />       

     

           <!--============================================-->

           <hr />

           <span>单个资产的转移:</span>

           <br />

     

           <span>接收方节点地址:</span>

           <input type="text" value="" id="addressto" name="addressto" />

           <br />      

           <span>要转移的资产ID:</span>  

           <input type="text" value="" id="idtransfer" name="idtransfer" />

           <br />

           <span>附加信息bytes:</span>  

           <input type="text" value="" id="databyte" name="databyte" />

           <br />               

           <input type="button" value="确认转移" id="cmdtransfer" name="cmdtransfer" onclick="cmdtransfer_click();" />

           <br />

           <span id="spanfortransfer" name="spanfortransfer"></span>

           <br /><br />       

     

           <!--============================================-->

           <hr />

           <span>授权另一个节点操作所有资产:</span>

           <br />

           <span>授权给哪个节点权限:</span>

           <input type="text" value="" id="addressapproveall" name="addressapproveall" />

           <br />

           <span>生成授权还是撤回授权</span>  

           <select id="selapproveall" name="selapproveall">

                <option value="true">生成授权</option>

                <option value="false">撤回授权</option>

           </select>

           <br />             

           <input type="button" value="确认授权" id="cmdapproveall" name="cmdapproveall" onclick="cmdapproveall_click();" />

           <br />

           <span id="spanforapproveall" name="spanforapproveall"></span>

           <br /><br /> 

     

     

     

        </div>

       

    </body>

    </html>

    ```

    js代码:

    ```

    function isNumber(val) { //https://www.cnblogs.com/wangyunhui/p/8981813.html

        var regPos = /^d+(.d+)?$/; //非负浮点数

        var regNeg = /^(-(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数

        if(regPos.test(val) || regNeg.test(val)) {

            return true;

            } else {

            return false;

            }

        }

     

    //获取指定id的select标签选中value或text

    function getAselectValueOrText(strid,isvalue){

        try {

            var obj=document.getElementById(strid);

            var intindex=obj.selectedIndex;

            if(isvalue==true){

                return obj.options[intindex].value;

            }else{

                return obj.options[intindex].text;

            }

        } catch (error) {

            return "";

        }

    }

    //得到bool类型的值   https://blog.csdn.net/asdfgh0077/article/details/103365856 第七楼

    function parseBool(value) {

        if (typeof value === "boolean") return value;

     

        if (typeof value === "number") {

            return value === 1 ? true : value === 0 ? false : undefined;

        }

     

        if (typeof value != "string") return undefined;

     

        return value.toLowerCase() === 'true' ? true : false;

    }

     

    // 格式化日期,如月、日、时、分、秒保证为2位数

    function formatNumber (n) {

        n = n.toString()

        return n[1] ? n : '0' + n;

    }

    // 参数number为毫秒时间戳,format为需要转换成的日期格式

    function formatTime (number, format) {

        let time = new Date(number)

        let newArr = []

        let formatArr = ['Y', 'M', 'D', 'h', 'm', 's']

        newArr.push(time.getFullYear())

        newArr.push(formatNumber(time.getMonth() + 1))

        newArr.push(formatNumber(time.getDate()))

     

        newArr.push(formatNumber(time.getHours()))

        newArr.push(formatNumber(time.getMinutes()))

        newArr.push(formatNumber(time.getSeconds()))

     

        for (let i in newArr) {

            format = format.replace(formatArr[i], newArr[i])

        }

        return format;

    }

     

                //声明一些钱包地址:

                //下面一行定义的是部署合约的节点(创世节点)的信息,公钥

                var wallet_address="0x5227C3EF48B5A1bcF784593e46D9579D26a3b592"; //狐狸钱包的公钥,就是钱包地址,是eth网络上的一个节点。

                //下面一行定义的是节点2的信息

                var w2add="0xe2d6c2f289c53B5aEA44C47293Ba179a3bfa21f0"; //公钥

        

                //下面一行定义的是节点3 的信息

                var w3add="0xb40599fB0366DCf0ffe86677b005b3f20Dfa29aE"; //公钥

        

                //下面一行定义的是节点4 的信息

                var w4add="0x70c8461366d5368B1E79CBFc2Acf4ba56C745977"; //公钥

        

        

       

                // 2. Start code here

                var cc;

                var web3;

                //----下面是StandardAssetRegistryTest的合约地址

                var heyueAddress='0xeD4202E35c63EDfDC38Fff34100Ac86217960DD3';

     

                function startApp() {

                    try {

                        $("#contractaddress").html(heyueAddress);

                        var ccc=web3.eth.contract(nftABI);

                        cc=ccc.at(heyueAddress); //https://www.cnblogs.com/tinyxiong/p/9046626.html

                        //cc =new web3.eth.contract(cryptozombiesABI, cryptoZombiesAddress); //如果是另一个版本可能还得加上new关键字。

                        $("#firstinfo").html("连接上合约!")

                        //alert(typeof cc);

                        web3.eth.getAccounts(function (err, accounts) {

                                if (accounts.length == 0) {

                                    $("#firstinfo").html("请检查钱包是否解锁");

                                }else{

                                    $("#accountsinfo").html("获取的默认钱包地址:" + web3.eth.defaultAccount);

                                    //getCurGift();

                                }

                            });

        

                    } catch (err) {

                        alert(err);

                    }

                    $("#firstinfo").html("加载成功");

                    

                }

        

                //async () => 

     

            //现在这种通过we3.min.js来加载钱包连接的方法,在metamask钱包和麦子钱包中都测试通过。

            window.addEventListener('load',function() {

            try{

                if (typeof web3 !== 'undefined') {

                    web3 = new Web3(web3.currentProvider);

                    startApp();

                } else {

                    //$('#app_loading').hide();

                    //alert(jQuery.i18n.prop('lrn_error_alert'));

                    //mathWallet.closePage();

                    alert("这儿没有钱包环境。");

                }

     

            }catch(err){

                alert(err);

            }

            });

     

            //===========================================================================================

        

                //----------------下面是自定义的与合约交互的函数-------------------------

               //1--查询指定节点的NFT资产数量-----

            function cmdone_click(){

                try{

                    querryaddressbalance();

                }catch(err){

                    alert(err);

                }

            }

            //--查询指定节点的资产余额---

                function querryaddressbalance(){

                    try{

                        var a=document.getElementById("txtaddressforone").value;

                        if(a!=""){

                            cc.balanceOf(a,function(error, result){

                            if(!error)

                            {

                                //var da=result[0];

                                //var xi=result[1];

                                //var intda=da / (10**18);

                                //var intxi=xi / (10**18); 

                                $("#total").html("当前地址拥有:" + result + "个资产");

                                //alert(result);

                            }

                            else{

                                //alert(error);

                                $("#total").html('获取出错:' + error);

                            }});

                            //return "ok"

                            $("#total").html('正在获取。。。');

                        }else{

                            alert("请先填写正确的节点地址。");

                        }

     

                    }catch(err){

                        $("#total").html('出错 :' + err);

                    }

                }

                //---------------------------------------------------------

                //--查询指定ID的NFT资产是否存在---

                        function cmdone2_click(){

                            try{

                                var strid=document.getElementById("txtnftidfortwo").value;

                                if(isNumber(strid)==true){

                                    var nid=parseInt(strid);

                                    isnftexist(nid);                                

                                }

     

                            }catch(err){

                                alert(err);

                            }

                        }

                

                function isnftexist(intid){

                    try{

                        cc.exists(intid,function(error, result){

                        

                        //alert(result);

                        //alert(result.toString + "---"); //如果这样写toString那么就返回一个函数对象而不是字符串,要加括号!

     

                        if(!error)

                        {

                            var strls=result.toString();

                            //alert(strls);

                            if(strls.search("true")>=0){

                                $("#total2").html("id为" + intid + "的nft资产存在。");

                            }else{

                                $("#total2").html("id为" + intid + "的nft资产不存在。");

                            }

     

                        }

                        else{

                            //alert(error);

                            $("#total2").html('获取出错:' + error);

                        }});

                        //return "ok"

                        $("#total2").html('正在获取。。。');

                    }catch(err){

                        $("#total2").html('出错 :' + err);

                    }

                }

    //------------------------------------------------------------------------------------    

            //2查询指定ID的NFT资产已授权给哪个节点地址

            function cmdtwo_click(){

                try{

                    var strid=document.getElementById("txtnftidfortwo2").value;

                    if(isNumber(strid)==true){

                        var nid=parseInt(strid);

                        getApproved(nid);                                

                    }                

                }catch(err){

                    alert(err);

                }

            }

     

                //查询指定ID的NFT资产已授权给哪个节点地址

                function getApproved(intid){

                    try{

                        cc.getApproved(intid,function(error, result){

                        //alert(result);

                        if(!error)

                        {

                            var strls=result.toString();

                            if(strls.search("0x0000000000000000000000000000000000000000")>=0){

                                document.getElementById("spanapprove").innerText="此资产并没有授权给任何节点";

                            }else{

                                document.getElementById("spanapprove").innerText="此资产已授权给:" + result;

                            }

                        }

                        else{

                            //alert(error);

                            document.getElementById("spanapprove").innerText='获取出错:' + error;

                        }});

                        //return "ok"

                        document.getElementById("spanapprove").innerText='正在获取。。。';

                    }catch(err){

                        alert("使用授权代币转移时出错 :" + err)

                        return err;

                    }

                }

     

    //----------------------------------------------------------------------------------------

                //3--查询一个节点是不是向另一个节点授权了所有资产的操作权-----------------

                function cmdthree_click(){

                    try{

                        var strholder=document.getElementById("txtaddressforthreeholder").value;

                        var stroperator=document.getElementById("txtaddressforthreeoperator").value;

                        //alert(strholder);

                        //alert(stroperator);

                        if(strholder!="" && stroperator!=""){

                            isApprovedForAll(strholder,stroperator);

                        }else{

                            alert("请先输入两个节点的地址。");

                        }

                    }catch(err){

                        alert(err);

                    }

                }

     

                function isApprovedForAll(strholder,stroperator){

                    try{

                        //alert(strholder);

                        //alert(stroperator);

                        cc.isApprovedForAll(strholder,stroperator,function(error, result){

                        if(!error)

                        {

                            var strls=result.toString();

                            //alert(strls);

                            if(strls.search("true")>=0){

                                document.getElementById("allapproveaddress").innerText='授权关系存在。';

                            }else{

                                document.getElementById("allapproveaddress").innerText='授权关系不存在。';

                            }

     

                        }

                        else{

                            //alert(error);

                            document.getElementById("allapproveaddress").innerText='获取出错:' + error;

                        }});

                        //return "ok"

                        document.getElementById("allapproveaddress").innerText='获取中.....';

                    }catch(err){

                        alert("查询出错:" + err)

                        return err;

                    }

                }

     

    //------------------------------------------------------------------------------------------------------

               //4--查询指定的address是否已经拥有了对指定ID的NFT资产的控制权

               function cmdfoure_click(){

                    try{

                        var strid=document.getElementById("txtnftforfoure").value;

                        var straddress=document.getElementById("txtaddressforfoure").value;

                        if(isNumber(strid)==true){

                            var nid=parseInt(strid);

                            isAuthorized(straddress,nid);                                

                        }                                    

                    }catch(err){

                        alert(err);

                    }

                }

     

                function isAuthorized(straddress,intid){

                    try {

                        cc.isAuthorized(straddress,intid,function(error, result){

                        if(!error)

                        {

                            var strls=result.toString();

                            //alert(strls);

                            if(strls.search("true")>=0){

                                document.getElementById("spanforfoure").innerText='授权关系存在。';

                            }else{

                                document.getElementById("spanforfoure").innerText='授权关系不存在。';

                            }

     

                        }

                        else{

                            //alert(error);

                            $("#spanforfoure").html('获取出错:' + error);

                        }});

                        //return "ok"

                        $("#spanforfoure").html('正在获取。。。');

                    } catch (error) {

                        alert("在查询指定节点是否拥有对指定资产的控制权时出错:" + error)

                    }

                }

    //----------------------------------------------------------------------------------------

               //5--查询指定节点的NFT资产数量-----

               function cmdfive_click(){

                try{

                    isContractProxy();

                }catch(err){

                    alert(err);

                }

            }

            //--查询指定节点的资产余额---

                function isContractProxy(){

                    try{

                        var a=document.getElementById("txtaddressforfive").value;

                        if(a!=""){

                            cc.isContractProxy(a,function(error, result){

                            if(!error)

                            {

                                var strls=result.toString();

                                //alert(strls);

                                if(strls.search("true")>=0){

                                    document.getElementById("spanforfive").innerText='这是一个合约地址。';

                                }else{

                                    document.getElementById("spanforfive").innerText='这是一个普通地址。';

                                }

        

        

                            }

                            else{

                                //alert(error);

                                document.getElementById("spanforfive").innerText='获取出错:' + error;

                            }});

                            //return "ok"

                            $("#spanforfive").html('正在获取。。。');

                        }else{

                            alert("请先填写正确的节点地址。");

                        }

     

                    }catch(err){

                        $("#spanforfive").html('出错 :' + err);

                    }

                }

    //---------------------------------------------------------------------------------------------------

            //6---查询一个指定ID的NFT资产是属于哪个节点的========

            function cmdsix_click(){

                try{

                    var strid=document.getElementById("txtnftidforsix").value;

                    if(isNumber(strid)==true){

                        var nid=parseInt(strid);

                        ownerOf(nid);                                

                    }                

                }catch(err){

                    alert(err);

                }

            }

     

            function ownerOf(intid){

                try{

                    cc.ownerOf(intid,function(error, result){

                    //alert(result);

                    if(!error)

                    {

                        var strls=result.toString();

                        if(strls.search("0x0000000000000000000000000000000000000000")>=0){

                            document.getElementById("spanforsix").innerText="此资产不属于任何节点";

                        }else{

                            document.getElementById("spanforsix").innerText="此资产属于:" + result;

                        }

                    }

                    else{

                        //alert(error);

                        document.getElementById("spanforsix").innerText='获取出错:' + error;

                    }});

                    //return "ok"

                    document.getElementById("spanforsix").innerText='正在获取。。。';

                }catch(err){

                    alert("查询出错 :" + err)

                    return err;

                }

            }

     

    //---------------------------------------------------------------------------------------------------

            //7---查询一个指定ID的NFT资产具体数据,如URL或组成数据代码等========

            function cmdseven_click(){

                try{

                    var strid=document.getElementById("txtnftidforseven").value;

                    if(isNumber(strid)==true){

                        var nid=parseInt(strid);

                        tokenMetadata(nid);                                

                    }                

                }catch(err){

                    alert(err);

                }

            }

     

            function tokenMetadata(intid){

                try{

                    cc.tokenMetadata(intid,function(error, result){

                    //alert(result);

                    if(!error)

                    {

     

                        document.getElementById("spanforseven").innerText=result;

                    }

                    else{

                        //alert(error);

                        document.getElementById("spanforseven").innerText='获取出错:' + error;

                    }});

                    //return "ok"

                    document.getElementById("spanforseven").innerText='正在获取。。。';

                }catch(err){

                    alert("查询出错 :" + err)

                    return err;

                }

            }

     

    //----------------------------------------------------------------------------------------

               //8--查询指定ID的NFT资产在指定ADDRESS用户资产库的index值

               function cmdeight_click(){

                try{

                    var strid=document.getElementById("txtnftforeight").value;

                    var straddress=document.getElementById("txtaddressforeight").value;

                    if(isNumber(strid)==true){

                        var nid=parseInt(strid);

                        tokenOfOwnerByIndex(straddress,nid);                                

                    }                                    

                }catch(err){

                    alert(err);

                }

            }

     

            function tokenOfOwnerByIndex(straddress,intid){

                try {

                    cc.tokenOfOwnerByIndex(straddress,intid,function(error, result){

                    if(!error)

                    {

                        var strls=result.toString();

                        if(strls=="0"){

                            document.getElementById("spanforeight").innerText="你没有这个资产,你的资产只有" + intid + "个"

                        }else{

                            document.getElementById("spanforeight").innerText='id:' + result;

                        }



                    }

                    else{

                        //alert(error);

                        $("#spanforeight").html('获取出错:' + error);

                    }});

                    //return "ok"

                    $("#spanforeight").html('正在获取。。。');

                } catch (error) {

                    alert("在查询指定节点是否拥有对指定资产的控制权时出错:" + error)

                }

            }

     

    //----------------------------------------------------------------------------------------------

               //9--查询指定节点的NFT资产列表-----

               function cmdnine_click(){

                try{

                    tokensOf();

                }catch(err){

                    alert(err);

                }

            }

            //--查询指定节点的资产列表---

                function tokensOf(){

                    try{

                        var a=document.getElementById("txtaddressfornine").value;

                        if(a!=""){

                            cc.tokensOf(a,function(error, result){

                            if(!error)

                            {

                                if(result.length>0){

                                    var strls="此节点拥有以下ID的资产: ";

                                    for(var i=0;i<result.length;i++){

                                        strls=strls + result[i] + " ";

                                        document.getElementById("spanfornine").innerText=strls;

                                    }

     

                                }else{

                                    document.getElementById("spanfornine").innerText="此节点没有任何资产";

                                }

     

                            }

                            else{

                                //alert(error);

                                document.getElementById("spanfornine").innerText=error;

                            }});

                            //return "ok"

                            document.getElementById("spanfornine").innerText="正在查询...";

                        }else{

                            document.getElementById("spanfornine").innerText="请先输入合约地址。";

                        }

     

                    }catch(err){

                        document.getElementById("spanfornine").innerText='出错 :' + err;

                    }

                }

     

    //-----------------------------------------------------------------------------------------------

     

                //生成资产

                function cmdgenerate_click(){

                    var a=""; //要生成资产的地址

                    var s=""; //资产的具体数据

                    try {

                        a=document.getElementById("addressgenerate").value;

                        s=document.getElementById("datagenerate").value;

                        if(a==""){

                            alert("请先输入要获得资产的节点地址。");

                            return false;

                        }

                        if(s==""){

                            alert("必须指定资产的具体数据");

                            return false;

                        }

                    } catch (error) {

                        alert("在生成资产时出错:" + error);

                        return false;

                    }

                    try{

                        web3.eth.getTransactionCount(web3.eth.defaultAccount,function(err,res){

                            if(!err){

                                //alert(res);web3.toWei(intvalue,'ether')

                                var message = {to:heyueAddress,value:0,'chainId': 3,'gas': 300000,'gasPrice': web3.toWei('40', 'gwei'),'nonce': res};

                                cc.generate(a,s,message,function(err, res){

                                    var output = "";

                                    if (!err) {

                                        output += res;

                                    } else {

                                        output = err;

                                    }

                                    //----listendonate();

                                    document.getElementById('spanforgenerate').innerHTML = "返回信息:Transaction response= " + output + "<br />";

                                });

                                return true;

                            }else{

                                alert(err);

                            }

                        }); 

                        document.getElementById('spanforgenerate').innerHTML = "操作执行中...";

                    }catch(err){

                        alert('执行生成资产出错:' + err);

                        document.getElementById('spanforgenerate').innerHTML = "执行失败:Transaction response= " + output + "<br />";

                        return false;

                    }

                }

    //-----------------------------------------------------------------------------------------

    //授权单个资产------

                function cmdapprove_click(){

                    var a=""; //要生成资产的地址

                    var s=""; //资产的id

                    try {

                        a=document.getElementById("addressapprove").value;

                        s=document.getElementById("idapprove").value;

                        if(a==""){

                            alert("请先输入要授权给哪个节点地址。");

                            return false;

                        }

                        if(isNumber(s)==false){

                            alert("要授权给另一个节点操作的资产ID是一个数字");

                            return false;

                        }

                    } catch (error) {

                        alert("在资产授权时出错:" + error);

                        return false;

                    }

                    try{

                        web3.eth.getTransactionCount(web3.eth.defaultAccount,function(err,res){

                            if(!err){

                                //alert(res);web3.toWei(intvalue,'ether')

                                var message = {to:heyueAddress,value:0,'chainId': 3,'gas': 300000,'gasPrice': web3.toWei('40', 'gwei'),'nonce': res};

                                cc.approve(a,s,message,function(err, res){

                                    var output = "";

                                    if (!err) {

                                        output += res;

                                    } else {

                                        output = err;

                                    }

                                    //----listendonate();

                                    document.getElementById('spanforapprove').innerHTML = "返回信息:Transaction response= " + output + "<br />";

                                });

                                return true;

                            }else{

                                alert(err);

                            }

                        }); 

                        document.getElementById('spanforapprove').innerHTML = "操作执行中...";

                    }catch(err){

                        alert('执行单个资产授权出错:' + err);

                        document.getElementById('spanforapprove').innerHTML = "执行失败:Transaction response= " + output + "<br />";

                        return false;

                    }                

                }

    //=------------------------------------------------------------------------------

    //----转移资产----------------------

                function cmdtransfer_click(){

                    var afrom=""; 

                    var ato=""

                    var id=""; //资产的id

                    var databytes="";

                    try {

                        afrom=web3.eth.defaultAccount;

                        ato=document.getElementById("addressto").value;

                        id=document.getElementById("idtransfer").value;

                        databytes=document.getElementById("databyte").value;

                        if(afrom==""){

                            alert("没有获取到当前钱包地址。");

                            return false;

                        }

                        if(ato==""){

                            alert("请先输入要向哪个地址转入资产。");

                            return false;

                        }                    

                        if(isNumber(id)==false){

                            alert("资产ID必须是一个数字");

                            return false;

                        }

                    } catch (error) {

                        alert("在准备转移资产时出错:" + error);

                        return false;

                    }

     

                    try{

                        web3.eth.getTransactionCount(web3.eth.defaultAccount,function(err,res){

                            if(!err){

                                //alert(res);web3.toWei(intvalue,'ether')

                                var message = {to:heyueAddress,value:0,'chainId': 3,'gas': 300000,'gasPrice': web3.toWei('40', 'gwei'),'nonce': res};

                                cc.safeTransfer(afrom,ato,id,databytes,message,function(err, res){

                                    var output = "";

                                    if (!err) {

                                        output += res;

                                    } else {

                                        output = err;

                                    }

                                    //----listendonate();

                                    document.getElementById('spanfortransfer').innerHTML = "返回信息:Transaction response= " + output + "<br />";

                                });

                                return true;

                            }else{

                                alert(err);

                            }

                        }); 

                        document.getElementById('spanfortransfer').innerHTML = "操作执行中...";

                    }catch(err){

                        alert('执行单个资产转移出错:' + err);

                        document.getElementById('spanfortransfer').innerHTML = "执行失败:Transaction response= " + output + "<br />";

                        return false;

                    }  

     

                }

     

    //------------------------------------------------------------------------------------------

    //------把自己的资产全部授权----------------------------

                function cmdapproveall_click(){

                    var a=""; //要授权去的地址

                    var s=""; //true OR false

                    try {

                        a=document.getElementById("addressapproveall").value;

                        s=getAselectValueOrText("selapproveall",true);

                        if(a==""){

                            alert("请先输要授权给哪个节点地址。");

                            return false;

                        }

                        if(s==""){

                            alert("请指明授权还是撤消授权");

                            return false;

                        }

                    } catch (error) {

                        alert('执行全部资产授权出错:' + error);

                        return false;

                    }                

                    try {

                        web3.eth.getTransactionCount(web3.eth.defaultAccount,function(err,res){

                            if(!err){

                                //alert(res);web3.toWei(intvalue,'ether')

                                var message = {to:heyueAddress,value:0,'chainId': 3,'gas': 300000,'gasPrice': web3.toWei('40', 'gwei'),'nonce': res};

                                cc.setApprovalForAll(a,parseBool(s),message,function(err, res){

                                    var output = "";

                                    if (!err) {

                                        output += res;

                                    } else {

                                        output = err;

                                    }

                                    //----listendonate();

                                    document.getElementById('spanforapproveall').innerHTML = "返回信息:Transaction response= " + output + "<br />";

                                });

                                return true;

                            }else{

                                alert(err);

                            }

                        }); 

                        document.getElementById('spanforapproveall').innerHTML = "操作执行中...";                    

                    } catch (error) {

                        alert('执行全部资产授权出错:' + err);

                   

                        return false;                    

                    }

                }



                //监听事件

                function listendonate(){

                    try {

                        document.getElementById('donateback').innerText="事件结果监听中......";

                        //------------------------------------------

                        var event=cc.FundTransfer();

                        event.watch(function(error,result){

                            if(!error){

                                var strls="";

                                var strls2="";

                                for(let key in result){

                                   strls=strls + key + " : " + result[key] + "; ";

                                }

                                //https://me.tryblockchain.org/blockchain-solidity-event.html

                                try {

                                    var fl=0.0;

                                    try {

                                        fl=(result.args.amount - 0) / (10 ** 18);

                                    } catch (error) {

                                        fl=0.0

                                    }

                                    var strls3=result.args.amount + "wei";

                                    if(fl!=0.0){

                                        strls3=fl.toString + "ether"

                                    }

                                    strls2="参与众筹金额:" + strls3 + ",得到的代币CI金额" + result.args.backci + "ci.";

                                } catch (error) {

                                    strls2="具体事件参数信息未能获取到:" + error;

                                }

     

                                document.getElementById('donateback').innerHTML=strls + "<br />" + strls2;

                            }else{

                               document.getElementById('donateback').innerText=error;

                            }

                        })

     

                        /*

                            下面是接收到的返回事件监控信息:

                            address : 0x7a2559f23e056f39e844a465600eb605c4e3aeabblockHash : 0xfab4a6bc95c9aa41fc688e89a87023b124fd581c6bd680226ab08b3d883002ebblockNumber : 6459690logIndex : 1removed : falsetransactionHash : 0xdcd4b44f01fce402ac56ec22be5d6ba479195a48db3ea8082382b4cea81af856transactionIndex : 0event : FundTransferargs : [object Object]

    undefined,1000000000000000,100,100000000000000000000,100,100000 

     

                        */

     

                        //上面的写法成功,但只能监听当前 一次事件

     

                        /*

                            注意参照以下代码:

                            instructorEvent.watch(function(error, result) {

                                if (!error)

                                    {

                                        $("#info").html(result.args.name + ' (' + result.args.age + ' years old)');

                                    } else {

                                        console.log(error);

                                    }

                            });

                        */

     

                        //下面这种写法报错

                        //var myEvent = cc.events.FundTransfer({

                        //    filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23

                        //                fromBlock: 0

                        //                },

                        //    fromBlock: 0

                        //}, function(error, event){})

                        //.on('data', function(event){

                        //    document.getElementById("listenback").innerText=event; // same results as the optional callback above

                        //})

                        //.on('changed', function(event){

                        //        // remove event from local database

                        //})        

                        //.on('error', console.error);

     

                        

                    } catch (error) {

                        document.getElementById('donateback').innerText="监听事件错误:" + error;

                    }

                }

     

               

    ```

    今天对Js代码中bool类型的一些理解

    1.字符串可以自动转换为bool值,

    规则是:

    参数为 0、null 和无参数返回false,有参数返回true。

    默认情况下,相当于对字符串运用了Boolean()函数

    示例:

    ```

    Boolean("");  //输出为:false

    Boolean(null);  //输出为:false

    Boolean(0);  //输出为:false

    Boolean("hi");  //输出为:true

    Boolean(100);  //输出为:true

    Boolean(new  Object());  //输出为:true

    ```

    2.但是,不是我最初猜想的那样

    字符串“true”àtrue     这是可以的,因为字符串不为空,所以认为是true

    字符串“false”àfalse    这是想多了,怎么可能 ,因为字符串也不为空,所以认为是true

    3.当把“true”和“false”这样的字符串传递给solidity的合约函数时,如果合约函数需要的参数为bool类型,也会自动转换为bool类型,原则与js是一样的,因此,这两个字符串转换后的bool值都是true.

    4.因此不能想当然地用字符串“true”和“false”来当作bool类型传递给合约函数。

     

     

    github: https://github.com/lhghroom/Self-learning-blockchain-from-scratch

     

     

    【欢迎大家加入[就是要学]社群】

    如今,这个世界的变化与科技的发展就像一个机器猛兽,它跑得越来越快,跑得越来越快,在我们身后追赶着我们。

    很多人很早就放弃了成长,也就放弃了继续奔跑,多数人保持终身不变的样子,原地不动,成为那猛兽的肚中餐——当然那也不错,在猛兽的逼迫下,机械的重复着自我感觉还良好地稳定工作与生活——而且多半感觉不到这有什么不正常的地方,因为在猛兽肚子里的是大多数人,就好像大多数人都在一个大坑里,也就感觉不出来这是一个大坑了,反而坑外的世界显得有些不大正常。

    为什么我们不要做坑里的大多数人?

    因为真正的人生,应当有百万种可能 ;因为真正的一生可以有好多辈子组成,每一辈子都可以做自己喜欢的事情;因为真正的人生,应当有无数种可以选择的权利,而不是总觉得自己别无选择。因为我们要成为一九法则中为数不多的那个一;因为我们要成为自己人生的导演而不是被迫成为别人安排的戏目中的演员。

    【请注意】

    就是要学社群并不会告诉你怎样一夜暴富!也不会告诉你怎样不经努力就实现梦想!

    【请注意】

    就是要学社群并没有任何可以应付未来一切变化的独门绝技,也没有值得吹嘘的所谓价值连城的成功学方法论!

    【请注意】

    社群只会互相帮助,让每个人都看清自己在哪儿,自己是怎样的,重新看见心中的梦想,唤醒各自内心中的那个英雄,然后勇往直前,成为自己想要成为的样子!

    期待与你并肩奔赴未来!

     

    QQ群:646854445 (【就是要学】终身成长)

     

     

    【原文地址】

    https://www.941xue.com/content.aspx?k=941XUEYRWZKV06057824114026484121

    【同步语音笔记】

    https://www.ximalaya.com/keji/19103006/363255055

     

    【学习过程屏幕录屏】

    https://www.bilibili.com/video/BV1Ea411A7wy/

    欢迎大家添加我为好友: QQ: 578652607
  • 相关阅读:
    C 语言中字符的输入输出
    C 语言 ctype.h 中系列字符处理函数
    C 语言中 for 循环的几种用法
    C 中优先级和关系运算符
    字符串和格式化输入/输出 [printf & scanf]
    C++中关于string类的一些API总结
    两大基本数据类型
    这些时候的总结
    PL/SQL 十进制数转任意进制
    复现题目[CISCN 2019 华东北赛区 Web2 WriteUp](https://www.zhaoj.in/read-6100.html)的一些东西
  • 原文地址:https://www.cnblogs.com/lhghroom/p/14365331.html
Copyright © 2011-2022 走看看