Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turing-staging faucet #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,780 changes: 1,235 additions & 1,545 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
"author": "",
"license": "ISC",
"dependencies": {
"@polkadot/api": "^7.11.1",
"@polkadot/api": "^9.5.1",
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"bs58": "^4.0.1",
"crypto": "^1.0.1",
"discord.js": "^13.5.0",
"discord.js": "^14.6.0",
"express": "^4.17.1",
"lodash": "^4.17.21",
"matrix-js-sdk": "^12.1.0",
"moment": "^2.29.3",
"nedb": "^1.8.0"
}
}
71 changes: 3 additions & 68 deletions src/bot/actions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
const _ = require('lodash');
const moment = require('moment');
const pdKeyring = require('@polkadot/keyring');
const axios = require('axios');

const { DRIP_TYPE, SS58_PREFIX } = require("../constants");
const { tokenSymbol, networkName, dripActions: { later: { maxScheduleSeconds } } } = require('./config');
const { getNextHourStr, parseTime } = require('./helperFn');
const { dripActions } = require("./config");
const { tokenSymbol, networkName, dripActions } = require('./config');

const keyring = new pdKeyring.Keyring({ type: 'sr25519' });
keyring.setSS58Format(SS58_PREFIX);
Expand Down Expand Up @@ -58,69 +55,7 @@ const drip = async (sender, address) => {

const { amount } = dripActions[DRIP_TYPE.NORMAL];

return `I just sent ${amount} ${tokenSymbol} to address ${address}. Extrinsic hash: ${res.data.hash}.`;
return `I just sent ${amount} ${tokenSymbol} to ${address}. Extrinsic hash: ${res.data.hash}.\n\nTry out our recurring payment feature on https://ace.web3go.xyz ! (Click on Connect at the top right corner to select Turing Staging network)`;
}

const dripLater = async (sender, address, time) => {
if (_.isEmpty(address)) {
return 'please enter a wallet address after !drip-later.';
}

if (!isValidAddress(address)) {
return `The address ${address} entered is incompatible to ${networkName}.`;
}

const dripTime = parseTime(time);
if (!dripTime) {
return `Please enter the specified time format(UTC). Example: !drip-later address ${getNextHourStr()}`;
}
if (dripTime.isBefore(moment())) {
return "The time in UTC must be in the future.";
}
if (dripTime.isAfter(moment().add(maxScheduleSeconds, 'seconds'))) {
return `The time in UTC cannot be farther than ${moment.duration(maxScheduleSeconds, 'seconds').asDays()} days.`;
}

const res = await requestDrip({
sender,
address,
dripType: DRIP_TYPE.LATER,
dripTime: dripTime.valueOf(),
});

if (res.data === 'LIMIT') {
return `Your Discord ID or the address has reached its daily quota. Please request 3 times every 24 hours.`;
}

const { amount } = dripActions[DRIP_TYPE.LATER];
const { data: { hash, providerId } } = res;

return `I will send ${amount} NEU to address ${address} at ${time} UTC. Extrinsic hash: ${hash}. Your provided_id: ${providerId}.`;
}

const dripSwag = async (sender, address) => {
if (_.isEmpty(address)) {
return 'please enter a wallet address after !drip-swag.';
}

if (!isValidAddress(address)) {
return `The address ${address} entered is incompatible to ${networkName}.`;
}

const res = await requestDrip({
sender,
address,
dripType: DRIP_TYPE.SWAG,
});

if (res.data === 'LIMIT') {
return `Your Discord ID or the address has reached its daily quota. Please request only once every 24 hours.`;
}

const { amount } = dripActions[DRIP_TYPE.SWAG];
const { data: { hash, providerId } } = res;

return `For the next 24 hours, I will send ${amount} NEU to address ${address} per hour. Extrinsic hash: ${hash}. Your provided_id: ${providerId}.`;
}

module.exports = { drip, dripLater, dripSwag };
module.exports = { drip };
15 changes: 1 addition & 14 deletions src/bot/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const _ = require('lodash');

const config = require('./config');
const actions = require('./actions');
const { getNextHourStr } = require('./helperFn');

// Check environment variables valid
if (!process.env.ACCESS_TOKEN) {
Expand All @@ -18,7 +17,7 @@ let ax = axios.create({
timeout: 10000,
});

const client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES] });
const client = new Discord.Client({ intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.MessageContent] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
Expand All @@ -40,23 +39,11 @@ client.on('messageCreate', async msg => {
return;
}

if (action === '!drip-later') {
msg.reply(await actions.dripLater(sender, args[1], _.join(_.slice(args, 2), ' ')));
return;
}

if (action === '!drip-swag') {
msg.reply(await actions.dripSwag(sender, args[1]));
return;
}

if (action === '!faucet') {
msg.reply(`
Usage:
!balance - Get the faucet's balance.
!drip <Address> - Send ${tokenSymbol}s to <Address>.
!drip-later <Address> <Time> - Send ${tokenSymbol}s to <Address> later. Time format(UTC): ${getNextHourStr()}
!drip-swag <Address> - For the next 24 hours, Send ${tokenSymbol}s to <Address> per hour.
!faucet - Prints usage information.`);
}
});
Expand Down
35 changes: 0 additions & 35 deletions src/bot/helperFn.js

This file was deleted.

13 changes: 2 additions & 11 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
const config = {
networkName: 'Neumann Network',
tokenSymbol: 'NEU',
networkName: 'Turing Staging Network',
tokenSymbol: 'TUR',
units: 10**10,
dripActions: {
normal: {
amount: 100,
timesLimit: 1,
},
later: {
amount: 100,
maxScheduleSeconds: 7 * 24 * 60 * 60,
timesLimit: 3,
},
swag: {
amount: 10,
timesLimit: 1,
},
},
};

Expand Down
2 changes: 0 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const DRIP_TYPE = {
NORMAL: 'normal',
LATER: 'later',
SWAG: 'swag',
};

const SS58_PREFIX = 51;
Expand Down
33 changes: 0 additions & 33 deletions src/server/actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const _ = require('lodash');
const { WsProvider, ApiPromise } = require('@polkadot/api');
const pdKeyring = require('@polkadot/keyring');
const uuid = require('uuid');
const moment = require('moment');

const { DRIP_TYPE, SS58_PREFIX } = require('../constants');
const { dripActions, units } = require('./config');
Expand Down Expand Up @@ -35,10 +33,6 @@ class Actions {
switch (dripType) {
case DRIP_TYPE.NORMAL:
return this.drip(params);
case DRIP_TYPE.LATER:
return this.dripLater(params);
case DRIP_TYPE.SWAG:
return this.dripSwag(params);
default:
}
return null;
Expand All @@ -51,33 +45,6 @@ class Actions {
return { hash: extrinsic.hash.toHex() };
}

async dripLater({ address, dripTime }) {
const amount = dripActions[DRIP_TYPE.LATER].amount * units;
const providerId = uuid.v4();
const executionTime = Math.floor(dripTime / 1000);
const extrinsic = this.api.tx.automationTime.scheduleNativeTransferTask(providerId, [executionTime], address, amount);
await this.sendExtrinsic(extrinsic, this.account);
return { hash: extrinsic.hash.toHex(), providerId };
}

async dripSwag({ address }) {
const amount = dripActions[DRIP_TYPE.SWAG].amount * units;
const providerId = uuid.v4();

const now = moment().utc();
const nowWholeHour = moment(`${now.format('YYYY-MM-DD[T]hh')}:00:00Z`); //moment().utc();

const executionTimes = [];
for (let i = 1; i <= 24; i += 1) {
const calcTime = nowWholeHour.clone();
executionTimes.push(Math.ceil(calcTime.add(i, 'hours').valueOf() / 1000));
}

const extrinsic = this.api.tx.automationTime.scheduleNativeTransferTask(providerId, executionTimes, address, amount);
await this.sendExtrinsic(extrinsic, this.account);
return { hash: extrinsic.hash.toHex(), providerId };
}

async checkBalance() {
let balance = 0;
try {
Expand Down
2 changes: 0 additions & 2 deletions src/server/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const express = require('express');
const bodyParser = require('body-parser');
const moment = require('moment');

const Actions = require('./actions');
const Storage = require('./storage');
const config = require('./config');
const { DRIP_TYPE } = require('../constants');

const storage = new Storage();
const app = express();
Expand Down