Skip to content

Commit

Permalink
Add XUMM login for link wallet (#3)
Browse files Browse the repository at this point in the history
* Add xumm login
* Shared function to process link wallet logic
* Bump version
* Remove address in command if xumm login enabled
  • Loading branch information
jacobpretorius authored Nov 13, 2022
1 parent 8e0cad4 commit 65eb243
Show file tree
Hide file tree
Showing 9 changed files with 750 additions and 129 deletions.
442 changes: 438 additions & 4 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "XRPL-Discord-Bot",
"version": "1.0.0",
"version": "1.1.0",
"description": "A customisable open-source Discord bot that brings the power of the XRPL to Discord communities.",
"repository": {
"type": "git",
Expand All @@ -26,13 +26,16 @@
"@types/node": "^16.11.9",
"applicationinsights": "^2.1.9",
"axios": "^0.24.0",
"body-parser": "^1.20.1",
"discord-api-types": "^0.24.0",
"discord.js": "^13.3.1",
"eventemitter3": "^4.0.7",
"express": "^4.17.1",
"mongodb": "^3.6.2",
"tslint": "^6.1.3",
"typescript": "^4.0.3",
"xrpl": "^2.0.2"
"xrpl": "^2.0.2",
"xrpl-txdata": "^1.2.1",
"xumm-sdk": "^1.3.9"
}
}
31 changes: 22 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ if (SETTINGS.APPLICATION_INSIGHTS.ENABLED) {
const LOGGER = appInsights?.defaultClient ?? null;

import express from 'express';
import bodyParser from 'body-parser';
import { Client, Intents, Message, Interaction } from 'discord.js';
import EventFactory from './events/EventFactory';
import { EventTypes } from './events/BotEvents';

import { scanLinkedWallets } from './business/scanLinkedWallets.js';
import { scanLinkedAccounts } from './business/scanLinkedAccounts.js';
import xummWebhook from './integration/xumm/webhook.js';

// Discord Client
const discordClient = new Client({
Expand All @@ -35,14 +36,16 @@ const commands = [
{
name: 'linkwallet',
description: 'Link your wallet to get server roles',
options: [
{
type: 3,
name: 'wallet-address',
description: 'Your XRP Wallet Address',
required: true,
},
],
options: SETTINGS.XUMM.ENABLED
? null
: [
{
type: 3,
name: 'wallet-address',
description: 'Your XRP Wallet Address',
required: true,
},
],
},
{
name: 'price',
Expand Down Expand Up @@ -131,6 +134,16 @@ webServer.get('/updateAccounts', async (req, res) => {
res.send(await scanLinkedAccounts(discordClient, LOGGER));
});

webServer.use('/xummWebhook', bodyParser.json());

webServer.post('/xummWebhook', async (req, res) => {
if (SETTINGS.XUMM.ENABLED) {
await xummWebhook(req.body, discordClient);
}

res.send({ status: 'OK' });
});

webServer.listen(SETTINGS.APP.PORT, async () => {
console.log(`Listening at http://localhost:${SETTINGS.APP.PORT}`);
});
114 changes: 114 additions & 0 deletions src/business/linkWalletToDiscordAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { getWalletHoldings } from '../integration/xrpl/getWalletHoldings.js';
import { updateUserWallet } from '../data/updateUserWallet.js';
import { updateUserRoles } from '../integration/discord/updateUserRoles.js';
import { Client, User } from 'discord.js';
import truncate from '../utils/truncate.js';
import { WalletUpdateResponse } from '../models/enum/WalletUpdateResponse.js';

const linkWalletToDiscordAccount = async (
walletAddress: string,
verified: boolean,
user: User,
client: Client,
LOGGER: any
): Promise<string> => {
if (!walletAddress) {
return null;
}

// Get holdings
let holdings = await getWalletHoldings(walletAddress, null);
if (holdings === -1) {
if (LOGGER !== null) {
LOGGER.trackEvent({
name: 'linkWallet-no-trustline',
properties: { walletAddress },
});
}

return `Seems like you don't have the project trustline yet, please retry once it has been added 👉 https://xrpscan.com/account/${walletAddress}`;
}

// Allow them to set it even with network error
let hadError = false;
if (holdings === null) {
hadError = true;
holdings = 0;
}

const newWallet: IWallet = {
address: walletAddress,
points: holdings,
verified,
};

const newUser: IBotUser = {
discordId: user.id,
discordUsername: user.username,
discordDiscriminator: user.discriminator,
previousDiscordUsername: '',
previousDiscordDiscriminator: '',
totalPoints: holdings,
wallets: [],
};

// Save in Mongo
const mongoUpdateResult = await updateUserWallet(newUser, newWallet, false);

// The wallet has been claimed before, needs to be set by admin
if (mongoUpdateResult === WalletUpdateResponse.ErrorAddressAlreadyClaimed) {
if (LOGGER !== null) {
LOGGER.trackEvent({
name: 'linkWallet-claimed-by-another-user',
properties: {
walletAddress,
activeUserId: user.id,
activeUserName: user.username,
},
});
}

return `This address has been claimed before, if it wasn't done by you please message a mod with ownership proof to claim it`;
}

// If the user has set too many addresses an admin has to do it
if (mongoUpdateResult === WalletUpdateResponse.ErrorTooManyAccountClaims) {
if (LOGGER !== null) {
LOGGER.trackEvent({
name: 'linkWallet-too-many-claimed',
properties: {
walletAddress,
activeUserId: user.id,
activeUserName: user.username,
},
});
}
return `You seem to have claimed too many addresses, please message a mod with ownership proof to claim more`;
}

// Set role
await updateUserRoles(0, holdings, user.id, client, LOGGER, false);

if (LOGGER !== null) {
LOGGER.trackEvent({
name: 'linkWallet-success',
properties: {
walletAddress,
activeUserId: user.id,
activeUserName: user.username,
},
});
}

// Send confirmation to the user
if (hadError) {
return `Wallet linked! There was an error trying to get your holdings from the XRPL network. Your role will be updated automatically once the network is working. You do not need to do anything else.`;
}

return `Found your ${truncate(
holdings,
2
)} points! Updated server roles set 🚀`;
};

export { linkWalletToDiscordAccount };
2 changes: 1 addition & 1 deletion src/commands/AdminLinkWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const adminLinkWallet = async (message: Message, client: Client) => {
const newWallet: IWallet = {
address: walletAddress,
points: holdings,
verified: false, // todo when we have XUMM integration
verified: false,
};

const newUser: IBotUser = {
Expand Down
Loading

0 comments on commit 65eb243

Please sign in to comment.