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

Move grab transaction to context repo #28

Merged
Merged
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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ If you want your protocol to work well on your own app, you can stick with a pri
If you want your protocol to work well across all of web3, contribute to **EVM Context**.

##### Ideal contributors
* Protocol teams

- Protocol teams

##### Ideal users
* Wallets
* Web3 browsing tools
* Web3 analytics tools

- Wallets
- Web3 browsing tools
- Web3 analytics tools

## Quick start

Expand All @@ -26,7 +28,7 @@ This will generate a new file called `protocol/[name of protocol].ts` and a test

You should write unit tests for your PR using a real transaction.

You can do this by running `npm run grab:transaction --hash [txHash] --prefix [nickname for the type of tx]`.
You can do this by running `npm run grab:transaction [txHash] [nickname for the type of tx]`.

### Finishing your new contextualization

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"prepublishOnly": "npm run build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"grab-transaction": "node ./scripts/grab-transaction.js",
"grab:transaction": "npm run build && node ./dist/commands/main.js grab-transaction",
"create:contextualizer": "npm run build && node ./dist/commands/main.js create-contextualizer"
},
"repository": {
Expand Down
45 changes: 45 additions & 0 deletions src/commands/grabTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as fs from 'fs';
import * as path from 'path';
import { program } from './main';
import { shortenTxHash } from '../helpers/utils';

export function registerGrabTransactionCommand() {
program
.command('grab-transaction')
.description('Grab a transaction from API')
.argument('<hash>', 'transaction hash')
.argument('<prefix>', 'file name prefix')
.action(async (hash, prefix, options) => {
const srcDir = path.join(__dirname, '..', '..', 'src');
// generate a file name
const fileName = prefix + '-' + shortenTxHash(hash);
const txFilePath = path.join(
srcDir,
'test',
'transactions',
`${fileName}.json`,
);

try {
console.log(`Fetching transaction from transaction api: ${hash}`);
// grab a transaction
const defaultApiUrl = 'https://api.onceupon.gg/v1';
const API_URL = process.env.API_URL || defaultApiUrl;
const transaction = await fetch(
`${API_URL}/v1/transactions/${hash}?withContext=false`,
).then((res) => res.json());

// write to file
fs.writeFileSync(txFilePath, JSON.stringify(transaction, null, 2));
console.log(`Transaction saved to ${txFilePath}.json`);
process.exit(0); // Successful exit
} catch (error) {
console.error('Failed to grab the transaction:', error);
process.exit(1); // Exit with error
}
});
}

function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
2 changes: 2 additions & 0 deletions src/commands/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Command } = require('commander');
import { registerCreateContextualizerCommand } from './createContextualizer';
import { registerGrabTransactionCommand } from './grabTransaction';
export const program = new Command();

program
Expand All @@ -8,5 +9,6 @@ program
.version('0.1.0');

registerCreateContextualizerCommand();
registerGrabTransactionCommand();

program.parse(process.argv);
5 changes: 5 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ export const countValidChars = (stringToCount: string) => {
}
return count;
};

export function shortenTxHash(hash: string): string {
if (hash.length <= 10) return hash;
return hash.substr(0, 6) + hash.substr(-4);
}