美文网首页
js-conflux-sdk封装函数

js-conflux-sdk封装函数

作者: symY_Y | 来源:发表于2023-01-15 10:14 被阅读0次

    common.js

    import abi from "../abi/ERC1155.json";
    import { notification } from "ant-design-vue";
    import { Conflux, Drip, PrivateKeyAccount  } from "js-conflux-sdk";
    const cfx = new Conflux({
      url: process.env.VUE_APP_CONFLUX_URL, // 测试网络
      networkId: Number(process.env.VUE_APP_NETWORK_ID), // 正式1029
    });
    window.confluxJS = cfx;
    // 创建要交互的合约对象,参数为合约的abi和地址
    const contract = window.confluxJS.Contract({
      abi,
      address: process.env.VUE_APP_ADDRESS,
    });
    // create 账户
    // 通过私钥创建
    const account = window.confluxJS.wallet.addPrivateKey(privateKey);
    // 通过钱包创建
    // const account = window.confluxJS.wallet.addRandom();
    // 通过私钥账户创建
    // const randomSeed = "0xfffff"; // any random buffer
    // const account = PrivateKeyAccount.random(randomSeed, 1);
    let address = "";
    export default {
      install(Vue) {
        // 连接钱包
        Vue.prototype.connectedWallet = async function () {
          if (window.conflux) {
            const fromAddress = await window.conflux.request({
              method: "cfx_requestAccounts",
            });
            this.$store.commit("setWallentAddress", fromAddress);
            address = fromAddress;
            notification.success({
              message: "提示",
              description: "已成功连接钱包",
            });
          } else {
            notification.error({
              message: "提示",
              description: "请安装Fluent钱包",
            });
            // 打开安装钱包的地址
            setTimeout(() => {
              window.open("https://fluentwallet.com/", "_blank");
            }, 1000);
          }
        };
        // 获取钱包CFX余额
        Vue.prototype.getCFX = async function () {
          if (address) {
            const balance = await window.confluxJS.cfx.getBalance(address);
            console.log(balance, Drip(balance).toGDrip()); // "4999998839889983249999999.950307784"
            // console.log(Drip(balance).toCFX()); // "4999998839889983.249999999950307784"
          }
        };
        // 读取合约
        Vue.prototype.readContract = async function (method, params) {
          if (await window.conflux.isConnected()) {
            // 读取合约信息
            let nftInfo;
            if (params) {
              nftInfo = await contract[method](params);
            } else {
              nftInfo = await contract[method]();
            }
            return { type: "contract", content: nftInfo };
          } else {
            await Vue.prototype.connectedWallet();
          }
        };
        // 写入合约
        Vue.prototype.writeContract = async function (method, params) {
          if (await window.conflux.isConnected()) {
            // 写入合约  -- 写入合约是异步的,不能及时获取结果,还需处理
            const result = await contract[method](...params)
              .sendTransaction({
                from: account ,
              })
              .confirmed();
            return { type: "contract", content: result };
          } else {
            await Vue.prototype.connectedWallet();
          }
        };
        // 两个钱包之间的交易
        Vue.prototype.getTransaction = async function (toAddress,value) {
          if (await window.conflux.isConnected()) {
            const params = [
              {
                from: address[0],
                to: toAddress,
                gas: "0x76c0", // 30400
                gasPrice: "0x9184e72a000", // 10000000000000
                value,                    //"0x9184e72a"   注意格式
                data: "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675",
              },
            ];
            window.conflux
              .request({ method: "cfx_sendTransaction", params })
              .then(function (result) {
                console.log(result, "resultresultresult");
              })
              .catch(function (error) {
                console.log(error, "errorerrorerrorerror");
              });
          } else {
            await Vue.prototype.connectedWallet();
          }
        };
      },
    };
    
    

    test.vue 调用数据

    // 调用读取合约
    const info = this.readContract("nftInfo", 9);
    const params = [
         11,
         "swor11119",
         "20221224001001",
         1,
         "1671850031",
         "A sword with white light",
         "11111",
     ];
    // 写入合约 - 返回结果
    const result = await this.writeContract("create", params);
    // 钱包之间的交易
    const content = await this.getTransaction (toAddress,value)
    

    相关文章

      网友评论

          本文标题:js-conflux-sdk封装函数

          本文链接:https://www.haomeiwen.com/subject/dwxacdtx.html