Skip to content

Commit ca44711

Browse files
committed
Add the account to watch to DynamoDB
1 parent 0b7d127 commit ca44711

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

cdk/functions/receiver.ts

+36-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import {
2+
DynamoDBClient,
3+
GetItemCommand,
4+
PutItemCommand,
5+
} from "@aws-sdk/client-dynamodb";
6+
import { unmarshall } from "@aws-sdk/util-dynamodb";
17
import axios from "axios";
28
import { getFromEnv } from "../lib/cdk-stack";
39

10+
const ddb = new DynamoDBClient({});
11+
412
export const handler = async (event: any) => {
513
const body = JSON.parse(event.body) as Body;
614
const chatId = body.message.chat.id;
715
const text = body.message.text;
16+
const user = body.message.chat.username;
817
console.log(text);
918

1019
const addressRegex = new RegExp(/^.*(0x[a-zA-Z0-9]{64}$)/g);
@@ -14,7 +23,7 @@ export const handler = async (event: any) => {
1423
await sendErrorMessagetoUser(chatId);
1524
} else {
1625
await sendConfirmationMessagetoUser(chatId, match[1]);
17-
await addAddresstoDb(match[1]);
26+
await addAddresstoDb(chatId, match[1], user);
1827

1928
const addressToWatch = match[1];
2029
console.log(addressToWatch);
@@ -28,16 +37,7 @@ export const handler = async (event: any) => {
2837
};
2938
};
3039

31-
interface Body {
32-
message: {
33-
chat: {
34-
id: string;
35-
};
36-
text: string;
37-
};
38-
}
39-
40-
async function sendErrorMessagetoUser(chatId: string) {
40+
async function sendErrorMessagetoUser(chatId: number) {
4141
const errorMessage =
4242
"Address not found. Please include your Starknet address following the command /watch";
4343
const BOT_API_KEY = getFromEnv("BOT_API_KEY");
@@ -46,7 +46,7 @@ async function sendErrorMessagetoUser(chatId: string) {
4646
console.log(res);
4747
}
4848

49-
async function sendConfirmationMessagetoUser(chatId: string, match: string) {
49+
async function sendConfirmationMessagetoUser(chatId: number, match: string) {
5050
const addressWatched = match;
5151
const confirmationMessage = `Watching address ${addressWatched} ...`;
5252
const BOT_API_KEY = getFromEnv("BOT_API_KEY");
@@ -55,7 +55,28 @@ async function sendConfirmationMessagetoUser(chatId: string, match: string) {
5555
console.log(res);
5656
}
5757

58-
async function addAddresstoDb(match: string) {
59-
const addressWatched = match;
60-
return;
58+
async function addAddresstoDb(chatId: number, addressWatched: string, username: string) {
59+
60+
const params = {
61+
TableName: "CdkStack-AccountsToWatch0A702DCE-Z6XSJDP9YT94",
62+
Item: {
63+
accountAddress: { S: addressWatched },
64+
chatId: { N: chatId.toString() },
65+
username: { S: username },
66+
},
67+
};
68+
69+
const res = await ddb.send(new PutItemCommand(params));
70+
71+
return null;
72+
}
73+
74+
interface Body {
75+
message: {
76+
chat: {
77+
id: number;
78+
username: string;
79+
};
80+
text: string;
81+
};
6182
}

cdk/lib/cdk-stack.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@ import * as apigateway from 'aws-cdk-lib/aws-apigateway';
1212

1313

1414
export class CdkStack extends cdk.Stack {
15-
table: cdk.aws_dynamodb.Table;
1615
fetchLambda: cdk.aws_lambda_nodejs.NodejsFunction;
1716
sendLambda: cdk.aws_lambda_nodejs.NodejsFunction;
1817
receiveLambda: cdk.aws_lambda_nodejs.NodejsFunction;
1918
api: cdk.aws_apigateway.RestApi;
19+
transactionStatusTable: cdk.aws_dynamodb.Table;
20+
accountTable: cdk.aws_dynamodb.Table;
2021

2122
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
2223
super(scope, id, props);
2324

24-
this.setupTable();
25+
this.setupTransactionTable();
26+
this.setupAccountTable();
2527
this.setupFetcher();
2628
this.setupSender();
2729
this.setupReceiver();
28-
this.table.grantFullAccess(this.fetchLambda);
2930
this.setupApiGateway();
31+
this.transactionStatusTable.grantFullAccess(this.fetchLambda);
32+
this.accountTable.grantFullAccess(this.receiveLambda);
33+
this.accountTable.grantFullAccess(this.fetchLambda);
3034
}
3135

3236
setupApiGateway() {
@@ -60,8 +64,8 @@ export class CdkStack extends cdk.Stack {
6064

6165

6266

63-
setupTable() {
64-
this.table = new Table(this, "TransactionStatus", {
67+
setupTransactionTable() {
68+
this.transactionStatusTable = new Table(this, "TransactionStatus", {
6569
partitionKey: {
6670
name: "transactionHash",
6771
type: AttributeType.STRING,
@@ -71,6 +75,17 @@ export class CdkStack extends cdk.Stack {
7175
});
7276
}
7377

78+
setupAccountTable() {
79+
this.accountTable = new Table(this, "AccountsToWatch", {
80+
partitionKey: {
81+
name: "accountAddress",
82+
type: AttributeType.STRING,
83+
},
84+
billingMode: BillingMode.PAY_PER_REQUEST,
85+
removalPolicy: RemovalPolicy.RETAIN,
86+
});
87+
}
88+
7489
setupFetcher() {
7590
this.fetchLambda = new NodejsFunction(this, "Fetcher", {
7691
timeout: Duration.seconds(40),

0 commit comments

Comments
 (0)