Skip to content

Commit 0ed3f53

Browse files
committed
up abigen
1 parent a5f235c commit 0ed3f53

File tree

12 files changed

+533
-0
lines changed

12 files changed

+533
-0
lines changed

abigen/.gitignore

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
*.gem
2+
*.rbc
3+
/.config
4+
/coverage/
5+
/InstalledFiles
6+
/pkg/
7+
/spec/reports/
8+
/test/tmp/
9+
/test/version_tmp/
10+
/tmp/
11+
12+
## Specific to RubyMotion:
13+
.dat*
14+
.repl_history
15+
build/
16+
17+
## Documentation cache and generated files:
18+
/.yardoc/
19+
/_yardoc/
20+
/doc/
21+
/rdoc/
22+
23+
## Environment normalisation:
24+
/.bundle/
25+
/vendor/bundle
26+
/lib/bundler/man/
27+
28+
# for a library or gem, you might want to ignore these files since the code is
29+
# intended to run in multiple environments; otherwise, check them in:
30+
# Gemfile.lock
31+
.ruby-version
32+
.ruby-gemset
33+
34+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35+
.rvmrc
36+
37+
# Gladiator (Glimmer Editor)
38+
.gladiator
39+
.DS_Store
40+
41+
42+
43+
####
44+
# exclude all tmp & tmp2 and o directory (in all levels)
45+
tmp/
46+
tmp2/
47+
o/
48+
49+
########
50+
# exclude all dbs e.g. artbase.db etc.
51+
*.db
52+
53+
######
54+
# exclude all zips (in /dl)
55+
*.zip
56+
57+
58+

abigen/lib/abigen.rb

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
require 'abiparser'
2+
3+
4+
## our own code
5+
## require_relative 'abidoc/version' # note: let version always go first
6+
7+
8+
9+
module ABI
10+
class Contract
11+
12+
13+
def generate_code( name: 'Contract', address: nil )
14+
buf = ''
15+
buf << "#########################\n"
16+
buf << "# #{name} contract / (blockchain) services / function calls\n"
17+
buf << "# auto-generated via abigen (see https://rubygems.org/gems/abigen) on #{Time.now.utc}\n"
18+
buf << "# - #{query_functions.size} query functions(s)\n\n"
19+
20+
21+
buf << "class #{name} < Ethlite::Contract\n\n"
22+
23+
if address
24+
buf << " address "
25+
buf << %Q{"#{address}"}
26+
buf << "\n"
27+
end
28+
29+
30+
if query_functions.size > 0
31+
buf << "\n"
32+
query_functions.each do |func|
33+
buf << "# #{func.doc} _readonly_\n"
34+
35+
buf << %Q{sig "#{func.name}"}
36+
37+
if func.inputs.size > 0
38+
quoted_types = func.inputs.map {|param| %Q{"#{param.sig}"} }
39+
buf << ", inputs: [#{quoted_types.join(',')}]"
40+
end
41+
42+
if func.outputs.size > 0
43+
quoted_types = func.outputs.map {|param| %Q{"#{param.sig}"} }
44+
buf << ", outputs: [#{quoted_types.join(',')}]"
45+
end
46+
buf << "\n"
47+
48+
buf << "def #{func.name}("
49+
50+
func.inputs.each_with_index do |param,i|
51+
buf << ", " if i > 0
52+
if param.name
53+
buf << "#{param.name}"
54+
else
55+
buf << "arg#{i}"
56+
end
57+
end
58+
59+
buf << ")\n"
60+
61+
buf << " do_call("
62+
buf << %Q{"#{func.name}"}
63+
64+
func.inputs.each_with_index do |param,i|
65+
buf << ", "
66+
if param.name
67+
buf << "#{param.name}"
68+
else
69+
buf << "arg#{i}"
70+
end
71+
end
72+
73+
buf << ")\n"
74+
75+
buf << "end\n"
76+
buf << "\n"
77+
end
78+
end
79+
80+
81+
### todo: add (pure) helper functions too!!!
82+
=begin
83+
if helper_functions.size > 0
84+
buf << "\n"
85+
buf << "# #{helper_functions.size} Helper Functions(s)\n\n"
86+
helper_functions.each do |func|
87+
buf << "# - #{func.doc}\n"
88+
## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n"
89+
end
90+
end
91+
=end
92+
93+
buf << "end ## class #{name}\n"
94+
buf << "\n"
95+
buf
96+
end
97+
98+
end ## class Contract
99+
end ## module ABI

abigen/sandbox/test_gen.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
###
2+
# to run use
3+
# ruby -I ./lib sandbox/test_gen.rb
4+
5+
require 'abigen'
6+
7+
punks_v1 = '0x6ba6f2207e343923ba692e5cae646fb0f566db8d'
8+
9+
abi = ABI.read( "../test/address/#{punks_v1}.json" )
10+
pp abi
11+
12+
buf = abi.generate_code( name: 'PunksV1' )
13+
write_text( "./tmp/punks_v1.rb", buf )
14+
15+
16+
17+
punk_blocks = '0x58e90596c2065befd3060767736c829c18f3474c'
18+
19+
abi = ABI.read( "../test/address/#{punk_blocks}.json" )
20+
pp abi
21+
22+
buf = abi.generate_code( name: 'PunkBlocks' )
23+
write_text( "./tmp/punk_blocks.rb", buf )
24+
25+
puts "bye"

