Skip to content

Instantly share code, notes, and snippets.

@Maadhav
Created September 9, 2023 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maadhav/541b9749091c8d7f2ebb7899ec7f0746 to your computer and use it in GitHub Desktop.
Save Maadhav/541b9749091c8d7f2ebb7899ec7f0746 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
ref: refs/heads/main
DIRCd��3a�d��3a�$�����o#q{K���Sc��X�W�n.prettierrc.jsond��.@�d��.@���^[��z5|���3���7k/�Xcontracts/MyToken.sold��0�n@d��0�n@ ���cS?���5?,���7��'�|scripts/deploy_with_ethers.tsd��1�Pd��1�P!������.��E�r���NH-��Wscripts/deploy_with_web3.tsd��2?(�d��2?(�"���'S��Н��M�%5�s<��wscripts/ethers-lib.tsd��2�:�d��2�:�#��%�~P9���WrV���ߒ�~�scripts/web3-lib.tsd��4U7�d��4U7�&�� �s�� �rw�6����T�~^�Htests/MyToken_test.solH���9 kD7o��� �:
x�M��
�0D��7I@���P���M�&m�bڔ$RBɿ[k���;e��f��Uuk]@�R��F$\��AŪ�j�4���0�cS�qd�.� ��N�� � ;Y�i��c<هn�� ���ha��,��1�?bjR��
�;��g_[�z �M�(I���B�
x�]SMo�0 �ٿ����@�>C�v�:耦Z`��6���%CR�A�{)�#�r0"�||���ç�_�}�m����ې�p�����GQvv�����{�[�� )(�r �vh���uF��8���%P����:>��P�y��1�?G��<��B#������iC&��湫m��ἢ�[�nUI��d8a��v�R9>Cnk��ǁ�͔f��e��!.�^��u/�wc�,�$n��%��=]�3���{����..�C��"�uC��u��p�x�W?�S�2XiG,]Pn #;�������-�6�؀�P�"��%OV��d�Nj�:��_��w���f�U������2,��� ��m��un�Β�FD�M!�ų�j�\oP�^$����RV�w M�xh�D�\����J��XJp�ҁ�V���&�+��
k2��� �ċ�t���xG�n܋,9�[�=Լd�� �y�;�?��"�Ǒ��5�ŒE�aE7�Y�w��� ��`^�!;��Sz��j��s�=��M������b�H�O�wT�D�|1����:�+|j�~.!���t]��i�~^V�O�Mۓ�;�v���e�dm�
��WA��� k<��� }�V
x�+)JMU041c040031Q�+(J-)�L-*J��*��cش4_���{v?���#����31��������b�� ���K{��2r�t��c^�)��8�(��¯j�&��i����W<L޷��.����b��*���u�lc��}|��d���m��?�
x���;
1@�s��������Zx���� l��(���v�y��ZJ�@8lz�~�� �����L��5 ��I�8M��}� ����/8�q8�
�ek9���!��8آը�o��U]�g^`m�I�o�R w��� ���@�
x�+)JMU047c040031QHI-�ɯ�/�,ɈO-�H-*�+)fH���u����N��^��w�`�P��d R.�j�t���]o�<���C�m�x8T9�Lݜ�$�B���{.�}���`����b�gʡ
Af��� �\��-�(l��g��'u���I5
x�+)JMU0�d040031Q� ��N��+��a��VeZ���ڻ�O���b��?/��N
x����N�0�=�)&=A�R�f���D=l�h��'M)]�X��bpû[X<;�9�|��7�5p�+΄��绷ͣQ���[�dF�k��5���$gMkh��"�̯J�L�;O�o\��G����rH^*
�ܗFq�r�-N;���דJ���7���Ub��z`I�`%�� N�A��IzB�J<^�<����0�"�:��ȶ���e0��vw�sR
��X���c��k�<�<K+\.��M�Ut!
x�M��
�@�;�svʢC��ޠ[�\�O��_v7Bd�=3�N���d�3��E�tl=��p��@��Y�6u&�Hj׷9�Br���S9��Ò{���k?�Iq��|�V�p��ΆbåLuQ��;`9|� �ȵ�+HRg_��f$uI%� ��C�
x��V]o�0广�*/���I���2�"44�i�1� r��p�`;����8_mҤ�S��Sz}uν���tM����������aQ�&�>�������df��A��&�@0�},w���i�2L��4� s1�aĸC�B��DB
C%�O�籘� c�1*9�����\� �<eP�<�*p�E�� �z��s$P ����U�صȬqtSObF���%!�1��5��b���k��'�E�$���9^�r�Ֆl��\�-׮���rg�\�-��s���L7���b�!�I�D�$K!��C2�0D����W�N09>�e�Pz�a��
�؅kF2��"�k����!$"��.�EXE2 �*�"�K��C-i �-���v�B� �ļ��ut�)�H=������GG�gc"�r+��K��zNuY�K�k���5�ZR���#��*\��F<UW�K���\e��3��5�hOXn�8���g���<���o���\f�`S�Y������w9�"諧u����KP.����Ws]A��׆건5M]� ����L������tY];�mE��d���l�2<[��=;,ph�j�Y����o����]�I{ꓯ��N.����`����sJ��F AŅ�-œ6��ņ'x�:0��m�~~���t,-#��]��\k³����N���-�I(�@NjB-�9e�:g�D'V���h�Dp5��U�}A-��)Vsؿ�����sɔK9qݨ��
�?�
x�+)JMU01a040031Q� ��N͋/I-.�+��a(�8��^T�������B>�����)�
x����
�0 �=�)F�2ă��-D��xh�L��)m&�ػ�6�1�9%��|��"�m�E�M1|��2����?�U���
�<��+��%� I�m>\�R�Y�to�t���E�oF������^p�`�pR�J$������hx��8��~������N񯌇���=ꐶ������5��/�̢�
x�mT_k�0߳?�= �Tn(c`�n�Ơ���>�a "�G�-InR��d��F ��~�t�V9̯�߉f�����_�Z����Qx��JZ� ;�P��h7�����
%t��.��GQz~�9|�m��`7�xA C� ~�r�8����1���tj��C���3N� ������m\Wja������*�OF�ڜ����P���,�����>�KC�T�,�&G�Aō�բ}am�%B����h'����~{�!n���, �v#�������䙷��@F詷v��#E�����nn�ED�ё�ĝ���=�E��Y�FV�*Y�I��KMSX*K��p�f
-����4�����6�؀���D�-����?�f+j�R�V��������z2 �����ACwx�K��n��U��ΠNF��)��g����6$^rˉ���Ò�@L�� ��
^�I�&w�\�
u<��B���ij͜��� 7�Ν��b^�`2 >�qU!��`��\��p;���x�RX?Ɂ��+���_��X��ʟ�� >���m��g~��]7�'���=sY�X��s ���d�輽�]�u���-��NRp���]�(@���L�]Q���p
2ec7094b7a35b129d5b048d2ab2c7869eb451465
{
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false
}
},
{
"files": "*.yml",
"options": {}
},
{
"files": "*.yaml",
"options": {}
},
{
"files": "*.toml",
"options": {}
},
{
"files": "*.json",
"options": {}
},
{
"files": "*.js",
"options": {}
},
{
"files": "*.ts",
"options": {}
}
]
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, Ownable {
constructor() ERC20("MyToken", "MTK") {}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
import { deploy } from './ethers-lib'
(async () => {
try {
const result = await deploy('MyToken', [])
console.log(`address: ${result.address}`)
} catch (e) {
console.log(e.message)
}
})()
import { deploy } from './web3-lib'
(async () => {
try {
const result = await deploy('MyToken', [])
console.log(`address: ${result.address}`)
} catch (e) {
console.log(e.message)
}
})()
import { ethers } from 'ethers'
/**
* Deploy the given contract
* @param {string} contractName name of the contract to deploy
* @param {Array<any>} args list of constructor' parameters
* @param {Number} accountIndex account index from the exposed account
* @return {Contract} deployed contract
*/
export const deploy = async (contractName: string, args: Array<any>, accountIndex?: number): Promise<ethers.Contract> => {
console.log(`deploying ${contractName}`)
// Note that the script needs the ABI which is generated from the compilation artifact.
// Make sure contract is compiled and artifacts are generated
const artifactsPath = `browser/contracts/artifacts/${contractName}.json` // Change this for different path
const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath))
// 'web3Provider' is a remix global variable object
const signer = (new ethers.providers.Web3Provider(web3Provider)).getSigner(accountIndex)
const factory = new ethers.ContractFactory(metadata.abi, metadata.data.bytecode.object, signer)
const contract = await factory.deploy(...args)
// The contract is NOT deployed yet; we must wait until it is mined
await contract.deployed()
return contract
}
import Web3 from 'web3'
import { Contract, ContractSendMethod, Options } from 'web3-eth-contract'
/**
* Deploy the given contract
* @param {string} contractName name of the contract to deploy
* @param {Array<any>} args list of constructor' parameters
* @param {string} from account used to send the transaction
* @param {number} gas gas limit
* @return {Options} deployed contract
*/
export const deploy = async (contractName: string, args: Array<any>, from?: string, gas?: number): Promise<Options> => {
const web3 = new Web3(web3Provider)
console.log(`deploying ${contractName}`)
// Note that the script needs the ABI which is generated from the compilation artifact.
// Make sure contract is compiled and artifacts are generated
const artifactsPath = `browser/contracts/artifacts/${contractName}.json`
const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath))
const accounts = await web3.eth.getAccounts()
const contract: Contract = new web3.eth.Contract(metadata.abi)
const contractSend: ContractSendMethod = contract.deploy({
data: metadata.data.bytecode.object,
arguments: args
})
const newContractInstance = await contractSend.send({
from: from || accounts[0],
gas: gas || 1500000
})
return newContractInstance.options
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "remix_tests.sol";
import "remix_accounts.sol";
import "../contracts/MyToken.sol";
contract MyTokenTest is MyToken {
address acc0;
address acc1;
address acc2;
address acc3;
address acc4;
function beforeAll() public {
acc0 = TestsAccounts.getAccount(0);
acc1 = TestsAccounts.getAccount(1);
acc2 = TestsAccounts.getAccount(2);
acc3 = TestsAccounts.getAccount(3);
acc4 = TestsAccounts.getAccount(4);
}
function testTokenInitialValues() public {
Assert.equal(name(), "MyToken", "token name did not match");
Assert.equal(symbol(), "MTK", "token symbol did not match");
Assert.equal(decimals(), 18, "token decimals did not match");
Assert.equal(totalSupply(), 0, "token supply should be zero");
}
function testTokenMinting() public {
Assert.equal(balanceOf(acc0), 0, "token balance should be zero initially");
mint(acc0, 10000);
Assert.equal(balanceOf(acc0), 10000, "token balance did not match");
}
function testTotalSupply() public {
Assert.equal(totalSupply(), 10000, "total supply did not match");
}
/// #sender: account-1
function failTestTokenMintingWithWrongOwner() public {
Assert.equal(balanceOf(acc0), 0, "token balance should be zero initially");
mint(acc0, 10000);
Assert.equal(balanceOf(acc0), 10000, "token balance did not match");
}
function failTestTokenMintingForZeroAddress() public {
mint(address(0), 10000);
}
function testTokenTransfer() public {
Assert.equal(balanceOf(acc1), 0, "token balance should be zero initially");
transfer(acc1, 500);
Assert.equal(balanceOf(acc0), 9500, "token balance did not match");
Assert.equal(balanceOf(acc1), 500, "token balance did not match");
}
/// #sender: account-1
function testTokenTransferToOtherAddress() public {
Assert.equal(balanceOf(acc1), 500, "acc1 token balance did not match");
transfer(acc2, 100);
Assert.equal(balanceOf(acc1), 400, "acc1 token balance did not match");
Assert.equal(balanceOf(acc2), 100, "acc2 token balance did not match");
}
function failTestTokenTransferToZeroAddress() public {
transfer(address(0), 100);
}
/// #sender: account-2
function failTestTokenTransferMoreThanBalance() public {
transfer(acc3, 110);
}
function testTokenApprove() public {
Assert.equal(allowance(acc0, acc3), 0, "token allowance should be zero initially");
approve(acc3, 500);
Assert.equal(allowance(acc0, acc3), 500, "token allowance did not match");
}
function failTestTokenApproveForZeroSpenderAddress() public {
approve(address(0), 500);
}
/// #sender: account-3
function testTokenTransferfrom() public {
Assert.equal(allowance(acc0, acc3), 500, "token allowance did not match");
transferFrom(acc0, acc4, 400);
Assert.equal(balanceOf(acc4), 400, "acc4 token balance did not match");
Assert.equal(allowance(acc0, acc3), 100, "token allowance did not match");
}
/// #sender: account-3
function failTestTokenTransferfromForMoreThanAllowance() public {
transferFrom(acc0, acc4, 110);
}
/// #sender: account-3
function failTestTokenTransferfromForZeroToAddress() public {
transferFrom(acc0, address(0), 100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment