Skip to content

Commit

Permalink
fix(airdrop): some issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wayne.liang committed Jul 15, 2022
1 parent 73685ea commit edf0eba
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ coverage
*.config.js

cypress/
airdrop/

**/__tests__/
**/plugins/**/*.js
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = {
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'@typescript-eslint/prefer-for-of': 'error',
'@typescript-eslint/prefer-function-type': 'error',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/triple-slash-reference': [
'error',
{
Expand Down Expand Up @@ -93,7 +94,7 @@ module.exports = {
complexity: [
'error',
{
max: 5,
max: 20,
},
],
eqeqeq: ['error', 'smart'],
Expand Down
2 changes: 1 addition & 1 deletion airdrop/api/grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { VercelRequest, VercelResponse } from '@vercel/node';
function getConfig() {
const config = {
defaults: {
origin: 'https://docs.darwinia.network',
origin: 'https://apps.darwinia.network',
transport: 'querystring',
state: true,
},
Expand Down
23 changes: 14 additions & 9 deletions airdrop/api/pangoro/airdrop/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { VercelRequest, VercelResponse } from '@vercel/node';
import { Octokit } from '@octokit/rest';

const Redis = require('ioredis');
const ident = 2;
const gapSeconds = 43200000;

export default async function (req: VercelRequest, res: VercelResponse) {
res.setHeader('content-type', 'application/json');
Expand All @@ -22,7 +24,8 @@ export default async function (req: VercelRequest, res: VercelResponse) {
err: 1,
message: 'Authorization failed. please try login again',
};
res.end(JSON.stringify(body, null, 2));

res.end(JSON.stringify(body, null, ident));
return;
}

Expand All @@ -38,7 +41,7 @@ export default async function (req: VercelRequest, res: VercelResponse) {
state: '',
},
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}

Expand All @@ -49,18 +52,18 @@ export default async function (req: VercelRequest, res: VercelResponse) {
err: 1,
message: `Sorry, we can't find your ip address`,
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}

const chainName = 'PANGORO';
const cacheKeyIp = `${chainName}-${user.id}-${ip}`;

const recordTime = await client.get(cacheKeyIp);
if (recordTime != null) {
if (recordTime !== null) {
const lastClaimTime = +recordTime;
const now = +new Date();
if (now - lastClaimTime <= 1000 * 60 * 60 * 12) {
if (now - lastClaimTime <= gapSeconds) {
res.statusCode = 429;
const body = {
err: 1,
Expand All @@ -70,7 +73,7 @@ export default async function (req: VercelRequest, res: VercelResponse) {
time: lastClaimTime,
},
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}
}
Expand All @@ -79,13 +82,15 @@ export default async function (req: VercelRequest, res: VercelResponse) {
const body = {
err: 0,
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
}

let _redis: any;
let _redis: unknown;

function redis() {
if (_redis) return _redis;
if (_redis) {
return _redis;
}
const config = require('../config/redis.json');
config.url = process.env.REDIS_CONNECT_URL;
_redis = new Redis(config.url);
Expand Down
52 changes: 27 additions & 25 deletions airdrop/api/pangoro/airdrop/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { typesBundleForPolkadotApps } from '@darwinia/types/mix';
import { FrameSystemAccountInfo } from '@polkadot/types/lookup';
import {} from '@polkadot/api-augment';
import { Keyring } from '@polkadot/keyring';
import { hexToU8a } from '@polkadot/util';
import { Octokit } from '@octokit/rest';
import * as qs from 'qs';

const { is } = require('is_js');
const Redis = require('ioredis');

const ident = 2;
const gapSeconds = 43200000;
const AMOUNT = 100;
const precision = 1000000000;

// request
export default async function (req: VercelRequest, res: VercelResponse) {
Expand All @@ -22,23 +23,22 @@ export default async function (req: VercelRequest, res: VercelResponse) {
err: 1,
message: `Sorry, we can't find your ip address.`,
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}
// const

const data = qs.parse(req.body);

res.setHeader('content-type', 'application/json');

// check data
if (!data || typeof data.address != 'string') {
if (!data || typeof data.address !== 'string') {
res.statusCode = 403;
const body = {
err: 1,
message: 'Not have data',
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}

Expand All @@ -49,7 +49,7 @@ export default async function (req: VercelRequest, res: VercelResponse) {
err: 1,
message: 'No address found, please type receiver address',
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}

Expand All @@ -65,7 +65,7 @@ export default async function (req: VercelRequest, res: VercelResponse) {
state: '',
},
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}

Expand All @@ -88,7 +88,7 @@ export default async function (req: VercelRequest, res: VercelResponse) {
state: 'NO_LOGIN',
},
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}

Expand All @@ -97,7 +97,7 @@ export default async function (req: VercelRequest, res: VercelResponse) {
const lastClaimTime = await client.get(cacheKeyLastClaimedTime);
if (lastClaimTime) {
const now = +new Date();
if (now - lastClaimTime <= 1000 * 60 * 60 * 12) {
if (now - lastClaimTime <= gapSeconds) {
res.statusCode = 403;
const body = {
err: 1,
Expand All @@ -107,30 +107,30 @@ export default async function (req: VercelRequest, res: VercelResponse) {
time: lastClaimTime,
},
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}
}

try {
// transfer
const result = await transfer(chainName, data.address);
if (result == null) {
if (result === null) {
res.statusCode = 403;
const body = {
err: 1,
message: 'Transfer failed. please connect team',
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}
if (result instanceof String || typeof result == 'string') {
if (result instanceof String || typeof result === 'string') {
res.statusCode = 403;
const body = {
err: 1,
message: result,
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
return;
}

Expand All @@ -145,18 +145,18 @@ export default async function (req: VercelRequest, res: VercelResponse) {
err: 0,
data: result,
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
} catch (e) {
res.statusCode = 403;
const body = {
err: 1,
message: 'Transfer failed: ' + e.message,
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
}
}

async function transfer(chainName: String, address: String): Promise<TransferReceipt | String | null> {
async function transfer(chainName: string, address: string): Promise<TransferReceipt | string | null> {
chainName = chainName.toUpperCase();

const chain = require('../config/chain.json').pangolin_smart;
Expand All @@ -175,16 +175,16 @@ async function transfer(chainName: String, address: String): Promise<TransferRec

const { data } = await api.query.system.account<FrameSystemAccountInfo>(chain.address);
console.log(`free balance ${data.free} of address ${chain.address}`);
if (data.free.toNumber() <= AMOUNT * 1000000000) {
if (data.free.toNumber() <= AMOUNT * precision) {
return 'All airdrops have ended';
}

console.log(`Transfer chain ${chainName} to ${address.toString()}`);

const keyring = new Keyring({ type: 'sr25519' });
const faucetAccount = keyring.addFromSeed(hexToU8a(chain.seed));
const faucetAccount = keyring.addFromUri(chain.seed);

const txHash = await api.tx.balances.transfer(address.toString(), AMOUNT * 1000000000).signAndSend(faucetAccount);
const txHash = await api.tx.balances.transfer(address.toString(), AMOUNT * precision).signAndSend(faucetAccount);

return {
tx: txHash.toString(),
Expand All @@ -196,17 +196,19 @@ async function transfer(chainName: String, address: String): Promise<TransferRec
}
}

let _redis: any;
let _redis: unknown;

function redis() {
if (_redis) return _redis;
if (_redis) {
return _redis;
}
const config = require('../config/redis.json');
config.url = process.env.REDIS_CONNECT_URL;
_redis = new Redis(config.url);
return _redis;
}

interface TransferReceipt {
tx: String;
preview: String;
tx: string;
preview: string;
}
12 changes: 7 additions & 5 deletions airdrop/api/pangoro/user/info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { VercelRequest, VercelResponse } from '@vercel/node';
import { Octokit } from '@octokit/rest';

const ident = 2;

export default async function (req: VercelRequest, res: VercelResponse) {
const user = await queryGithubAccountInfo(req);
res.statusCode = 200;
Expand All @@ -10,7 +12,7 @@ export default async function (req: VercelRequest, res: VercelResponse) {
message: user ? 'Ok' : 'Can not get user info, Please login',
data: user,
};
res.end(JSON.stringify(body, null, 2));
res.end(JSON.stringify(body, null, ident));
}

async function queryGithubAccountInfo(req: VercelRequest): Promise<UserInfo | null> {
Expand All @@ -36,10 +38,10 @@ async function queryGithubAccountInfo(req: VercelRequest): Promise<UserInfo | nu
}

interface UserInfo {
id: Number;
type: String;
avatar: String;
name: String;
id: number;
type: string;
avatar: string;
name: string;
created_at: Date;
updated_at: Date;
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"include": [
"src",
"src/global.d.ts"
"src/global.d.ts",
"airdrop"
]
}

0 comments on commit edf0eba

Please sign in to comment.