mirror of
https://github.com/alexgo-io/stacks-transaction-sponsor.git
synced 2026-04-30 20:11:38 +08:00
chore: lint code & upgrade deps
Signed-off-by: bestmike007 <i@bestmike007.com>
This commit is contained in:
2
.envrc
2
.envrc
@@ -10,7 +10,7 @@ export POSTGRES_USER=alexgo
|
||||
export POSTGRES_PASSWORD=FYvAsE72XtjbYSt8
|
||||
export POSTGRES_DATA_VOLUME=pgdata
|
||||
|
||||
export HASURA_VERSION=v2.36.1
|
||||
export HASURA_VERSION=v2.37.0
|
||||
export HASURA_PORT=23580
|
||||
export HASURA_ADMIN_SECRET=$POSTGRES_PASSWORD
|
||||
|
||||
|
||||
30
package.json
30
package.json
@@ -26,11 +26,11 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@stacks/common": "^6.10.0",
|
||||
"@stacks/encryption": "^6.11.0",
|
||||
"@stacks/network": "^6.10.0",
|
||||
"@stacks/stacks-blockchain-api-types": "^7.3.6",
|
||||
"@stacks/transactions": "^6.11.0",
|
||||
"@stacks/wallet-sdk": "^6.11.0",
|
||||
"@stacks/encryption": "^6.11.3",
|
||||
"@stacks/network": "^6.11.3",
|
||||
"@stacks/stacks-blockchain-api-types": "^7.8.1",
|
||||
"@stacks/transactions": "^6.11.3",
|
||||
"@stacks/wallet-sdk": "^6.11.3",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.18.2",
|
||||
"got-cjs": "^12.5.4",
|
||||
@@ -38,26 +38,26 @@
|
||||
"ramda": "^0.29.1",
|
||||
"safe-json-stringify": "^1.2.0",
|
||||
"slonik": "^37.2.0",
|
||||
"ts-clarity": "^0.0.15",
|
||||
"ts-clarity": "^0.0.16",
|
||||
"zod": "^3.22.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@swc/core": "^1.3.102",
|
||||
"@swc/core": "^1.4.0",
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/memoizee": "^0.4.11",
|
||||
"@types/node": "^20.10.6",
|
||||
"@types/ramda": "^0.29.9",
|
||||
"@types/node": "^20.11.16",
|
||||
"@types/ramda": "^0.29.10",
|
||||
"@types/safe-json-stringify": "^1.1.5",
|
||||
"@typescript-eslint/eslint-plugin": "^6.17.0",
|
||||
"@typescript-eslint/parser": "^6.17.0",
|
||||
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
||||
"@typescript-eslint/parser": "^6.21.0",
|
||||
"eslint": "^8.56.0",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"eslint-plugin-import": "^2.29.1",
|
||||
"eslint-plugin-prettier": "^5.1.2",
|
||||
"husky": "^8.0.3",
|
||||
"lint-staged": "^15.2.0",
|
||||
"prettier": "^3.1.1",
|
||||
"eslint-plugin-prettier": "^5.1.3",
|
||||
"husky": "^9.0.10",
|
||||
"lint-staged": "^15.2.2",
|
||||
"prettier": "^3.2.5",
|
||||
"prettier-plugin-organize-imports": "^3.2.4",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsetse": "^0.0.4",
|
||||
|
||||
@@ -34,8 +34,9 @@ export function getSponsorAccounts(): SponsorAccount[] {
|
||||
let secretKey = kSponsorSecretKey;
|
||||
for (let i = 0; i < kSponsorAccountCount - 1; i++) {
|
||||
const sk = hashSha256Sync(Buffer.from(secretKey, 'hex'));
|
||||
secretKey =
|
||||
Buffer.from(sk, sk.byteOffset, sk.byteLength).toString('hex') + '01';
|
||||
secretKey = `${Buffer.from(sk, sk.byteOffset, sk.byteLength).toString(
|
||||
'hex',
|
||||
)}01`;
|
||||
const address = getAddressFromPrivateKey(secretKey, transactionVersion);
|
||||
accounts.push({
|
||||
address,
|
||||
|
||||
@@ -18,7 +18,7 @@ async function main() {
|
||||
const balances = await Promise.all(
|
||||
accounts.map(async account => {
|
||||
const url = network.getAccountExtendedBalancesApiUrl(account.address);
|
||||
const { stx } = await got.get(url).json<any>();
|
||||
const { stx } = await got.get(url).json<{ stx: { balance: bigint } }>();
|
||||
return Number(stx.balance) / 1e6;
|
||||
}),
|
||||
);
|
||||
@@ -32,7 +32,7 @@ async function main() {
|
||||
// Don't use this for mainnet, there will be nonce issue if we use the first account to send gas to the rest.
|
||||
// Use another account to send gas to these sponsor accounts
|
||||
if (kStacksNetworkType === 'mainnet') {
|
||||
console.log(`Skip sending gas for mainnet`);
|
||||
console.log('Skip sending gas for mainnet');
|
||||
return;
|
||||
}
|
||||
const { possible_next_nonce } = await getAccountNonces(accounts[0].address, {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { getPgPool } from '../db';
|
||||
|
||||
async function main() {
|
||||
if (kStacksNetworkType === 'mainnet') {
|
||||
console.log(`This script is not for mainnet`);
|
||||
console.log('This script is not for mainnet');
|
||||
return;
|
||||
}
|
||||
const pgPool = await getPgPool();
|
||||
|
||||
@@ -10,9 +10,9 @@ export function getRequiredEnv(envKey: string) {
|
||||
return value;
|
||||
}
|
||||
|
||||
export const kStacksNetworkType: 'mocknet' | 'mainnet' = getRequiredEnv(
|
||||
'STACKS_NETWORK_TYPE',
|
||||
) as any;
|
||||
export const kStacksNetworkType = getRequiredEnv('STACKS_NETWORK_TYPE') as
|
||||
| 'mocknet'
|
||||
| 'mainnet';
|
||||
assert(
|
||||
kStacksNetworkType === 'mainnet' || kStacksNetworkType === 'mocknet',
|
||||
`Invalid STACKS_NETWORK_TYPE: ${kStacksNetworkType}`,
|
||||
|
||||
@@ -61,9 +61,8 @@ export const executeSponsorTransaction: RequestHandler = async (req, res) => {
|
||||
if (e instanceof UnsupportedOperation) {
|
||||
respond(401, 'operation_not_supported', String(e));
|
||||
return;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
const contractCall = tx.payload;
|
||||
assert(
|
||||
@@ -86,7 +85,7 @@ export const executeSponsorTransaction: RequestHandler = async (req, res) => {
|
||||
|
||||
try {
|
||||
tx.verifyOrigin();
|
||||
} catch (e: any) {
|
||||
} catch (e) {
|
||||
respond(400, 'invalid_tx', `unable to verify tx: ${e}`);
|
||||
return;
|
||||
}
|
||||
@@ -142,7 +141,7 @@ export const executeSponsorTransaction: RequestHandler = async (req, res) => {
|
||||
respond(
|
||||
429,
|
||||
'capacity_exceed',
|
||||
`Platform sponsor transaction capacity exceeded, please retry until next block`,
|
||||
'Platform sponsor transaction capacity exceeded, please retry until next block',
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -167,11 +166,9 @@ export const executeSponsorTransaction: RequestHandler = async (req, res) => {
|
||||
${sql.binary(hexToBuffer(tx.txid()))},
|
||||
${sql.binary(hexToBuffer(req.body.tx))},
|
||||
${sender}, ${String(nonce)},
|
||||
${
|
||||
addressToString(contractCall.contractAddress) +
|
||||
'.' +
|
||||
${`${addressToString(contractCall.contractAddress)}.${
|
||||
contractCall.contractName.content
|
||||
},
|
||||
}`},
|
||||
${contractCall.functionName.content},
|
||||
${JSON.stringify(
|
||||
contractCall.functionArgs.map(arg => cvToString(arg)),
|
||||
@@ -182,7 +179,7 @@ export const executeSponsorTransaction: RequestHandler = async (req, res) => {
|
||||
NOW()
|
||||
)`);
|
||||
respond(200, 'ok', null, tx.txid());
|
||||
} catch (e: any) {
|
||||
} catch (e) {
|
||||
respond(500, 'unknown_error', String(e));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -12,11 +12,7 @@ export interface GasConfig {
|
||||
gasCap: bigint;
|
||||
}
|
||||
|
||||
export class UnsupportedOperation extends Error {
|
||||
constructor(msg: string) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
export class UnsupportedOperation extends Error {}
|
||||
|
||||
export async function loadGasConfig(op: StacksTransaction): Promise<GasConfig> {
|
||||
const payload = op.payload;
|
||||
|
||||
10
src/main.ts
10
src/main.ts
@@ -19,8 +19,8 @@ const isHealthy = memoizee(
|
||||
)`SELECT COUNT(*) as c FROM "public"."user_operations" WHERE status = 'pending'`,
|
||||
);
|
||||
return c >= 0n;
|
||||
} catch (e: any) {
|
||||
console.error(`Health check failed with error`, e);
|
||||
} catch (e: unknown) {
|
||||
console.error('Health check failed with error', e);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
@@ -39,7 +39,7 @@ async function printAccounts() {
|
||||
const balances = await Promise.all(
|
||||
accounts.map(async account => {
|
||||
const url = network.getAccountExtendedBalancesApiUrl(account.address);
|
||||
return await got.get(url).json<any>();
|
||||
return await got.get(url).json<{ stx: { balance: bigint } }>();
|
||||
}),
|
||||
);
|
||||
accounts.forEach((account, i) => {
|
||||
@@ -87,12 +87,12 @@ async function main() {
|
||||
try {
|
||||
return await new Promise<void>(f => {
|
||||
process.once('SIGINT', () => {
|
||||
console.log(`User interrupted.`);
|
||||
console.log('User interrupted.');
|
||||
f();
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
console.log(`Closing server and stopping worker`);
|
||||
console.log('Closing server and stopping worker');
|
||||
server.close();
|
||||
stopWorker();
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ export function bufferToHexPrefixString(buff: Buffer): string {
|
||||
if (buff.length === 0) {
|
||||
return '';
|
||||
}
|
||||
return '\\x' + buff.toString('hex');
|
||||
return `\\x${buff.toString('hex')}`;
|
||||
}
|
||||
|
||||
export function hexToBuffer(hex: string): Buffer {
|
||||
|
||||
@@ -87,7 +87,7 @@ export async function submitPendingTransactions(
|
||||
}
|
||||
try {
|
||||
user_tx.verifyOrigin();
|
||||
} catch (e: any) {
|
||||
} catch (e) {
|
||||
await pgPool.query(sql.typeAlias('void')`UPDATE user_operations
|
||||
SET status = 'failed',
|
||||
error = ${String(e)},
|
||||
|
||||
@@ -63,10 +63,10 @@ async function syncRbfTransactionStatus(txId: Buffer) {
|
||||
);
|
||||
return;
|
||||
}
|
||||
} catch (e: any) {
|
||||
} catch (e: unknown) {
|
||||
console.error(
|
||||
`Fail to update status for tx 0x${tx.sponsor_tx_id.toString('hex')}: ${
|
||||
e.stack || e
|
||||
(e as Error).stack || e
|
||||
}`,
|
||||
);
|
||||
}
|
||||
@@ -167,10 +167,10 @@ export async function syncTransactionStatus(
|
||||
WHERE id = ${String(tx.id)}`);
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
} catch (e: unknown) {
|
||||
console.error(
|
||||
`Fail to update status for tx 0x${tx.sponsor_tx_id.toString('hex')}: ${
|
||||
e.stack || e
|
||||
(e as Error).stack || e
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import safeJsonStringify from 'safe-json-stringify';
|
||||
|
||||
export type ReplacerFn = (key: any, value: any) => any;
|
||||
export type ReplacerFn = (key: unknown, value: unknown) => unknown;
|
||||
|
||||
export function hexToBuffer(bytesLike: string) {
|
||||
return Buffer.from(
|
||||
@@ -19,9 +19,9 @@ export function bigintReplacer(replacer?: ReplacerFn | null): ReplacerFn {
|
||||
}
|
||||
|
||||
export function stringify(
|
||||
obj: any,
|
||||
obj: unknown,
|
||||
replacer?: ReplacerFn | null,
|
||||
indent?: number,
|
||||
) {
|
||||
return safeJsonStringify(obj, bigintReplacer(replacer), indent);
|
||||
return safeJsonStringify(obj as object, bigintReplacer(replacer), indent);
|
||||
}
|
||||
|
||||
@@ -46,8 +46,10 @@ export function startWorker() {
|
||||
while (running) {
|
||||
try {
|
||||
await runWorkerLoop();
|
||||
} catch (e: any) {
|
||||
console.error(`Worker loop failed with error: ${e.stack || e}`);
|
||||
} catch (e: unknown) {
|
||||
console.error(
|
||||
`Worker loop failed with error: ${(e as Error).stack || e}`,
|
||||
);
|
||||
}
|
||||
for (let i = 0; running && i < 100; i++) {
|
||||
await new Promise(f => setTimeout(f, 100));
|
||||
|
||||
Reference in New Issue
Block a user