{"id":13767579,"url":"https://github.com/AdvaithD/huff-pg","last_synced_at":"2025-05-10T23:30:46.243Z","repository":{"id":97729052,"uuid":"371237420","full_name":"AdvaithD/huff-pg","owner":"AdvaithD","description":"huff playground - an EVM programming language.","archived":false,"fork":false,"pushed_at":"2021-08-06T01:15:55.000Z","size":46,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-17T03:30:26.339Z","etag":null,"topics":["huff","macros"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AdvaithD.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-05-27T03:40:31.000Z","updated_at":"2023-02-12T17:28:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"5ca5dffd-d34f-43bf-b232-b38ae0bbb983","html_url":"https://github.com/AdvaithD/huff-pg","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdvaithD%2Fhuff-pg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdvaithD%2Fhuff-pg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdvaithD%2Fhuff-pg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdvaithD%2Fhuff-pg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AdvaithD","download_url":"https://codeload.github.com/AdvaithD/huff-pg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253497296,"owners_count":21917683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["huff","macros"],"created_at":"2024-08-03T16:01:09.945Z","updated_at":"2025-05-10T23:30:45.500Z","avatar_url":"https://github.com/AdvaithD.png","language":"JavaScript","funding_links":[],"categories":["We heard you like ERC20 implementations"],"sub_categories":[],"readme":"### **Huff playground**\n\nHuff is a low-level programming language used to instrument macros inside of an EVM. This repo serves as a playground towards learning huff and lower level EVM details.\n#### **Introduction**\n\nHuff was created while writing a zk proof library (weierstrudel). At the time, this was something that could not be done in solidity.\n\nHuff is about as close as you can get to the EVM in terms of assembly code.\n\n- Most huff programs are macros, which can contain more macros or evm opcodes.\n- When a macro is invoked, template params are supplied to the macro.\n- Huff doesn't have functions or variables\n- Ultimately, if your goal is to write gas-efficient contracts, huff is the way to go.\n\n### **Instructions**\n\n1. Clone repo, run `yarn install`\n2. To run tests, run `yarn test`\n3. If you'd like to see OPCODES and stack logged on each interaction, modify `shouldLogSteps` in `config.js` to `true`.\n\nExpected output:\n\n```\n  Simpletoken - ERC20 in Huff\ninfo GAS Gas used by balanceOf(): 422\ninfo GAS Gas used by balanceOf(): 422\n    ✔ checks balances and totalSupply on init == 0\ninfo GAS Gas used by totalSupply(): 290\n    ✔ should expect initial supply == 0\ninfo GAS Gas used by mint(): 42394\ninfo GAS Gas used by balanceOf(): 422\n    ✔ should mint tokens to owner address\ninfo GAS Gas used by totalSupply(): 290\n    ✔ should have 16k as totalSupply (post mint)\ninfo GAS Gas used by mint(): 27394\ninfo GAS Gas used by balanceOf(): 422\ninfo GAS Gas used by totalSupply(): 290\n    ✔ deployer should mint tokens to address1\ninfo GAS Gas used by transfer(): 27533\ninfo GAS Gas used by balanceOf(): 422\ninfo GAS Gas used by balanceOf(): 422\n    ✔ should be able to transfer tokens\ninfo GAS Gas used by getAllowance(): 547\n    ✔ should check allowances and assert == 0\ninfo GAS Gas used by approve(): 21993\ninfo GAS Gas used by getAllowance(): 547\n    ✔ should set allowance and check new values\n```\n\n### **Project Structure**\n\n```\n./annotated\n    /uzicoin.huff -         - Huff contract written with a ton of rough notes, hence annotated\n./test\n    /util.js                - Contains helpers to initiate a VM, Huff runtime, orchestrating fn calls\n    /simpletoken.spec.js    - Test suite for the erc20\nconfig.js                   - contains a flag (shouldLogSteps), turning this to true logs OPCODES \n                              and stack on every interaction inside the VM\nsimpletoken.huff            - Clean version of ERC20 written in huff\n```\n\n### **ERC20 Spec**\n\nA given ERC20 token has the following functions, all of which we will be implementing inside of a huff program (in the form of macros)\n```\nfunction totalSupply() public view returns (uint);\nfunction balanceOf(address tokenOwner) public view returns (uint);\nfunction allowance(address tokenOwner, address spender) public view returns (uint);\nfunction transfer(address to, uint tokens) public returns (bool);\nfunction approve(address spender, uint tokens) public returns (bool);\nfunction transferFrom(address from, address to, uint tokens) public returns (bool);\nfunction mint(address to, uint tokens) public returns (bool);\nevent Transfer(address indexed from, address indexed to, uint tokens);\nevent Approval(address indexed tokenOwner, address indexed spender, uint tokens);\n```\n\n**Variables:**\n- We need to identify storage locations and map them to a variable.\n- To do this, we create macros that refer to storage locations that the ERC20\ncontract is interested in (e.g: balance location, owner address location)\n\n**Solidity Mappings:**\n- Smart contracts store data using `sstore`, using a pointer to a storage location.\n- Each storage location can contain upto 32 bytes of data.\n- Mappings are instrumented by combining the mapping key with storage slot of the mapping. This is then hashed, resulting in a 32 byte storage pointer unique to the key and variable in context.\n\n**Calldata:**\n- Data structure that stores input data sent as part of a contract call / transaction. Huff can load calldata using the `calldataload` opcode. Although, we do need to mention the offset in calldata to start loading from. =\u003e costs `6 gas` to load a word from calldata.\n- We instead duplicate the entirety of calldata, since it costs only `3 gas`\n\n**Events:**\n- Events have two important attributes - `topics` and `data`. Topics are created when an indexed parameter exists (e.g: `Transfer(address indexed from, address indexed to, uint256 value)`)\n- `keccack256` hash of an indexed element is used as the database lookup index (TODO: dig deeper)\n- **Event signature:** is the `keccack256` hash of the event signature (e.g: `Transfer(address)`, `Approval(address, address, uint)`)\n- We arrive at the conclusion that we need to put the data associated with `topics` onto the stack.\n\n\n### **Credits**\n\nThe aztec team for the tutorials: [1](https://medium.com/aztec-protocol/from-zero-to-nowhere-smart-contract-programming-in-huff-1-2-ba2b6de7fa83) [2](https://medium.com/aztec-protocol/from-zero-to-nowhere-smart-contract-programming-in-huff-2-3-5438ef7e5beb) [3](https://medium.com/aztec-protocol/from-zero-to-nowhere-smart-contract-programming-in-huff-3-4-6b347e23d66e) [4](https://medium.com/aztec-protocol/from-zero-to-nowhere-smart-contract-programming-in-huff-4-4-9e6c34648992)\n\nSolidity workshop on storage [source](https://github.com/androlo/solidity-workshop/blob/master/tutorials/2016-03-13-advanced-solidity-IV.md)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAdvaithD%2Fhuff-pg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAdvaithD%2Fhuff-pg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAdvaithD%2Fhuff-pg/lists"}