convert-index.vue 6.7 KB

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