Skip to content

Commit

Permalink
up abigen
Browse files Browse the repository at this point in the history
  • Loading branch information
geraldb committed Jan 9, 2023
1 parent a5f235c commit 0ed3f53
Show file tree
Hide file tree
Showing 12 changed files with 533 additions and 0 deletions.
58 changes: 58 additions & 0 deletions abigen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/test/tmp/
/test/version_tmp/
/tmp/

## Specific to RubyMotion:
.dat*
.repl_history
build/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalisation:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
.ruby-version
.ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# Gladiator (Glimmer Editor)
.gladiator
.DS_Store



####
# exclude all tmp & tmp2 and o directory (in all levels)
tmp/
tmp2/
o/

########
# exclude all dbs e.g. artbase.db etc.
*.db

######
# exclude all zips (in /dl)
*.zip



99 changes: 99 additions & 0 deletions abigen/lib/abigen.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require 'abiparser'


## our own code
## require_relative 'abidoc/version' # note: let version always go first



module ABI
class Contract


def generate_code( name: 'Contract', address: nil )
buf = ''
buf << "#########################\n"
buf << "# #{name} contract / (blockchain) services / function calls\n"
buf << "# auto-generated via abigen (see https://rubygems.org/gems/abigen) on #{Time.now.utc}\n"
buf << "# - #{query_functions.size} query functions(s)\n\n"


buf << "class #{name} < Ethlite::Contract\n\n"

if address
buf << " address "
buf << %Q{"#{address}"}
buf << "\n"
end


if query_functions.size > 0
buf << "\n"
query_functions.each do |func|
buf << "# #{func.doc} _readonly_\n"

buf << %Q{sig "#{func.name}"}

if func.inputs.size > 0
quoted_types = func.inputs.map {|param| %Q{"#{param.sig}"} }
buf << ", inputs: [#{quoted_types.join(',')}]"
end

if func.outputs.size > 0
quoted_types = func.outputs.map {|param| %Q{"#{param.sig}"} }
buf << ", outputs: [#{quoted_types.join(',')}]"
end
buf << "\n"

buf << "def #{func.name}("

func.inputs.each_with_index do |param,i|
buf << ", " if i > 0
if param.name
buf << "#{param.name}"
else
buf << "arg#{i}"
end
end

buf << ")\n"

buf << " do_call("
buf << %Q{"#{func.name}"}

func.inputs.each_with_index do |param,i|
buf << ", "
if param.name
buf << "#{param.name}"
else
buf << "arg#{i}"
end
end

buf << ")\n"

buf << "end\n"
buf << "\n"
end
end


### todo: add (pure) helper functions too!!!
=begin
if helper_functions.size > 0
buf << "\n"
buf << "# #{helper_functions.size} Helper Functions(s)\n\n"
helper_functions.each do |func|
buf << "# - #{func.doc}\n"
## buf << " - sig #{func.sig} => 0x#{sig(func.sig).hexdigest}\n"
end
end
=end

buf << "end ## class #{name}\n"
buf << "\n"
buf
end

end ## class Contract
end ## module ABI
25 changes: 25 additions & 0 deletions abigen/sandbox/test_gen.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
###
# to run use
# ruby -I ./lib sandbox/test_gen.rb

require 'abigen'

punks_v1 = '0x6ba6f2207e343923ba692e5cae646fb0f566db8d'

abi = ABI.read( "../test/address/#{punks_v1}.json" )
pp abi

buf = abi.generate_code( name: 'PunksV1' )
write_text( "./tmp/punks_v1.rb", buf )



punk_blocks = '0x58e90596c2065befd3060767736c829c18f3474c'

abi = ABI.read( "../test/address/#{punk_blocks}.json" )
pp abi

buf = abi.generate_code( name: 'PunkBlocks' )
write_text( "./tmp/punk_blocks.rb", buf )

puts "bye"
61 changes: 61 additions & 0 deletions ethlite-contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
*.gem
*.rbc
/.config
/coverage/
/InstalledFiles
/pkg/
/spec/reports/
/test/tmp/
/test/version_tmp/
/tmp/

## Specific to RubyMotion:
.dat*
.repl_history
build/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalisation:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
.ruby-version
.ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# Gladiator (Glimmer Editor)
.gladiator
.DS_Store



####
# exclude all tmp & tmp2 and o directory (in all levels)
tmp/
tmp2/
o/

