-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from fioprotocol/develop
Develop > master for 1.10.0 release
- Loading branch information
Showing
30 changed files
with
960 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
server/db/migrations/20220411191359-add_account_profile_table_and_refactor_wallet.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use strict'; | ||
|
||
require('dotenv').config({ | ||
path: require('path').resolve(process.cwd(), '.env-server') | ||
}); | ||
|
||
const Sequelize = require("sequelize"); | ||
const {Fio, Ecc} = require("@fioprotocol/fiojs"); | ||
const {PrivateKey} = Ecc; | ||
|
||
const walletPrivateKey = process.env.WALLET_PRIVATE_KEY; | ||
|
||
const walletPublicKey = PrivateKey(walletPrivateKey).toPublic().toString('FIO'); | ||
|
||
const defaultActor = Fio.accountHash(walletPublicKey); | ||
const defaultPermission = 'active'; | ||
|
||
module.exports = { | ||
up: async (QI, DT) => { | ||
return QI.sequelize.transaction( async t => { | ||
await QI.createTable('account_profile', | ||
{ | ||
id: { | ||
type: DT.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
allowNull: false | ||
}, | ||
actor: { | ||
type: DT.STRING, | ||
allowNull: false, | ||
}, | ||
permission: { | ||
type: DT.STRING, | ||
allowNull: false | ||
}, | ||
name: { | ||
type: DT.STRING, | ||
allowNull: true | ||
}, | ||
is_default: { | ||
type: DT.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: false | ||
}, | ||
created_at: { | ||
type: DT.DATE, | ||
defaultValue: Sequelize.fn('now'), | ||
allowNull: false | ||
}, | ||
updated_at: { | ||
type: DT.DATE, | ||
defaultValue: Sequelize.fn('now'), | ||
allowNull: false | ||
}, | ||
deleted_at: { type: DT.DATE }, | ||
}, | ||
{ transaction: t } | ||
); | ||
|
||
await QI.addColumn('wallet', 'account_profile_id', { | ||
type: DT.INTEGER, | ||
references: { | ||
model: 'account_profile', | ||
key: 'id' | ||
}, | ||
onUpdate: 'cascade', | ||
onDelete: 'set null', | ||
allowNull: true, | ||
defaultValue: null | ||
}, | ||
{transaction: t}); | ||
|
||
await QI.sequelize.query(`INSERT INTO public.account_profile ( actor, permission, is_default ) VALUES ('${defaultActor}', '${defaultPermission}', TRUE)`, {transaction: t}); | ||
await QI.sequelize.query(`INSERT INTO public.account_profile ( actor, permission ) SELECT actor, permission FROM public.wallet WHERE actor <> '' GROUP BY actor, permission;`, {transaction: t}); | ||
await QI.sequelize.query(`UPDATE public.wallet SET account_profile_id = public.account_profile.id FROM public.account_profile WHERE public.account_profile.actor = public.wallet.actor AND public.account_profile.permission = public.wallet.permission;`, {transaction: t}); | ||
await QI.sequelize.query(`UPDATE public.wallet SET account_profile_id = 1 WHERE public.wallet.account_profile_id IS NULL;`, {transaction: t}); | ||
await QI.removeColumn('wallet', 'actor', {transaction: t}); | ||
await QI.removeColumn('wallet', 'permission', {transaction: t}); | ||
}); | ||
}, | ||
|
||
down: async (QI, DT) => { | ||
return QI.sequelize.transaction( async t => { | ||
await QI.addColumn('wallet', 'actor', { | ||
type: DT.STRING, | ||
allowNull: false, | ||
defaultValue: '' | ||
}, {transaction: t}); | ||
await QI.addColumn('wallet', 'permission', { | ||
type: DT.STRING, | ||
allowNull: false, | ||
defaultValue: '' | ||
}, {transaction: t}); | ||
|
||
await QI.sequelize.query(`UPDATE public.wallet SET actor = public.account_profile.actor, permission = public.account_profile.permission FROM public.account_profile WHERE public.account_profile.id = public.wallet.account_profile_id;`, {transaction: t}) | ||
|
||
await QI.removeColumn('wallet', 'account_profile_id', {transaction: t}); | ||
return QI.dropTable('actor', {transaction: t}); | ||
}) | ||
} | ||
}; |
55 changes: 55 additions & 0 deletions
55
server/db/migrations/20220503103542-auto-retry-insufficient-funds-errored.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
const autoRetry = async QI => { | ||
QI.sequelize.transaction( async t => { | ||
const btes = await QI.sequelize.query(`SELECT account_id, type FROM "blockchain_trx" bt | ||
JOIN blockchain_trx_event bte ON bte.blockchain_trx_id = bt.id | ||
WHERE bte.id = ( | ||
SELECT max(sub_bte.id) over (partition by account_id) mid | ||
FROM blockchain_trx sub_bt | ||
LEFT OUTER JOIN blockchain_trx_event sub_bte ON sub_bte.blockchain_trx_id = sub_bt.id | ||
WHERE sub_bte.created > '04-28-2022' AND sub_bt.account_id = bt.account_id | ||
GROUP BY sub_bte.id, sub_bt.account_id, sub_bt.type | ||
ORDER BY sub_bte.id DESC | ||
LIMIT 1 | ||
) | ||
AND bte.trx_status = 'review' | ||
AND bte.trx_status_notes = 'Insufficient funds to cover fee' | ||
AND bte.created > '04-28-2022'` | ||
, { transaction: t }); | ||
|
||
const bts = [] | ||
const accountIdByBts = {} | ||
for (const bteItem of btes[0]) { | ||
const btRes = await QI.sequelize.query(` | ||
INSERT INTO "blockchain_trx" (account_id, type) VALUES (${bteItem.account_id}, '${bteItem.type}') RETURNING id | ||
`, { transaction: t }); | ||
bts.push(btRes[0][0].id) | ||
accountIdByBts[btRes[0][0].id] = bteItem.account_id | ||
} | ||
for (const btId of bts) { | ||
const bteRes = await QI.sequelize.query(` | ||
INSERT INTO "blockchain_trx_event" (blockchain_trx_id, trx_status, trx_status_notes) | ||
VALUES (${btId}, 'retry', 'Retry insufficient funds trx errors') RETURNING id | ||
`, { transaction: t }); | ||
|
||
await QI.sequelize.query(` | ||
UPDATE "registrations-search" SET blockchain_trx_id = ${btId}, | ||
blockchain_trx_event_id = ${bteRes[0][0].id}, | ||
trx_status = 'retry' | ||
WHERE account_id = ${accountIdByBts[btId]} | ||
`, { transaction: t }); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = { | ||
up: async QI => { | ||
// Auto-retry Insufficient funds errored. No need to launch for now | ||
// autoRetry(QI) | ||
}, | ||
|
||
down: async () => { | ||
// | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const Sequelize = require('sequelize') | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const AccountProfile = sequelize.define('AccountProfile', { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
allowNull: false | ||
}, | ||
actor: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
permission: { | ||
type: DataTypes.STRING, | ||
allowNull: false | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: true | ||
}, | ||
is_default: { | ||
type: DataTypes.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: false | ||
}, | ||
}, { | ||
tableName: 'account_profile', | ||
paranoid: true, | ||
timestamps: true, | ||
underscored: true, | ||
}); | ||
|
||
AccountProfile.associate = function(models) { | ||
AccountProfile.hasMany(models.Wallet, { as: 'accountProfile', foreignKey: 'account_profile_id'}) | ||
}; | ||
|
||
return AccountProfile; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.