Skip to content

Instantly share code, notes, and snippets.

View MCarlomagno's full-sized avatar
🚀

Marcos Carlomagno MCarlomagno

🚀
  • OpenZeppelin
  • Rosario - Argentina
  • 21:19 (UTC -03:00)
View GitHub Profile
@MCarlomagno
MCarlomagno / attempt.ts
Created August 6, 2022 22:51
❌ Go-like error catching implementation to avoid messy try-catch statements in Typescript.
/**
* Go-like error catching implementation.
*
* The 'attempt' function receives a callback function as a parameter
* and calls it inside a try catch statement.
*
* Returns a tuple of [data, error] where 'data' (if exists) is the
* result of the operation and 'error' (if exists)
* is the error from the catch statement.
*/
@MCarlomagno
MCarlomagno / metamask.ts
Last active July 19, 2023 22:36
🦊🪝 React hook for Metamask extension using ethers.js. The hook includes some of the most common use cases such as wallet connection, events listening, transaction, addresses and signer management.
import * as ethers from 'ethers';
import {
ExternalProvider,
JsonRpcSigner,
Network,
Web3Provider
} from '@ethersproject/providers';
import { useState } from 'react';
declare global {
const untar = await require("js-untar");
async download(file: PersssistFile) {
const iterable = this.ipfs.get(file.filePath);
var chunks: Uint8Array[] = [];
// we need to use a for await for downloading
// the buffer in chunks.
for await (const b of iterable) {
chunks.push(b);
async getFilesMetadata(): Promise<PersssistFile[]> {
const methods = this.contract.methods;
const filesCount = await methods.fileCount().call();
const filesMetadata: PersssistFile[] = [];
for (var i = filesCount; i >= 1; i--) {
const file = await methods.files(i).call()
filesMetadata.push({
fileId: file.id,
fileName: file.fileName,
filePath: file.filePath,
async uploadFileMetadata(
path: string,
size: number,
type: string,
name: string,
account: string,
) {
return this.contract.methods
.uploadFile(path, size, type, name)
.send({ from: account })
import { create } from "ipfs-http-client";
async upload(file: File) {
this.ipfs = create({
host: 'ipfs.infura.io',
port: 5001,
protocol: 'https'
});
const blob = new Blob([file.buffer], { type: file.type });
import Web3 from "web3";
// we import the abi file created after
// the migration using truffle.
import Persssist from '../../public/abis/Persssist.json';
async initializeContractLocal() {
if (window.ethereum) this.web3 = new Web3(window.ethereum)
else if (window.web3) this.web3 = new Web3(window.web3.currentProvider);
async requestAccounts() {
if(typeof window === "undefined") return;
return window
.ethereum?.request({ method: "eth_requestAccounts" })
.catch((err: any) => console.log(err));
}
async fetchAccounts() {
if(typeof window === "undefined") return;
return window
.ethereum?.request({ method: "eth_accounts" })
.catch((err: any) => console.log(err));
}
it("uploads a valid file", async () => {
// checks filecount variable
// before uploading the file
const countBeforeUpload = await this.contract.fileCount();
await this.contract.uploadFile('path', 1, 'type', 'name');
// checks filecount increased
// by one after upload
const countAfterUpload = await this.contract.fileCount();