convert-index.vue 6.5 KB

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