Skip to content

Commit

Permalink
Merge branch 'master' into caspian-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlei committed Feb 13, 2024
2 parents 98f6a58 + 3452116 commit d8ce31d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 53 deletions.
11 changes: 10 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ Funds new Testnet accounts

## Usage

### Run the server:
### Run the server (example):

#### command line
```
npm install
NODE_ENV="production" PORT=3000 RIPPLED_URI="wss://s.altnet.rippletest.net:51233" FUNDING_ADDRESS=rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe FUNDING_SECRET=<secret> XRP_AMOUNT=10000
npm start
```

#### using env file
or fill out `.env` file using `.env.example` and run
```
npm install
npm start
```

### Fund a new account:

```
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 19 additions & 49 deletions src/routes/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { getDestinationAccount } from "../destination-wallet";
import { Client, Payment, Wallet, xrpToDrops } from "xrpl";
import { Account, FundedResponse } from "../types";
import { fundingWallet } from "../wallet";
import { BigQuery } from "@google-cloud/bigquery";
import { config } from "../config";
import { getTicket } from "../ticket-queue";
import rTracer from "cls-rtracer";
import { incrementTxRequestCount, incrementTxCount } from "../index";
import { insertIntoCaspian, insertIntoBigQuery } from "../logging";

export default async function (req: Request, res: Response) {
incrementTxRequestCount();
Expand Down Expand Up @@ -75,7 +75,11 @@ export default async function (req: Request, res: Response) {
try {
result = await submitPaymentWithTicket(payment, client, fundingWallet);
transactionHash = result.hash;
transactionHash = result.hash;
} catch (err) {
console.log(
`${rTracer.id()} | Failed to submit payment ${transactionHash}: ${err}`
);
console.log(
`${rTracer.id()} | Failed to submit payment ${transactionHash}: ${err}`
);
Expand Down Expand Up @@ -104,7 +108,19 @@ export default async function (req: Request, res: Response) {
account.address
} with ${amount} XRP (${status}), paymentHash: ${transactionHash}`
);

if (config.CASPIAN_API_KEY) {
try {
await insertIntoCaspian(
account,
Number(amount),
req.body,
client.networkID
);
console.log("Data sent to Caspian successfully");
} catch (error) {
console.warn("Caspian Insertion Error:", error);
}
}
if (config.BIGQUERY_PROJECT_ID) {
try {
await insertIntoBigQuery(account, amount, req.body);
Expand All @@ -113,7 +129,6 @@ export default async function (req: Request, res: Response) {
console.warn(`Failed to insert into BigQuery: ${error}`);
}
}

incrementTxCount();
res.send(response);
}
Expand All @@ -126,51 +141,6 @@ export default async function (req: Request, res: Response) {
}
}

async function insertIntoBigQuery(
account: Account,
amount: string,
reqBody: any
): Promise<void> {
const { userAgent = "", usageContext = "" } = reqBody;
const memos = reqBody.memos
? reqBody.memos.map((memo: any) => ({ memo }))
: [];
const rows = [
{
user_agent: userAgent,
usage_context: usageContext,
memos: memos,
account: account.xAddress,
amount: amount,
},
];
const bigquery = new BigQuery({
projectId: config.BIGQUERY_PROJECT_ID,
credentials: {
client_email: config.BIGQUERY_CLIENT_EMAIL,
private_key: config.BIGQUERY_PRIVATE_KEY,
},
});

return new Promise((resolve, reject) => {
bigquery
.dataset(config.BIGQUERY_DATASET_ID)
.table(config.BIGQUERY_TABLE_ID)
.insert(rows, (error) => {
if (error) {
console.warn(
"WARNING: Failed to insert into BigQuery",
JSON.stringify(error, null, 2)
);
reject(error);
} else {
console.log(`Inserted ${rows.length} rows`);
resolve();
}
});
});
}

async function submitPaymentWithTicket(
payment: Payment,
client: Client,
Expand Down Expand Up @@ -206,4 +176,4 @@ async function submitPaymentWithTicket(
}

return { result, hash };
}
}
4 changes: 3 additions & 1 deletion src/ticket-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export async function getTicket(client: Client) {
})
.catch((error) => {
console.log(
`Failed to create tickets. Error: ${JSON.stringify(error)} Message: ${error?.message}`
`Failed to create tickets. Error: ${JSON.stringify(error)} Message: ${
error?.message
}`
);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export interface Account {
export interface FundedResponse {
account: Account;
amount: number;
transactionHash: string;
seed?: string;
paymentHash: string;
}

0 comments on commit d8ce31d

Please sign in to comment.