js-conflux-sdk封裝函數(shù)

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, // 測(cè)試網(wǎng)絡(luò)
  networkId: Number(process.env.VUE_APP_NETWORK_ID), // 正式1029
});
window.confluxJS = cfx;
// 創(chuàng)建要交互的合約對(duì)象,參數(shù)為合約的abi和地址
const contract = window.confluxJS.Contract({
  abi,
  address: process.env.VUE_APP_ADDRESS,
});
// create 賬戶
// 通過私鑰創(chuàng)建
const account = window.confluxJS.wallet.addPrivateKey(privateKey);
// 通過錢包創(chuàng)建
// const account = window.confluxJS.wallet.addRandom();
// 通過私鑰賬戶創(chuàng)建
// 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: "請(qǐng)安裝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()) {
        // 寫入合約  -- 寫入合約是異步的,不能及時(shí)獲取結(jié)果,還需處理
        const result = await contract[method](...params)
          .sendTransaction({
            from: account ,
          })
          .confirmed();
        return { type: "contract", content: result };
      } else {
        await Vue.prototype.connectedWallet();
      }
    };
    // 兩個(gè)錢包之間的交易
    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 調(diào)用數(shù)據(jù)

// 調(diào)用讀取合約
const info = this.readContract("nftInfo", 9);
const params = [
     11,
     "swor11119",
     "20221224001001",
     1,
     "1671850031",
     "A sword with white light",
     "11111",
 ];
// 寫入合約 - 返回結(jié)果
const result = await this.writeContract("create", params);
// 錢包之間的交易
const content = await this.getTransaction (toAddress,value)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容