Setup handler to migrate SC ledger accounts to Database Players

This commit is contained in:
Hammad Jutt
2020-08-15 23:53:08 -06:00
parent bcb69ad307
commit c27c77eb74
9 changed files with 179 additions and 7 deletions

View File

@@ -0,0 +1,82 @@
import { Request, Response } from 'express';
import fetch from 'node-fetch';
import {
Account_Constraint,
Account_Update_Column,
Player_Constraint,
Player_Update_Column,
Scalars,
} from '../../../lib/autogen/sdk';
import { client } from '../../../lib/hasuraClient';
import { AddressBookEntry, SCAccountsData } from './types';
const ACCOUNTS_FILE =
'https://raw.githubusercontent.com/MetaFam/XP/gh-pages/output/accounts.json';
const ADDRESS_BOOK_FILE =
'https://raw.githubusercontent.com/MetaFam/TheSource/master/addressbook.json';
const parseAlias = (alias: string) => {
const [type, identifier] = alias.split('/');
return {
type: type.toUpperCase() as Scalars['account_type'],
identifier,
};
};
export const migrateSourceCredAccounts = async (
_: Request,
res: Response,
): Promise<void> => {
const accountsData: SCAccountsData = await (
await fetch(ACCOUNTS_FILE)
).json();
const addressBook: AddressBookEntry[] = await (
await fetch(ADDRESS_BOOK_FILE)
).json();
const accountOnConflict = {
constraint: Account_Constraint.AccountIdentifierTypePlayerKey,
update_columns: [Account_Update_Column.Identifier],
};
const accountList = accountsData.accounts
.filter((a) => a.account.identity.subtype === 'USER')
.map((a) => {
const discordAlias = a.account.identity.aliases.find(
(alias) => alias.description.indexOf(`discord/`) >= 0,
);
const discordId =
discordAlias && discordAlias.description.split('discord/')[1];
const addressEntry =
discordId && addressBook.find((adr) => adr.discordId === discordId);
return {
ethereum_address: addressEntry && addressEntry.address,
scIdentityId: a.account.identity.id,
username: a.account.identity.name,
totalXp: a.totalCred,
Accounts: {
data: a.account.identity.aliases.map((alias) => {
return parseAlias(alias.description);
}),
on_conflict: accountOnConflict,
},
};
});
const result = await client.UpsertPlayer({
objects: accountList,
onConflict: {
constraint: Player_Constraint.PlayerScIdentityIdKey,
update_columns: [
Player_Update_Column.EthereumAddress,
Player_Update_Column.Username,
Player_Update_Column.TotalXp,
],
},
});
res.json(result);
};

View File

@@ -0,0 +1,44 @@
export interface SCAccountsData {
accounts: SCAccount[];
intervalEndpoints: number[];
unclaimedAliases: SCUnclaimedAlias[];
}
export interface SCAccount {
account: SCAccountInfo;
cred: number[];
totalCred: number;
}
export interface SCAccountInfo {
active: boolean;
balance: string;
identity: SCIdentity;
paid: string;
}
export interface SCIdentity {
address: string;
aliases: SCAlias[];
id: string;
name: string;
subtype: string;
}
export interface SCAlias {
address: string;
description: string;
}
export interface SCUnclaimedAlias {
alias: SCAlias;
cred: number[];
totalCred: number;
}
export interface AddressBookEntry {
name: string;
createdAt: number;
address: string;
discordId: string;
}

View File

@@ -1,6 +1,7 @@
import express from 'express';
import { asyncHandlerWrapper } from '../../lib/apiHelpers';
import { migrateSourceCredAccounts } from './migrateSourceCredAccounts/handler';
import { updateBoxProfileHandler } from './updateBoxProfile/handler';
export const actionRoutes = express.Router();
@@ -9,3 +10,8 @@ actionRoutes.post(
'/updateBoxProfile',
asyncHandlerWrapper(updateBoxProfileHandler),
);
actionRoutes.post(
'/migrateSourceCredAccounts',
asyncHandlerWrapper(migrateSourceCredAccounts),
);

View File

@@ -4,7 +4,3 @@ type UpdateBoxProfileResponse = {
success: boolean;
updatedProfiles: Array<string>;
};
type Mutation = {
updateBoxProfile?: Maybe<UpdateBoxProfileResponse>;
};

View File

@@ -28,3 +28,14 @@ export const UpsertAccount = gql`
}
}
`;
export const UpsertPlayer = gql`
mutation UpsertPlayer(
$objects: [Player_insert_input!]!
$onConflict: Player_on_conflict
) {
insert_Player(on_conflict: $onConflict, objects: $objects) {
affected_rows
}
}
`;