|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <h1>Send LIKE</h1> |
| 4 | + <div v-if="error" style="color: red"> |
| 5 | + {{ error }} |
| 6 | + </div> |
| 7 | + <div v-if="isLoading" style="color: green"> |
| 8 | + Loading... |
| 9 | + </div> |
| 10 | + <div>Steps {{ step }} / 2</div> |
| 11 | + <hr> |
| 12 | + <section v-if="step === 1"> |
| 13 | + <h2>1. Send LIKE</h2> |
| 14 | + <div> |
| 15 | + <p><label>Enter default memo (optional)</label></p> |
| 16 | + <input v-model="defaultMemo" placeholder="default memo"> |
| 17 | + <p> |
| 18 | + <label>Upload LIKE CSV ( |
| 19 | + <a |
| 20 | + href="https://github.com/likecoin/iscn-nft-tools/blob/master/send-like/list_example.csv" |
| 21 | + target="_blank" |
| 22 | + > |
| 23 | + list.csv |
| 24 | + </a>) file: </label> |
| 25 | + </p> |
| 26 | + <div v-if="likeSendListData?.length"> |
| 27 | + <pre>Number of entries in CSV:{{ likeSendListData.length }}</pre> |
| 28 | + <pre>Total amount:{{ totalAmount }}</pre> |
| 29 | + <table> |
| 30 | + <thead> |
| 31 | + <tr><td>Address</td><td>LIKE</td></tr> |
| 32 | + </thead> |
| 33 | + <tbody> |
| 34 | + <tr v-for="entry in likeSendListData" :key="entry.address"> |
| 35 | + <td>{{ entry.address }}</td><td>{{ entry.LIKE }}</td> |
| 36 | + </tr> |
| 37 | + </tbody> |
| 38 | + </table> |
| 39 | + </div> |
| 40 | + <input type="file" @change="onSendLIKEFileChange"> |
| 41 | + <br> |
| 42 | + <button :disabled="isLoading" style="margin-top: 16px" @click="onSendNFTStart"> |
| 43 | + Send |
| 44 | + </button> |
| 45 | + </div> |
| 46 | + </section> |
| 47 | + |
| 48 | + <section v-if="step > 1"> |
| 49 | + Result: {{ sendResultTxHash }} |
| 50 | + </section> |
| 51 | + </div> |
| 52 | +</template> |
| 53 | + |
| 54 | +<script setup lang="ts"> |
| 55 | +import { storeToRefs } from 'pinia' |
| 56 | +import { parse } from 'csv-parse/sync' |
| 57 | +import { MsgSend } from 'cosmjs-types/cosmos/bank/v1beta1/tx.js' |
| 58 | +import { stringify } from 'csv-stringify/sync' |
| 59 | +
|
| 60 | +import { BigNumber } from 'bignumber.js' |
| 61 | +import { useWalletStore } from '~/stores/wallet' |
| 62 | +import { DENOM } from '~/constant' |
| 63 | +
|
| 64 | +const store = useWalletStore() |
| 65 | +const { wallet, signer } = storeToRefs(store) |
| 66 | +const { connect } = store |
| 67 | +
|
| 68 | +const step = ref(1) |
| 69 | +const error = ref('') |
| 70 | +const isLoading = ref(false) |
| 71 | +
|
| 72 | +const defaultMemo = ref('') |
| 73 | +const likeSendListData = ref<any>([]) |
| 74 | +const sendResultTxHash = ref('') |
| 75 | +
|
| 76 | +const totalAmount = computed(() => { |
| 77 | + return likeSendListData.value.reduce((total: any, item: any) => { |
| 78 | + return total + Number(item.LIKE) |
| 79 | + }, 0) |
| 80 | +}) |
| 81 | +
|
| 82 | +watch(isLoading, (newIsLoading) => { |
| 83 | + if (newIsLoading) { error.value = '' } |
| 84 | +}) |
| 85 | +
|
| 86 | +async function onSendNFTStart () { |
| 87 | + try { |
| 88 | + isLoading.value = true |
| 89 | + if (!wallet.value || !signer.value) { |
| 90 | + await connect() |
| 91 | + } |
| 92 | + if (!wallet.value || !signer.value) { return } |
| 93 | + if (!likeSendListData.value.length) { throw new Error('NFT data not exists') } |
| 94 | + const signingClient = await getSigningClientWithSigner(signer.value) |
| 95 | + const client = signingClient.getSigningStargateClient() |
| 96 | + if (!client) { throw new Error('Signing client not exists') } |
| 97 | +
|
| 98 | + step.value += 1 |
| 99 | + const msgAnyArray = [] |
| 100 | + for (let i = 0; i < likeSendListData.value.length; i += 1) { |
| 101 | + const e = likeSendListData.value[i] |
| 102 | + const msgSend = MsgSend.fromPartial({ |
| 103 | + fromAddress: wallet.value, |
| 104 | + toAddress: e.address, |
| 105 | + amount: [{ |
| 106 | + denom: DENOM, |
| 107 | + amount: `${new BigNumber(e.LIKE).shiftedBy(9).toFixed(0)}` |
| 108 | + }] |
| 109 | + }) |
| 110 | + msgAnyArray.push(msgSend) |
| 111 | + } |
| 112 | +
|
| 113 | + if (msgAnyArray.length) { |
| 114 | + const result = await client.signAndBroadcast( |
| 115 | + wallet.value, |
| 116 | + msgAnyArray.map(e => ({ |
| 117 | + typeUrl: '/cosmos.bank.v1beta1.MsgSend', |
| 118 | + value: e |
| 119 | + })), |
| 120 | + getGasFee(likeSendListData.value.length), |
| 121 | + defaultMemo.value |
| 122 | + ) |
| 123 | + sendResultTxHash.value = result.transactionHash |
| 124 | + } |
| 125 | + } catch (err) { |
| 126 | + console.error(err) |
| 127 | + error.value = (err as Error).toString() |
| 128 | + } finally { |
| 129 | + isLoading.value = false |
| 130 | + } |
| 131 | +} |
| 132 | +
|
| 133 | +function onSendLIKEFileChange (event: Event) { |
| 134 | + if (!event?.target) { return } |
| 135 | + const files = (event.target as HTMLInputElement)?.files |
| 136 | + if (!files) { return } |
| 137 | + const [file] = files |
| 138 | + const reader = new FileReader() |
| 139 | + reader.onload = (e) => { |
| 140 | + try { |
| 141 | + const text = e.target?.result |
| 142 | + if (typeof text !== 'string') { return } |
| 143 | + const data = parse(text, { columns: true }) |
| 144 | + likeSendListData.value = data |
| 145 | + sendResultTxHash.value = '' |
| 146 | + } catch (err) { |
| 147 | + console.error(err) |
| 148 | + error.value = (err as Error).toString() |
| 149 | + } |
| 150 | + } |
| 151 | + reader.readAsText(file) |
| 152 | +} |
| 153 | +
|
| 154 | +</script> |
0 commit comments