@@ -10,6 +10,7 @@ import io.ktor.http.content.*
1010import io.ktor.request.receiveParameters
1111import io.ktor.response.*
1212import io.ktor.routing.*
13+ import kotlinx.coroutines.flow.channelFlow
1314import kotlinx.html.*
1415import org.kethereum.ETH_IN_WEI
1516import org.kethereum.crypto.toAddress
@@ -18,19 +19,15 @@ import org.kethereum.ens.isPotentialENSDomain
1819import org.kethereum.erc55.isValid
1920import org.kethereum.model.*
2021import org.komputing.fauceth.FaucethLogLevel.*
21- import org.komputing.fauceth.util.AtomicNonce
2222import org.komputing.fauceth.util.log
2323import java.math.BigDecimal
24+ import java.math.BigInteger
2425
2526fun main (args : Array <String >) = io.ktor.server.netty.EngineMain .main(args)
2627
2728fun Application.module () {
2829
29- val initialNonce = rpc.getTransactionCount(config.keyPair.toAddress())
30-
31- log(INFO , " Got initial nonce: $initialNonce for address ${config.keyPair.toAddress()} " )
32-
33- val atomicNonce = AtomicNonce (initialNonce!! )
30+ if (config.chains.size != chains.size) fail(" Could not find definitions for all chains" )
3431
3532 routing {
3633 static(" /static" ) {
@@ -39,7 +36,9 @@ fun Application.module() {
3936 }
4037 get(" /" ) {
4138 val address = call.request.queryParameters[ADDRESS_KEY ]
42-
39+ val requestedChain = call.request.queryParameters[CHAIN_KEY ]?.toLongOrNull().let { chainId ->
40+ chains.firstOrNull { it.staticChainInfo.chainId == chainId }
41+ }
4342 call.respondHtml {
4443 head {
4544 title { + config.appTitle }
@@ -82,6 +81,25 @@ fun Application.module() {
8281 img(src = url, classes = " image" )
8382 }
8483 }
84+
85+ if (chains.size > 1 && requestedChain == null ) {
86+ select {
87+ name = " chain"
88+ chains.forEach { chain ->
89+ option {
90+ value = chain.staticChainInfo.chainId.toString()
91+ + chain.staticChainInfo.name
92+ }
93+ }
94+ }
95+ } else {
96+ hiddenInput {
97+ name = " chain"
98+ value = (requestedChain ? : chains.first()).staticChainInfo.chainId.toString()
99+ }
100+ }
101+
102+ br
85103 input(classes = " input" ) {
86104 name = ADDRESS_KEY
87105 value = address ? : " "
@@ -90,11 +108,11 @@ fun Application.module() {
90108 div(classes = " h-captcha center" ) {
91109 attributes[" data-sitekey" ] = config.hcaptchaSiteKey
92110 }
93- }
94- div (classes = " center " ) {
95- button(classes = " button " ) {
96- onClick = " submitForm() "
97- + " Request funds "
111+ div(classes = " center " ) {
112+ button (classes = " button " ) {
113+ onClick = " submitForm() "
114+ + " Request funds "
115+ }
98116 }
99117 }
100118 }
@@ -111,17 +129,23 @@ fun Application.module() {
111129 }
112130 + config.keyPair.toAddress().toString()
113131 br
114- b {
115- + " Nonce: "
132+ chains.forEach {
133+
134+ p {
135+ + " Chain: ${it.staticChainInfo.name} "
136+ }
137+ b {
138+ + " Nonce: "
139+ }
140+ + it.nonce.get().toString()
116141 }
117- + atomicNonce.get().toString()
118142 }
119-
120143 }
121144 }
122145 post(" /request" ) {
123146 val receiveParameters = call.receiveParameters()
124147 log(VERBOSE , " Serving /request with parameters $receiveParameters " )
148+
125149 val captchaResult: Boolean = receiveParameters[" h-captcha-response" ]?.let { captchaVerifier.verifyCaptcha(it) } ? : false
126150 var address = Address (receiveParameters[ADDRESS_KEY ] ? : " " )
127151 val ensName = receiveParameters[ADDRESS_KEY ]?.let { name -> ENSName (name) }
@@ -146,15 +170,18 @@ fun Application.module() {
146170 call.respondText(""" Swal.fire("Error", "Could not verify your humanity", "error");""" )
147171 } else {
148172
149- val txHash: String = sendTransaction(address, atomicNonce)
173+ val chain = chains.findLast { it.staticChainInfo.chainId == receiveParameters[" chain" ]?.toLong() }!!
174+ val txHash: String = sendTransaction(address, chain)
150175
151176 val amountString = BigDecimal (config.amount).divide(BigDecimal (ETH_IN_WEI ))
152- val msg = if (config.chainExplorer != null ) {
153- " send $amountString ETH (<a href='${config.chainExplorer} /tx/$txHash '>view here</a>)"
177+ val explorer = chain.staticChainInfo.explorers?.firstOrNull()?.url
178+ val msg = if (explorer != null ) {
179+ " send $amountString ETH (<a href='$explorer /tx/$txHash '>view here</a>)"
154180 } else {
155181 " send $amountString ETH (transaction: $txHash )"
156182 }
157183 call.respondText(""" Swal.fire("Transaction send", "$msg ", "success");""" )
184+
158185 }
159186 }
160187 }
0 commit comments