Skip to content

Instantly share code, notes, and snippets.

View guillermoriv's full-sized avatar
🎯
Focusing

Guillermo guillermoriv

🎯
Focusing
  • Portugal, SantΓ‘rem
View GitHub Profile
@guillermoriv
guillermoriv / abiGenerator.sh
Created January 20, 2023 10:49
Generating abi for contracts that uses another contracts
solc --abi --include-path node_modules/ --base-path . contracts/LiquidityMiningCampaign.sol -o abis
export const numberToStringWithoutCommas = (
bigNumber: string | number = '0',
minDecimals: number = 2,
): string => {
const num = typeof bigNumber === 'number' ? bigNumber : parseFloat(bigNumber);
if (num === 0) return '0.0';
if (Math.abs(num) < 0.01) return num.toFixed(4);
if (Math.abs(num) < 0.1) return num.toFixed(3);
if (Math.abs(num) < 10) return num === Math.floor(num) ? num.toString() : num.toFixed(2);
@guillermoriv
guillermoriv / formatNumber.js
Created June 15, 2022 09:05
Formatting a number to a string value
export const formatTokenInput = (value: number): string => {
if (String(value).match(/^(0[1-9]+)/) || String(value).match(/^0{2,}[1-9]+/)) {
// to remove any leading zero after a number
return String(value).replace(/^[0.]+/, '');
} else if (String(value).match(/^0{2,}[\.,]/)) {
// to remove leading zeroes when has decimal point
return String(value).replace(/^0+/, '0');
} else if (String(value).match(/^0{2}/)) {
// to remove zeroes in the beginning
return String(value).replace(/^[0.]+/, '0');
@guillermoriv
guillermoriv / callStatic.js
Created June 2, 2022 16:05
CallStatic to a Write function ethersjs
await contract.callStatic.transfer(to, amount);
// to: being the address
// amount: being the amount to transfer
// return value: being the boolean that transfer returns
@guillermoriv
guillermoriv / diferentNumbers.js
Created May 13, 2022 09:27
This generator will guarantee that i < j < k, and it will give you three different numbers always
function main() {
let i, j, k;
// this will guarantee that i < j < k
for (i = 0; i <= 9; i++) {
for (j = i + 1; j <= 9; j++) {
for (k = j + 1; k <= 9; k++) {
console.log("%d%d%d\n", i, j, k);
}
@guillermoriv
guillermoriv / signMessage.js
Last active May 6, 2022 06:37
Sign parameters with ethers
const signMessage = async (signer, provider) => {
const message = ethers.utils.keccak256(
ethers.utils.defaultAbiCoder.encode(
["uint256", "address"],
[provider, signer.address]
)
);
return await signer.signMessage(ethers.utils.arrayify(message));
};
🌞 Morning 221 commits β–ˆβ–ˆβ–ˆβ–ˆβ–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ 20.9%
πŸŒ† Daytime 475 commits β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ 45.0%
πŸŒƒ Evening 322 commits β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ 30.5%
πŸŒ™ Night 37 commits β–‹β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ 3.5%
@guillermoriv
guillermoriv / logs.js
Created March 24, 2022 14:37
Find a transaction in the logs by the event signature in ethers (V5)
const receipt = await tx.wait(); // this is the receipt from the transaction that was mined, with the logs for the transactions.
const sig = ethers.utils.keccak256(
ethers.utils.toUtf8Bytes(
'LogLiquidityRemove(address,address,uint256,uint256)',
),
);
const resultFilter = receipt.logs.filter((item) =>
item.topics.includes(sig),
)[0];
const balancesFromRemove = ethers.utils.defaultAbiCoder.decode(