-
Notifications
You must be signed in to change notification settings - Fork 22
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 #193 from ckb-cell/feat/save-ckbvirtualtx-result
refactor(examples): Save ckbVirtualTxResult locally
- Loading branch information
Showing
14 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,49 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { | ||
BaseCkbVirtualTxResult, | ||
SporeVirtualTxResult, | ||
SporeCreateVirtualTxResult, | ||
SporeTransferVirtualTxResult, | ||
} from 'rgbpp/ckb'; | ||
|
||
/** | ||
* Save ckbVirtualTxResult to a log file | ||
* @param ckbVirtualTxResult - The ckbVirtualTxResult to save | ||
* @param exampleName - Example name used to distinguish different log files | ||
*/ | ||
|
||
export type CkbVirtualTxResultType = | ||
| BaseCkbVirtualTxResult | ||
| SporeVirtualTxResult | ||
| SporeCreateVirtualTxResult | ||
| SporeTransferVirtualTxResult; | ||
|
||
export const saveCkbVirtualTxResult = (ckbVirtualTxResult: CkbVirtualTxResultType, exampleName: string) => { | ||
try { | ||
// Define log file path | ||
const logDir = path.resolve(__dirname, '../logs'); | ||
const timestamp = new Date().toISOString().replace(/:/g, '-'); // Replace colons with hyphens | ||
const logFilePath = path.join(logDir, `${exampleName}-${timestamp}-ckbVirtualTxResult.log`); | ||
|
||
// Ensure the log directory exists | ||
if (!fs.existsSync(logDir)) { | ||
fs.mkdirSync(logDir); | ||
} | ||
|
||
// Validate and save ckbVirtualTxResult to log file | ||
if (typeof ckbVirtualTxResult === 'object' && ckbVirtualTxResult !== null) { | ||
fs.writeFileSync(logFilePath, JSON.stringify(ckbVirtualTxResult, null, 2)); | ||
console.info(`Saved ckbVirtualTxResult to ${logFilePath}`); | ||
} else { | ||
console.error('Invalid ckbVirtualTxResult format'); | ||
} | ||
|
||
// Remind developers to save the transaction result | ||
console.info( | ||
`Important: It's recommended to save the rgbpp_ckb_tx_virtual locally before the isomorphic transactions are finalized.`, | ||
); | ||
} catch (error) { | ||
console.error('Failed to save ckbVirtualTxResult:', error); | ||
} | ||
}; |
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
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
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