########
# exclude all dbs e.g. artbase.db etc.
*.db

######
# exclude all zips (in /dl)
*.zip


# do NOT check in dev/test enviroment variables in .env (dot env files)
.env


17 changes: 17 additions & 0 deletions ethlite-contracts/lib/ethlite-contracts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## 3rd party gems
require 'ethlite'




## out own code

# shared base contract machinery - keep here
## or rename to Base or such - why? why not?
require_relative 'ethlite/contract'

### generated contracts via abigen
require_relative 'ethlite/contracts/punks_v1'
require_relative 'ethlite/contracts/punk_blocks'


53 changes: 53 additions & 0 deletions ethlite-contracts/lib/ethlite/contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@


module Ethlite

class Contract


def self.at( address )
puts " creating new contract #{self.name} @ #{address}"
new( address )
end


def self.sig( name, inputs: [], outputs: [] )
@methods ||= {}
@methods[ name ] = ContractMethod.new( name,
inputs: inputs,
outputs: outputs )
@methods
end
def self.methods()
@methods ||= {}
@methods
end



def self.address( address )
@address = address
@address
end

def self.default_address()
defined?( @address ) ? @address : nil
end

def initialize( address = self.class.default_address )
@address = address
end


def do_call( name, *args )
puts "==> calling #{self.class.name}##{name} with args:"
pp args
method = self.class.methods[ name ]
## pp m
method.do_call( Ethlite.config.rpc, @address, args )
end

end ## class Contract


end ## module Ethlite
1 change: 1 addition & 0 deletions ethlite-contracts/lib/ethlite/contracts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative '../ethlite-contracts' ## lets you use require 'ethlite/contracts' too
59 changes: 59 additions & 0 deletions ethlite-contracts/lib/ethlite/contracts/punk_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#########################
# PunkBlocks contract / (blockchain) services / function calls
# auto-generated via abigen (see https://rubygems.org/gems/abigen) on 2023-01-09 17:48:57 UTC
# - 8 query functions(s)

class PunkBlocks < Ethlite::Contract

address "0x58e90596c2065befd3060767736c829c18f3474c"

# function **blocks**(bytes32 _) ⇒ (enum PunkBlocks.Layer layer, bytes dataMale, bytes dataFemale) _readonly_
sig "blocks", inputs: ["bytes32"], outputs: ["uint8","bytes","bytes"]
def blocks(arg0)
do_call("blocks", arg0)
end

# function **getBlocks**(uint256 _fromID, uint256 _count) ⇒ (struct PunkBlocks.Block[] _, uint256 _) _readonly_
sig "getBlocks", inputs: ["uint256","uint256"], outputs: ["(uint8,bytes,bytes)[]","uint256"]
def getBlocks(_fromID, _count)
do_call("getBlocks", _fromID, _count)
end

# function **index**(uint256 _) ⇒ (bytes32 _) _readonly_
sig "index", inputs: ["uint256"], outputs: ["bytes32"]
def index(arg0)
do_call("index", arg0)
end

# function **nextId**() ⇒ (uint256 _) _readonly_
sig "nextId", outputs: ["uint256"]
def nextId()
do_call("nextId")
end

# function **svgFromIDs**(uint256[] _ids) ⇒ (string _) _readonly_
sig "svgFromIDs", inputs: ["uint256[]"], outputs: ["string"]
def svgFromIDs(_ids)
do_call("svgFromIDs", _ids)
end

# function **svgFromKeys**(bytes32[] _attributeKeys) ⇒ (string _) _readonly_
sig "svgFromKeys", inputs: ["bytes32[]"], outputs: ["string"]
def svgFromKeys(_attributeKeys)
do_call("svgFromKeys", _attributeKeys)
end

# function **svgFromNames**(string[] _attributeNames) ⇒ (string _) _readonly_
sig "svgFromNames", inputs: ["string[]"], outputs: ["string"]
def svgFromNames(_attributeNames)
do_call("svgFromNames", _attributeNames)
end

# function **svgFromPunkID**(uint256 _tokenID) ⇒ (string _) _readonly_
sig "svgFromPunkID", inputs: ["uint256"], outputs: ["string"]
def svgFromPunkID(_tokenID)
do_call("svgFromPunkID", _tokenID)
end

end ## class PunkBlocks

Loading

0 comments on commit 0ed3f53

Please sign in to comment.