convert-index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <script>
  2. import tokenpocketBnb from "@/common/wallet/tokenpocket-wallet/tokenpocket-bnb";
  3. import {
  4. getTotalMoney
  5. } from "@/api/money";
  6. import {
  7. addDeal,
  8. getAddress
  9. } from "@/api/deal";
  10. import tools from "@/common/js/tools";
  11. import web3 from "web3";
  12. import {
  13. ethers
  14. } from "ethers";
  15. export default {
  16. name: "convert-index",
  17. components: {},
  18. data() {
  19. return {
  20. usdtNum: '',
  21. iconNum: '',
  22. type: 1,
  23. toNum: '',
  24. formNUm: '',
  25. price: '',
  26. address: '',
  27. dealAddress: '',
  28. change_num: 0,
  29. };
  30. },
  31. watch: {
  32. 'formNUm': function() {
  33. if (this.formNUm === '') {
  34. this.toNum = ''
  35. } else {
  36. if (this.type === 1) {
  37. this.toNum = this.formNUm * this.price
  38. } else {
  39. this.toNum = (this.formNUm / this.price).toFixed(6)
  40. }
  41. }
  42. }
  43. },
  44. mounted() {
  45. this.getIconNum()
  46. this.getTotalMoney()
  47. this.getAddress()
  48. },
  49. methods: {
  50. getAddress() {
  51. getAddress().then(res => {
  52. if (res.code === 1) {
  53. this.dealAddress = res.data.address
  54. this.change_num = res.data.change_num
  55. }
  56. })
  57. },
  58. async addDeal() {
  59. if (this.toNum === '' || this.formNUm === '') {
  60. tools.error("兑换数量异常")
  61. return
  62. }
  63. let data = ''
  64. let transactionData = ''
  65. let formHex = web3.utils.numberToHex(ethers.utils.parseEther(this.formNUm.toString()).toString());
  66. if (this.type === 1) {
  67. if (this.change_num > 10) {
  68. tools.error('交易已锁定')
  69. return
  70. }
  71. data = '0xa9059cbb'
  72. data = data + tools.replenishZero(this.dealAddress) + tools.replenishZero(formHex)
  73. transactionData = await tokenpocketBnb.getContractTransaction(data, 1);
  74. } else {
  75. data = '0xa9059cbb'
  76. // data='0x7472616e73666572'
  77. data = data + tools.replenishZero(this.dealAddress) + tools.replenishZero(formHex)
  78. transactionData = await tokenpocketBnb.getContractTransaction(data, 0);
  79. }
  80. console.log(transactionData)
  81. // data='0x5e9a0c48000000000000000000000000b7f1c21c51a0f85704a13b981aa9075d9b102962';
  82. tokenpocketBnb.sendTransaction(transactionData).then(res => {
  83. console.log(res)
  84. if (res) {
  85. addDeal({
  86. 'type': this.type,
  87. 'money': this.toNum,
  88. 'hash': res
  89. }).then(res => {
  90. if (res.code === 1) {
  91. tools.success(res.msg)
  92. this.toNum = ''
  93. this.getTotalMoney()
  94. this.getIconNum()
  95. } else {
  96. tools.error(res.msg)
  97. }
  98. })
  99. }
  100. })
  101. },
  102. setMax() {
  103. if (this.type === 1) {
  104. this.formNUm = this.usdtNum
  105. } else {
  106. this.formNUm = this.iconNum
  107. }
  108. },
  109. getTotalMoney() {
  110. getTotalMoney().then(res => {
  111. if (res.code === 1) {
  112. this.price = res.data.price
  113. }
  114. })
  115. },
  116. async getIconNum() {
  117. this.address = tokenpocketBnb.getMyAddress()
  118. if (this.address === '') {
  119. return
  120. }
  121. setTimeout(async () => {
  122. this.iconNum = await tokenpocketBnb.getTokenBalance(this.address, 0)
  123. this.usdtNum = await tokenpocketBnb.getTokenBalance(this.address, 1)
  124. }, 100)
  125. },
  126. setType() {
  127. this.type = (this.type === 1 ? 2 : 1)
  128. this.formNUm = ''
  129. }
  130. },
  131. }
  132. </script>
  133. <template>
  134. <view class="flex-common-box p-20 box-sizing-border">
  135. <view class="bgc-f p-40 box-sizing-border b-rad-20">
  136. <view>
  137. <view class="bgc-F6F7FA b-rad-20 mb-20 p-20 box-sizing-border mb-20" v-if="type===1">
  138. <view class="align-items-start flex-justify-space mb-20">
  139. <view class="align-items-center">
  140. <image class="t-img mr-8" :src="require('@/static/img/index/index/usdt.png')"></image>
  141. <view class="fs-28">USDT</view>
  142. </view>
  143. <view class="max-box b-rad-20 fs-28" @click="setMax">
  144. Max {{usdtNum}}
  145. </view>
  146. </view>
  147. <view>
  148. <input v-model="formNUm" placeholder="输入要兑换的USDT数量" placeholder-class="fs-28"></input>
  149. </view>
  150. </view>
  151. <view class="bgc-F6F7FA b-rad-20 mb-20 p-20 box-sizing-border mb-20" v-else>
  152. <view class="align-items-start flex-justify-space mb-20">
  153. <view class="align-items-center">
  154. <image class="t-img mr-8" :src="require('@/static/img/logo/logo.png')"></image>
  155. <view class="fs-28">DAO</view>
  156. </view>
  157. <view class="max-box b-rad-20 fs-28" @click="setMax">
  158. Max {{iconNum}}
  159. </view>
  160. </view>
  161. <view>
  162. <input v-model="formNUm" placeholder="输入要兑换的DAO数量" placeholder-class="fs-28"></input>
  163. </view>
  164. </view>
  165. </view>
  166. <view class="flex-direction-column align-items-center flex-justify-center mb-20" @click="setType">
  167. <view class="fs-28">
  168. FORM
  169. </view>
  170. <image class="jh-img" :src="require('@/static/img/index/index/jiaohuan.png')"></image>
  171. <view class="fs-28">
  172. TO
  173. </view>
  174. </view>
  175. <view>
  176. <view class="bgc-F6F7FA b-rad-20 mb-20 p-20 box-sizing-border mb-50" v-if="type===1">
  177. <view class="align-items-start flex-justify-space mb-20">
  178. <view class="align-items-center">
  179. <image class="logo-img mr-8" :src="require('@/static/img/logo/logo.png')"></image>
  180. <view class="fs-28">DAO</view>
  181. </view>
  182. </view>
  183. <view class="fs-28" :class="toNum?'':'fc-808080'">
  184. {{toNum?toNum:'请输入需要兑换的USDT数量'}}
  185. </view>
  186. </view>
  187. <view class="bgc-F6F7FA b-rad-20 mb-20 p-20 box-sizing-border mb-50" v-else>
  188. <view class="align-items-start flex-justify-space mb-20">
  189. <view class="align-items-center">
  190. <image class="logo-img mr-8" :src="require('@/static/img/index/index/usdt.png')"></image>
  191. <view class="fs-28">USDT</view>
  192. </view>
  193. </view>
  194. <view class="fs-28" :class="toNum?'':'fc-808080'">
  195. {{toNum?toNum:'请输入需要兑换的DAO数量'}}
  196. </view>
  197. </view>
  198. </view>
  199. <view class="convent-but b-rad-20 text-align-center fs-28 fc-f" @click="addDeal">
  200. 兑换
  201. </view>
  202. </view>
  203. </view>
  204. </template>
  205. <style scoped lang="scss">
  206. .flex-common-box {
  207. width: 100%;
  208. height: calc(100vh - 138rpx);
  209. display: flex;
  210. flex-direction: column;
  211. }
  212. .t-img {
  213. width: 70rpx;
  214. height: 60rpx;
  215. }
  216. .max-box {
  217. color: #0d81cf;
  218. border: 2rpx solid #0d81cf;
  219. padding: 6rpx 20rpx;
  220. }
  221. ::v-deep .el-input__inner {
  222. background-color: transparent;
  223. border: none;
  224. padding: 0;
  225. }
  226. .jh-img {
  227. width: 60rpx;
  228. height: 60rpx;
  229. }
  230. .logo-img {
  231. width: 66rpx;
  232. height: 50rpx;
  233. }
  234. .convent-but {
  235. height: 80rpx;
  236. background: #0d81cf;
  237. color: #fff;
  238. line-height: 80rpx;
  239. }
  240. </style>