ethlite-contracts/.gitignore

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
*.gem
2+
*.rbc
3+
/.config
4+
/coverage/
5+
/InstalledFiles
6+
/pkg/
7+
/spec/reports/
8+
/test/tmp/
9+
/test/version_tmp/
10+
/tmp/
11+
12+
## Specific to RubyMotion:
13+
.dat*
14+
.repl_history
15+
build/
16+
17+
## Documentation cache and generated files:
18+
/.yardoc/
19+
/_yardoc/
20+
/doc/
21+
/rdoc/
22+
23+
## Environment normalisation:
24+
/.bundle/
25+
/vendor/bundle
26+
/lib/bundler/man/
27+
28+
# for a library or gem, you might want to ignore these files since the code is
29+
# intended to run in multiple environments; otherwise, check them in:
30+
# Gemfile.lock
31+
.ruby-version
32+
.ruby-gemset
33+
34+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35+
.rvmrc
36+
37+
# Gladiator (Glimmer Editor)
38+
.gladiator
39+
.DS_Store
40+
41+
42+
43+
####
44+
# exclude all tmp & tmp2 and o directory (in all levels)
45+
tmp/
46+
tmp2/
47+
o/
48+
49+
########
50+
# exclude all dbs e.g. artbase.db etc.
51+
*.db
52+
53+
######
54+
# exclude all zips (in /dl)
55+
*.zip
56+
57+
58+
# do NOT check in dev/test enviroment variables in .env (dot env files)
59+
.env
60+
61+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 3rd party gems
2+
require 'ethlite'
3+
4+
5+
6+
7+
## out own code
8+
9+
# shared base contract machinery - keep here
10+
## or rename to Base or such - why? why not?
11+
require_relative 'ethlite/contract'
12+
13+
### generated contracts via abigen
14+
require_relative 'ethlite/contracts/punks_v1'
15+
require_relative 'ethlite/contracts/punk_blocks'
16+
17+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
3+
module Ethlite
4+
5+
class Contract
6+
7+
8+
def self.at( address )
9+
puts " creating new contract #{self.name} @ #{address}"
10+
new( address )
11+
end
12+
13+
14+
def self.sig( name, inputs: [], outputs: [] )
15+
@methods ||= {}
16+
@methods[ name ] = ContractMethod.new( name,
17+
inputs: inputs,
18+
outputs: outputs )
19+
@methods
20+
end
21+
def self.methods()
22+
@methods ||= {}
23+
@methods
24+
end
25+
26+
27+
28+
def self.address( address )
29+
@address = address
30+
@address
31+
end
32+
33+
def self.default_address()
34+
defined?( @address ) ? @address : nil
35+
end
36+
37+
def initialize( address = self.class.default_address )
38+
@address = address
39+
end
40+
41+
42+
def do_call( name, *args )
43+
puts "==> calling #{self.class.name}##{name} with args:"
44+
pp args
45+
method = self.class.methods[ name ]
46+
## pp m
47+
method.do_call( Ethlite.config.rpc, @address, args )
48+
end
49+
50+
end ## class Contract
51+
52+
53+
end ## module Ethlite
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require_relative '../ethlite-contracts' ## lets you use require 'ethlite/contracts' too
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#########################
2+
# PunkBlocks contract / (blockchain) services / function calls
3+
# auto-generated via abigen (see https://rubygems.org/gems/abigen) on 2023-01-09 17:48:57 UTC
4+
# - 8 query functions(s)
5+
6+
class PunkBlocks < Ethlite::Contract
7+
8+
address "0x58e90596c2065befd3060767736c829c18f3474c"
9+
10+
# function **blocks**(bytes32 _) ⇒ (enum PunkBlocks.Layer layer, bytes dataMale, bytes dataFemale) _readonly_
11+
sig "blocks", inputs: ["bytes32"], outputs: ["uint8","bytes","bytes"]
12+
def blocks(arg0)
13+
do_call("blocks", arg0)
14+
end
15+
16+
# function **getBlocks**(uint256 _fromID, uint256 _count) ⇒ (struct PunkBlocks.Block[] _, uint256 _) _readonly_
17+
sig "getBlocks", inputs: ["uint256","uint256"], outputs: ["(uint8,bytes,bytes)[]","uint256"]
18+
def getBlocks(_fromID, _count)
19+
do_call("getBlocks", _fromID, _count)
20+
end
21+
22+
# function **index**(uint256 _) ⇒ (bytes32 _) _readonly_
23+
sig "index", inputs: ["uint256"], outputs: ["bytes32"]
24+
def index(arg0)
25+
do_call("index", arg0)
26+
end
27+
28+
# function **nextId**() ⇒ (uint256 _) _readonly_
29+
sig "nextId", outputs: ["uint256"]
30+
def nextId()
31+
do_call("nextId")
32+
end
33+
34+
# function **svgFromIDs**(uint256[] _ids) ⇒ (string _) _readonly_
35+
sig "svgFromIDs", inputs: ["uint256[]"], outputs: ["string"]
36+
def svgFromIDs(_ids)
37+
do_call("svgFromIDs", _ids)
38+
end
39+
40+
# function **svgFromKeys**(bytes32[] _attributeKeys) ⇒ (string _) _readonly_
41+
sig "svgFromKeys", inputs: ["bytes32[]"], outputs: ["string"]
42+
def svgFromKeys(_attributeKeys)
43+
do_call("svgFromKeys", _attributeKeys)
44+
end
45+
46+
# function **svgFromNames**(string[] _attributeNames) ⇒ (string _) _readonly_
47+
sig "svgFromNames", inputs: ["string[]"], outputs: ["string"]
48+
def svgFromNames(_attributeNames)
49+
do_call("svgFromNames", _attributeNames)
50+
end
51+
52+
# function **svgFromPunkID**(uint256 _tokenID) ⇒ (string _) _readonly_
53+
sig "svgFromPunkID", inputs: ["uint256"], outputs: ["string"]
54+
def svgFromPunkID(_tokenID)
55+
do_call("svgFromPunkID", _tokenID)
56+
end
57+
58+
end ## class PunkBlocks
59+

0 commit comments

Comments
 (0)