From 0ed3f539e14b11afc220fb200db05c5a98519303 Mon Sep 17 00:00:00 2001 From: Gerald Bauer Date: Mon, 9 Jan 2023 18:52:46 +0100 Subject: [PATCH] up abigen --- abigen/.gitignore | 58 +++++++++++ abigen/lib/abigen.rb | 99 +++++++++++++++++++ abigen/sandbox/test_gen.rb | 25 +++++ ethlite-contracts/.gitignore | 61 ++++++++++++ ethlite-contracts/lib/ethlite-contracts.rb | 17 ++++ ethlite-contracts/lib/ethlite/contract.rb | 53 ++++++++++ ethlite-contracts/lib/ethlite/contracts.rb | 1 + .../lib/ethlite/contracts/punk_blocks.rb | 59 +++++++++++ .../lib/ethlite/contracts/punks_v1.rb | 95 ++++++++++++++++++ ethlite-contracts/sandbox/generate.rb | 30 ++++++ ethlite-contracts/sandbox/test_call.rb | 33 +++++++ ethlite/.gitignore | 2 + 12 files changed, 533 insertions(+) create mode 100644 abigen/.gitignore create mode 100644 abigen/lib/abigen.rb create mode 100644 abigen/sandbox/test_gen.rb create mode 100644 ethlite-contracts/.gitignore create mode 100644 ethlite-contracts/lib/ethlite-contracts.rb create mode 100644 ethlite-contracts/lib/ethlite/contract.rb create mode 100644 ethlite-contracts/lib/ethlite/contracts.rb create mode 100644 ethlite-contracts/lib/ethlite/contracts/punk_blocks.rb create mode 100644 ethlite-contracts/lib/ethlite/contracts/punks_v1.rb create mode 100644 ethlite-contracts/sandbox/generate.rb create mode 100644 ethlite-contracts/sandbox/test_call.rb diff --git a/abigen/.gitignore b/abigen/.gitignore new file mode 100644 index 0000000..497c6e2 --- /dev/null +++ b/abigen/.gitignore @@ -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 + + + diff --git a/abigen/lib/abigen.rb b/abigen/lib/abigen.rb new file mode 100644 index 0000000..b908145 --- /dev/null +++ b/abigen/lib/abigen.rb @@ -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 diff --git a/abigen/sandbox/test_gen.rb b/abigen/sandbox/test_gen.rb new file mode 100644 index 0000000..253f784 --- /dev/null +++ b/abigen/sandbox/test_gen.rb @@ -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" diff --git a/ethlite-contracts/.gitignore b/ethlite-contracts/.gitignore new file mode 100644 index 0000000..12f84f0 --- /dev/null +++ b/ethlite-contracts/.gitignore @@ -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 + + diff --git a/ethlite-contracts/lib/ethlite-contracts.rb b/ethlite-contracts/lib/ethlite-contracts.rb new file mode 100644 index 0000000..d512835 --- /dev/null +++ b/ethlite-contracts/lib/ethlite-contracts.rb @@ -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' + + diff --git a/ethlite-contracts/lib/ethlite/contract.rb b/ethlite-contracts/lib/ethlite/contract.rb new file mode 100644 index 0000000..e45cdde --- /dev/null +++ b/ethlite-contracts/lib/ethlite/contract.rb @@ -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 \ No newline at end of file diff --git a/ethlite-contracts/lib/ethlite/contracts.rb b/ethlite-contracts/lib/ethlite/contracts.rb new file mode 100644 index 0000000..e3714fd --- /dev/null +++ b/ethlite-contracts/lib/ethlite/contracts.rb @@ -0,0 +1 @@ +require_relative '../ethlite-contracts' ## lets you use require 'ethlite/contracts' too diff --git a/ethlite-contracts/lib/ethlite/contracts/punk_blocks.rb b/ethlite-contracts/lib/ethlite/contracts/punk_blocks.rb new file mode 100644 index 0000000..e1484b5 --- /dev/null +++ b/ethlite-contracts/lib/ethlite/contracts/punk_blocks.rb @@ -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 + diff --git a/ethlite-contracts/lib/ethlite/contracts/punks_v1.rb b/ethlite-contracts/lib/ethlite/contracts/punks_v1.rb new file mode 100644 index 0000000..d32a828 --- /dev/null +++ b/ethlite-contracts/lib/ethlite/contracts/punks_v1.rb @@ -0,0 +1,95 @@ +######################### +# PunksV1 contract / (blockchain) services / function calls +# auto-generated via abigen (see https://rubygems.org/gems/abigen) on 2023-01-09 17:48:57 UTC +# - 14 query functions(s) + +class PunksV1 < Ethlite::Contract + + address "0x6ba6f2207e343923ba692e5cae646fb0f566db8d" + +# function **name**() ⇒ (string _) _readonly_ +sig "name", outputs: ["string"] +def name() + do_call("name") +end + +# function **punksOfferedForSale**(uint256 _) ⇒ (bool isForSale, uint256 punkIndex, address seller, uint256 minValue, address onlySellTo) _readonly_ +sig "punksOfferedForSale", inputs: ["uint256"], outputs: ["bool","uint256","address","uint256","address"] +def punksOfferedForSale(arg0) + do_call("punksOfferedForSale", arg0) +end + +# function **totalSupply**() ⇒ (uint256 _) _readonly_ +sig "totalSupply", outputs: ["uint256"] +def totalSupply() + do_call("totalSupply") +end + +# function **decimals**() ⇒ (uint8 _) _readonly_ +sig "decimals", outputs: ["uint8"] +def decimals() + do_call("decimals") +end + +# function **imageHash**() ⇒ (string _) _readonly_ +sig "imageHash", outputs: ["string"] +def imageHash() + do_call("imageHash") +end + +# function **nextPunkIndexToAssign**() ⇒ (uint256 _) _readonly_ +sig "nextPunkIndexToAssign", outputs: ["uint256"] +def nextPunkIndexToAssign() + do_call("nextPunkIndexToAssign") +end + +# function **punkIndexToAddress**(uint256 _) ⇒ (address _) _readonly_ +sig "punkIndexToAddress", inputs: ["uint256"], outputs: ["address"] +def punkIndexToAddress(arg0) + do_call("punkIndexToAddress", arg0) +end + +# function **standard**() ⇒ (string _) _readonly_ +sig "standard", outputs: ["string"] +def standard() + do_call("standard") +end + +# function **balanceOf**(address _) ⇒ (uint256 _) _readonly_ +sig "balanceOf", inputs: ["address"], outputs: ["uint256"] +def balanceOf(arg0) + do_call("balanceOf", arg0) +end + +# function **symbol**() ⇒ (string _) _readonly_ +sig "symbol", outputs: ["string"] +def symbol() + do_call("symbol") +end + +# function **numberOfPunksToReserve**() ⇒ (uint256 _) _readonly_ +sig "numberOfPunksToReserve", outputs: ["uint256"] +def numberOfPunksToReserve() + do_call("numberOfPunksToReserve") +end + +# function **numberOfPunksReserved**() ⇒ (uint256 _) _readonly_ +sig "numberOfPunksReserved", outputs: ["uint256"] +def numberOfPunksReserved() + do_call("numberOfPunksReserved") +end + +# function **punksRemainingToAssign**() ⇒ (uint256 _) _readonly_ +sig "punksRemainingToAssign", outputs: ["uint256"] +def punksRemainingToAssign() + do_call("punksRemainingToAssign") +end + +# function **pendingWithdrawals**(address _) ⇒ (uint256 _) _readonly_ +sig "pendingWithdrawals", inputs: ["address"], outputs: ["uint256"] +def pendingWithdrawals(arg0) + do_call("pendingWithdrawals", arg0) +end + +end ## class PunksV1 + diff --git a/ethlite-contracts/sandbox/generate.rb b/ethlite-contracts/sandbox/generate.rb new file mode 100644 index 0000000..a7b7955 --- /dev/null +++ b/ethlite-contracts/sandbox/generate.rb @@ -0,0 +1,30 @@ +### +# to run use +# ruby sandbox/generate.rb + +$LOAD_PATH.unshift( "../abigen/lib" ) +require 'abigen' + +punks_v1 = '0x6ba6f2207e343923ba692e5cae646fb0f566db8d' + +abi = ABI.read( "../test/address/#{punks_v1}.json" ) +pp abi + + +buf = abi.generate_code( name: 'PunksV1', + address: punks_v1 ) +write_text( "./lib/ethlite/contracts/punks_v1.rb", buf ) + + + + +punk_blocks = '0x58e90596c2065befd3060767736c829c18f3474c' + +abi = ABI.read( "../test/address/#{punk_blocks}.json" ) +pp abi + +buf = abi.generate_code( name: 'PunkBlocks', + address: punk_blocks ) +write_text( "./lib/ethlite/contracts/punk_blocks.rb", buf ) + +puts "bye" diff --git a/ethlite-contracts/sandbox/test_call.rb b/ethlite-contracts/sandbox/test_call.rb new file mode 100644 index 0000000..dd110e0 --- /dev/null +++ b/ethlite-contracts/sandbox/test_call.rb @@ -0,0 +1,33 @@ +### +# to run use +# ruby -I ./lib sandbox/test_call.rb + + + +require 'ethlite/contracts' + + + + +punksv1 = PunksV1.new ## note: will use default (eth main network) contract address + + +pp punksv1 + + +pp punksv1.name +pp punksv1.symbol +pp punksv1.numberOfPunksReserved +pp punksv1.totalSupply +pp punksv1.imageHash +pp punksv1.balanceOf( '0x0000000000000000000000000000000000000000' ) + + +punkblocks = PunkBlocks.new ## note: will use default (eth main network) contract address + + +pp punkblocks.getBlocks( 0, 1 ) ## _fromID, _count + + + +puts "bye" diff --git a/ethlite/.gitignore b/ethlite/.gitignore index 497c6e2..119a46c 100644 --- a/ethlite/.gitignore +++ b/ethlite/.gitignore @@ -55,4 +55,6 @@ o/ *.zip +# do NOT check in dev/test enviroment variables in .env (dot env files) +.env