From b8e2467ceb31fbd2e15027f38fd6ce8b465cb436 Mon Sep 17 00:00:00 2001 From: Juhyung Park Date: Thu, 23 Aug 2018 15:05:44 +0900 Subject: [PATCH] test --- logic/index.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ routes/api.ts | 27 +++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/logic/index.ts b/logic/index.ts index afb1cac..95e5fe1 100644 --- a/logic/index.ts +++ b/logic/index.ts @@ -64,6 +64,57 @@ export async function giveCCC( } } +export async function giveCCCTest( + context: Context, + to: string, + amount: string +): Promise { + try { + const sdk = context.codechainSDK; + + let toAddress; + try { + toAddress = sdk.key.classes.PlatformAddress.fromString(to); + } catch (err) { + throw new FaucetError(ErrorCode.InvalidAddress, err); + } + + let nonce = context.nonce; + let result; + + try { + result = await giveCCCInternal( + context, + toAddress, + amount, + context.nonce + ); + } catch (err) { + console.warn( + `Error from codechain ${err.toString()}, ${JSON.stringify(err)}` + ); + console.warn("Retry with refreshed nonce"); + + nonce = (await sdk.rpc.chain.getNonce( + context.config.faucetCodeChainAddress + )) as U256; + + result = await giveCCCInternal(context, toAddress, amount, nonce); + } + + await historyModel.insert(context, to); + context.nonce = nonce.increase(); + + return result; + } catch (err) { + if (err.name !== "FaucetError") { + throw new FaucetError(ErrorCode.Unknown, err); + } else { + throw err; + } + } +} + async function giveCCCInternal( context: Context, toAddress: PlatformAddress, diff --git a/routes/api.ts b/routes/api.ts index 92ece75..b84619b 100644 --- a/routes/api.ts +++ b/routes/api.ts @@ -29,7 +29,7 @@ export function createRouter(context: Context) { ) { console.log( `marketingText: ${ - context.config.marketingText + context.config.marketingText }\n content: ${content}` ); throw new FaucetError(ErrorCode.NoMarketingText, null); @@ -61,5 +61,30 @@ export function createRouter(context: Context) { } }); + router.post("/requestMoneySeulgi", async (req, res) => { + console.log(`req body is ${JSON.stringify(req.body)}`); + const { to } = req.body; + + const amount = String(1000 * 1000 * 1000); + try { + if (to === null) { + throw new FaucetError(ErrorCode.InvalidAddress, null); + } + + const hash = await giveCCC(context, to, amount); + res.json({ + success: true, + hash, + message: successMessage(context, hash.toEncodeObject()) + }); + } catch (err) { + res.json({ + success: false, + err, + message: errorMessage(context, err) + }); + } + }); + return router; }