Skip to content

Commit 5dbfc30

Browse files
committed
simpleToken EVM test passes.
1 parent 701d4a5 commit 5dbfc30

File tree

3 files changed

+280
-0
lines changed

3 files changed

+280
-0
lines changed

src/example_contract/simpleToken.sc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/-
2+
token implementation satisfying the ERC20 standard:
3+
https://eips.ethereum.org/EIPS/eip-20
4+
5+
interface
6+
-/
7+
8+
signature TOKEN{
9+
10+
storage supply : UInt;
11+
12+
map balances : (Address) => UInt;
13+
14+
constructor c : (UInt) -> void;
15+
16+
method initialize : (void) -> UInt;
17+
method set : (Address, UInt) -> UInt;
18+
method totalSupply : (void) -> UInt;
19+
method balanceOf : (Address) -> UInt;
20+
method transfer : (Address, UInt) -> Bool;
21+
}
22+
23+
/- implementation -/
24+
25+
constructor c (s : UInt){
26+
storage
27+
supply |-> s;
28+
balances[Env.sender] |-> s;
29+
returns void;
30+
}
31+
32+
method initialize () {
33+
guard{}
34+
storage{
35+
balances[Env.sender] |-> 100000;
36+
}
37+
effects{}
38+
returns balances[Env.sender];
39+
}
40+
41+
method set (a:Address, v: UInt){
42+
guard{}
43+
storage{
44+
balances[a] |-> v;
45+
}
46+
effects{}
47+
returns balances[a];
48+
}
49+
50+
method totalSupply (){
51+
guard{
52+
Env.value == 0;
53+
}
54+
storage{}
55+
effects{}
56+
returns supply;
57+
}
58+
59+
method balanceOf (a : Address){
60+
guard{
61+
Env.value == 0;
62+
}
63+
storage{}
64+
effects{}
65+
returns balances[a];
66+
}
67+
68+
method transfer (a : Address, v : UInt){
69+
guard{
70+
Env.value == 0;
71+
72+
/- overflow checking -/
73+
balances[Env.sender] >= v;
74+
}
75+
storage{
76+
balances[Env.sender] |-> balances[Env.sender] - v;
77+
balances[a] |-> (balances[a] + v);
78+
}
79+
effects{}
80+
returns True;
81+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env node
2+
3+
// const net = require('net');
4+
// const solc = require('solc');
5+
const fs = require('fs');
6+
const ethers = require("ethers");
7+
8+
if (process.argv.length != 3) {
9+
console.log("invalid args");
10+
process.exit(1);
11+
}
12+
13+
const endpoint = "http://localhost:8545";
14+
const provider = new ethers.providers.JsonRpcProvider(endpoint);
15+
16+
// const abi = [
17+
// "constructor()",
18+
// "function initialize () public returns (uint)",
19+
// "function totalSupply() view returns (uint)",
20+
// "function balanceOf(uint) view returns (uint)",
21+
// "function transfer(uint, int) returns (bool)"
22+
// ];
23+
24+
const abi = [
25+
{"type":"function",
26+
"name":"initialize",
27+
"inputs":[],
28+
"outputs":[{"name":"", "type":"uint256"}],
29+
"payable":"true",
30+
"constant":false,
31+
"stateMutability":"payable"},
32+
{"type":"function",
33+
"name":"set",
34+
"inputs":[{"name":"a", "type":"uint256"},{"name":"v", "type":"uint256"}],
35+
"outputs":[{"name":"", "type":"uint256"}],
36+
"payable":"true",
37+
"constant":false,
38+
"stateMutability":"payable"},
39+
{"type":"function",
40+
"name":"totalSupply",
41+
"inputs":[],
42+
"outputs":[{"name":"", "type":"uint256"}],
43+
"payable":"true",
44+
"constant":true,
45+
"stateMutability":"view"},
46+
{"type":"function",
47+
"name":"balanceOf",
48+
"inputs":[{"name":"a", "type":"uint256"}],
49+
"outputs":[{"name":"", "type":"uint256"}],
50+
"payable":"true",
51+
"constant":true,
52+
"stateMutability":"view"},
53+
{"type":"function",
54+
"name":"transfer",
55+
"inputs":[{"name":"a", "type":"uint256"},{"name":"v", "type":"uint256"}],
56+
"outputs":[{"name":"", "type":"bool"}],
57+
"payable":"true",
58+
"constant":false,
59+
"stateMutability":"payable"}
60+
];
61+
62+
const bytecode = fs.readFileSync(process.argv[2]).toString().replace(/\n|\t|\r| /g, "");
63+
const signer = provider.getSigner(0);
64+
const creator = signer.getAddress();
65+
66+
async function deploy() {
67+
console.log("sending creation transaction...")
68+
let factory = new ethers.ContractFactory(abi, bytecode, signer);
69+
let contract = await factory.deploy();
70+
await contract.deployed();
71+
console.log("contract address: " + contract.address);
72+
console.log("transaction hash: " + contract.deployTransaction.hash);
73+
let deployedBytecode = await provider.getCode(contract.address);
74+
// console.log("deployed bytecode: " + deployedBytecode);
75+
76+
console.log("calling initalize...");
77+
let tx = await contract.initialize();
78+
console.log("transaction hash: " + tx.hash);
79+
80+
let alice = 24; // arbitrary address
81+
console.log("calling set...");
82+
tx = await contract.set(alice, 100);
83+
84+
console.log("calling totalSupply...");
85+
let supply = await contract.totalSupply();
86+
console.log("total supply: " + supply);
87+
88+
console.log("calling balanceof...");
89+
let aliceBalance = await contract.balanceOf(alice);
90+
let creatorBalance = await contract.balanceOf(creator);
91+
console.log("creator balance: " + creatorBalance);
92+
console.log("alice balance: " + aliceBalance);
93+
94+
console.log("calling transfer...");
95+
tx = await contract.transfer(alice, 100);
96+
console.log("transaction hash: " + tx.hash);
97+
98+
aliceBalance = await contract.balanceOf(alice);
99+
creatorBalance = await contract.balanceOf(creator);
100+
console.log("creator balance: " + creatorBalance);
101+
console.log("alice balance: " + aliceBalance);
102+
}
103+
104+
deploy();

src/example_contract/test-token.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env node
2+
3+
// const net = require('net');
4+
// const solc = require('solc');
5+
const fs = require('fs');
6+
const ethers = require("ethers");
7+
8+
if (process.argv.length != 3) {
9+
console.log("invalid args");
10+
process.exit(1);
11+
}
12+
13+
const endpoint = "http://localhost:8545";
14+
const provider = new ethers.providers.JsonRpcProvider(endpoint);
15+
16+
// const abi = [
17+
// "constructor()",
18+
// "function initialize () public returns (uint)",
19+
// "function totalSupply() view returns (uint)",
20+
// "function balanceOf(uint) view returns (uint)",
21+
// "function transfer(uint, int) returns (bool)"
22+
// ];
23+
24+
const abi = [
25+
{"type":"function",
26+
"name":"totalSupply",
27+
"inputs":[],
28+
"outputs":[{"name":"", "type":"uint256"}],
29+
"payable":"true",
30+
"constant":true,
31+
"stateMutability":"view"},
32+
{"type":"function",
33+
"name":"balanceOf",
34+
"inputs":[{"name":"a", "type":"uint256"}],
35+
"outputs":[{"name":"", "type":"uint256"}],
36+
"payable":"true",
37+
"constant":true,
38+
"stateMutability":"view"},
39+
{"type":"function",
40+
"name":"allowance",
41+
"inputs":[{"name":"owner", "type":"uint256"},{"name":"spender", "type":"uint256"}],
42+
"outputs":[{"name":"", "type":"uint256"}],
43+
"payable":"true",
44+
"constant":true,
45+
"stateMutability":"view"},
46+
{"type":"function",
47+
"name":"transfer",
48+
"inputs":[{"name":"a", "type":"uint256"},{"name":"v", "type":"uint256"}],
49+
"outputs":[{"name":"", "type":"bool"}],
50+
"payable":"true",
51+
"constant":false,
52+
"stateMutability":"payable"},
53+
{"type":"function",
54+
"name":"approve",
55+
"inputs":[{"name":"spender", "type":"uint256"},{"name":"v", "type":"uint256"}],
56+
"outputs":[{"name":"", "type":"bool"}],
57+
"payable":"true",
58+
"constant":false,
59+
"stateMutability":"payable"},
60+
{"type":"function",
61+
"name":"transferFrom",
62+
"inputs":[{"name":"from", "type":"uint256"},{"name":"to", "type":"uint256"},{"name":"v", "type":"uint256"}],
63+
"outputs":[{"name":"", "type":"bool"}],
64+
"payable":"true",
65+
"constant":false,
66+
"stateMutability":"payable"},
67+
];
68+
69+
const bytecode = fs.readFileSync(process.argv[2]).toString().replace(/\n|\t|\r| /g, "");
70+
const signer = provider.getSigner(0);
71+
const creator = signer.getAddress();
72+
73+
async function deploy() {
74+
console.log("sending creation transaction...")
75+
let factory = new ethers.ContractFactory(abi, bytecode, signer);
76+
let contract = await factory.deploy();
77+
await contract.deployed();
78+
console.log("contract address: " + contract.address);
79+
console.log("transaction hash: " + contract.deployTransaction.hash);
80+
let deployedBytecode = await provider.getCode(contract.address);
81+
// console.log("deployed bytecode: " + deployedBytecode);
82+
83+
let alice = 24; // arbitrary address
84+
console.log("calling transfer...");
85+
tx = await contract.transfer(alice, 100);
86+
console.log("transaction hash: " + tx.hash);
87+
let supply = await contract.totalSupply();
88+
let aliceBalance = await contract.balanceOf(alice);
89+
let creatorBalance = await contract.balanceOf(creator);
90+
console.log("total supply: " + supply);
91+
console.log("creator balance: " + creatorBalance);
92+
console.log("alice balance: " + aliceBalance);
93+
}
94+
95+
deploy();

0 commit comments

Comments
 (0)