Skip to content

Commit

Permalink
fixed erc20 constructor and corrected test as appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
laurencevs committed Aug 22, 2019
1 parent f949fb1 commit 50a05cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
23 changes: 11 additions & 12 deletions example/erc20/erc20_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ if (logSteps) {
});
}

async function init(caller_address, total_supply) {
const calldata = [{index: 0, value: new BN(total_supply), len: 32},];
const initialMemory = [], inputStack = [], callvalue = 0;
async function init(caller_address) {
const initialMemory = [], inputStack = [], calldata = [], callvalue = 0;
const callerAddr = caller_address;
let data = await main(vm, 'ERC20', inputStack, initialMemory, calldata, callvalue, callerAddr);
console.log(`*** Initialised ERC20 with balance ${total_supply} at address 0x${caller_address.toString(16)}.`);
console.log(`*** Initialised ERC20 with tokens at address 0x${caller_address.toString(16)}.`);
if (showGasCost) {console.log(`Gas cost when executing macro = ${data.gas}.`);}
}

Expand Down Expand Up @@ -93,19 +92,19 @@ async function transfer_from(caller_address, token_owner, recipient, amount) {
/*
Examples
init(20, 1000); // Initialises ERC20 with 1000 tokens at address 20
get_total_supply(); // Self-explanatory
init(A); // Initialises ERC20 with all tokens at address A
get_total_supply(); // Returns the number of tokens in circulation
get_balance_of(20); // Gets balance at address 20
transfer(20, 30, 100); // Transfers 100 tokens from 20 to 30
get_balance_of(A); // Gets balance at address A
transfer(A, B, 100); // Transfers 100 tokens from A to B
get_allowance(20, 40); // Gets allowance given to 40 by 20
approve(20, 40, 100); // Approves 100 of 20's tokens for 40
transfer_from(20, 40, 50); // Transfers 50 of 20's tokens approved for 40 to 40
get_allowance(A, C); // Gets allowance given to C by A
approve(A, C, 100); // Approves 100 of A's tokens for C
transfer_from(A, C, 50); // Transfers 50 of A's tokens approved for C to C
*/

async function runMainLoop() {
await init(0xabba, 20000);
await init(0xabba);
await get_total_supply();

await transfer(0xabba, 0xade1e, 1000);
Expand Down
41 changes: 15 additions & 26 deletions example/erc20/huff_modules/erc20.huff
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#define macro BALANCE_LOCATION = takes(0) returns(1) {
0x00 // do not change!
}
Expand All @@ -8,26 +6,12 @@
0x01
}

#define macro SUPPLY_LOCATION = takes(0) returns(1) {
0x02
}

#define macro ALLOWANCE_LOCATION = takes(0) returns(1) {
0x03
}

template <error_location>
#define macro MATH__ADD = takes(2) returns(1) {
dup2 add // (a+b) b
dup1 swap2 gt // (b > (a+b)) (a+b)
<error_location> jumpi
0x02
}

template <error_location>
#define macro MATH__SUB = takes(2) returns(1) {
dup1 dup3 gt // (b>a) a b
<error_location> jumpi
sub
#define macro SUPPLY = takes(0) returns(1) {
0x19000000000 // ~1.7 trillion
}

#define macro ADDRESS_MASK = takes(1) returns(1) {
Expand Down Expand Up @@ -55,15 +39,20 @@ template <error_location>
}

#define macro ERC20 = takes(0) returns(0) {
// 1. store the total_supply value
0x00 calldataload dup1 SUPPLY_LOCATION() sstore
// 1. load transfer event arguments
caller 0 TRANSFER_EVENT_SIGNATURE() 0x20 0x00 SUPPLY()
// stack: total_supply 0x00 0x20 signature from to
// 2. store the owner's address
caller dup1 OWNER_LOCATION() sstore
// stack: caller total_supply
caller OWNER_LOCATION() sstore
// stack: total_supply 0x00 0x20 signature from to
// 3. give the owner a balance with value total_supply
0x00 mstore
0x40 0x00 sha3 // key(caller balance) total_supply
dup1
caller 0x00 mstore
0x40 0x00 sha3 // key(balances[caller]) total_supply total_supply 0x00 0x20 signature from to
sstore
// stack: value 0x00 0x20 signature from to
// 4. emit Transfer event
0x00 mstore log3
}

template <transfer, transfer_from, balance_of, allowance, approve, total_supply>
Expand Down Expand Up @@ -238,7 +227,7 @@ template <error_location>
}

#define macro ERC20__TOTAL_SUPPLY = takes(0) returns(0) {
SUPPLY_LOCATION() sload
SUPPLY()
0x00 mstore
0x20 0x00 return
}
Expand Down

0 comments on commit 50a05cd

Please sign in to comment.