@multiversx/sdk-dapp-swap
Version:
A library to hold the main logic for swapping between tokens on the MultiversX blockchain
3 lines (2 loc) • 2.69 kB
JavaScript
import{a as C}from"./chunk-Y4ZHUZ6C.js";import{a as c}from"./chunk-AZN5ORWR.js";import{a as s}from"./chunk-WVDNULNU.js";import{a as d}from"./chunk-P5DITO6J.js";import{f as A}from"./chunk-5W737WN4.js";A();import e,{useEffect as S}from"react";import{useFormik as J}from"formik";var ae=({tokens:i,swapConfig:r,firstToken:n,firstAmount:m,secondToken:o,secondAmount:u,activeRoute:l,handleOnChangeFirstAmount:w,handleOnChangeSecondAmount:B,handleOnChangeFirstSelect:E,handleOnChangeSecondSelect:V,handleOnFirstMaxBtnChange:T,handleSwitchTokens:x,handleOnSubmit:h})=>{var O,F;let I={firstAmount:"",firstToken:n,secondAmount:"",secondToken:o,activeRoute:l},M=C({firstToken:n,secondToken:o,minAcceptedAmount:r==null?void 0:r.minSwapAmount}),N=()=>{h&&h()},{handleSubmit:H,handleBlur:p,handleChange:v,setFieldValue:a,setTouched:L,errors:b,touched:y}=J({onSubmit:N,initialValues:I,validationSchema:M}),f=i==null?void 0:i.map(t=>({label:t.identifier,value:t.identifier,token:t})),g=()=>{L({},!1),a("firstAmount",""),a("secondAmount",""),a("activeRoute",void 0)},U=t=>{a("firstToken",t),g(),E(t)},D=t=>{a("secondToken",t),g(),V(t)},j=t=>{v(t),w(t.currentTarget.value)},q=t=>{v(t),B(t.currentTarget.value)},z=n!=null&&n.token?d({amount:(O=n.token.balance)!=null?O:"0",decimals:n==null?void 0:n.token.decimals}):0,G=o!=null&&o.token?d({amount:(F=o.token.balance)!=null?F:"0",decimals:o==null?void 0:o.token.decimals}):0;return S(()=>{a("firstAmount",m,!0)},[m]),S(()=>{a("secondAmount",u,!0)},[u]),S(()=>{a("activeRoute",l,!0)},[l]),e.createElement("form",{className:"dapp-core-swap-form",noValidate:!0,onSubmit:H},e.createElement("div",null,e.createElement(s,{id:"firstToken",name:"firstToken",value:n,isSearchable:!1,options:f,onChange:U,onBlur:p,disabledOption:o}),e.createElement("div",null,"Balance: ",z),e.createElement("input",{type:"number",min:0,step:"0.000001",name:"firstAmount",value:m,onChange:j,onBlur:p}),e.createElement(c,{fieldName:"firstAmount",errors:b,touched:y}),e.createElement("button",{type:"button",onClick:T},"max")),e.createElement("br",null),e.createElement("button",{type:"button",onClick:x}," ","> reverse <"," "),e.createElement("br",null),e.createElement("br",null),e.createElement("div",null,e.createElement(s,{id:"secondToken",name:"secondToken",value:o,isSearchable:!1,options:f,onChange:D,disabledOption:n}),e.createElement("div",null,"Balance: ",G),e.createElement("input",{type:"number",min:0,step:"0.000001",name:"secondAmount",value:u,onChange:q,onBlur:p}),e.createElement(c,{fieldName:"secondAmount",errors:b,touched:y})),e.createElement("br",null),e.createElement("button",{type:"submit"},"Swap"))};export{ae as a};
//# sourceMappingURL=chunk-6CCEOGRB.js.map