Skip to content

Instantly share code, notes, and snippets.

View huantt's full-sized avatar
🎯
Focusing

Jack huantt

🎯
Focusing
View GitHub Profile
@huantt
huantt / rate_limit.go
Last active June 23, 2023 10:21
Rate limit pattern using channel in Golang
package main
import (
"fmt"
"sync"
"time"
)
var limit = 10
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import {IERC20} from "./IERC20.sol";
contract Fund {
mapping(address => mapping(address => uint)) public balances;
mapping(address => mapping(address => bool)) public lock;
contract Attack {
DepositFunds public depositFunds;
constructor(address _depositFundsAddress) {
depositFunds = DepositFunds(_depositFundsAddress);
}
// Fallback is called when DepositFunds sends Ether to this contract.
fallback() external payable {
if (address(depositFunds).balance >= 1 ether) {
contract DepositFunds {
mapping(address => uint) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw() public {
uint bal = balances[msg.sender];
require(bal > 0);
@huantt
huantt / IERC20.sol
Last active December 18, 2022 08:30
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
{
"name": "John Doe",
"email":"john@gmail.com",
"exp": 1671722663
}
@huantt
huantt / gist:5ae6643d58ab7c5998745b0f26e0400c
Created August 22, 2022 15:19
jwt-header-sample.json
{
"alg": "HS256",
"typ": "JWT"
}
@huantt
huantt / go-redis-marshal-binary.go
Created August 15, 2022 03:41
Redis is based on key-value pairs, and key-values are all strings and other string-based data structures. Therefore, if you want to put some data into redis, you should make these data strings. Your struct must implement this interface like code below to make go-redis able to stringify your type.
func (i Info) MarshalBinary() (data []byte, err error) {
bytes, err := json.Marshal(i) \\edited - changed to i
return bytes, err
}
package main
import (
"fmt"
"time"
)
func write(ch chan int) {
for i := 0; i < 4; i++ {
func chunkSlice(slice []int, chunkSize int) [][]int {
var chunks [][]int
for i := 0; i < len(slice); i += chunkSize {
end := i + chunkSize
// necessary check to avoid slicing beyond
// slice capacity
if end > len(slice) {
end = len(slice)
}