Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
geraldb committed Jan 15, 2023
1 parent bf73acc commit a7ce145
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
83 changes: 81 additions & 2 deletions ethlite-contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,88 @@ ethlite-contracts - ready-to-use (blockchain) contract services / function call



## Usage
## Usage by Example

### Contract №1 - PunksMeta (aka CryptoPunksTokenUri) by 0xTycoon

_The missing tokenURI() for the Punks_


Let's try the `PunksMeta` contract
with the token id 0, that is, punk no. 0:

``` ruby
require 'ethlite/contracts'

contract = PunksMeta.new


# function parseAttributes(uint256 _tokenId) returns (string[8])
#
# parseAttributes returns an array of punk attributes. 8 rows in total
# The first row is the Type, and next seven rows are the attributes.
# The values are fetched form the CryptoPunksData contract and then the
# string is parsed.
# @param _tokenId the punk id
ary = contract.parseAttributes( 0 )
#=> ["Female 2", "Earring", "Blonde Bob", "Green Eye Shadow", "", "", "",


# function getAttributes(uint256 _tokenId) returns (string)
#
# getAttributes calls parseAttributes and returns the result as JSON
# @param _tokenId the punk id
str = contract.getAttributes( 0 )
data = JSON.parse( str )
#=> [{"trait_type"=>"Type", "value"=>"Female 2"},
# {"trait_type"=>"Accessory", "value"=>"Earring"},
# {"trait_type"=>"Accessory", "value"=>"Blonde Bob"},
# {"trait_type"=>"Accessory", "value"=>"Green Eye Shadow"}]


# function tokenURI(uint256 _tokenId) returns (string)
#
# tokenURI gets the metadata about a punk and returns as a JSON
# formatted string, according to the ERC721 schema and market
# recommendations. It also embeds the SVG data.
# The attributes and SVG data are fetched form the CryptoPunksData
# contract, which stores all the CryptoPunks metadata on-chain.
# @param _tokenId the punk id
str = contract.tokenURI( 0 )
if str.start_with?( 'data:application/json;base64,' )
str = str.sub( 'data:application/json;base64,', '' )
## get metadata (base64-encoded)
data = JSON.parse( Base64.decode64( str ) )
#=> {"description"=> "CryptoPunks launched as a fixed set of 10,000 items in mid-2017...",
# "external_url"=>"https://cryptopunks.app/cryptopunks/details/0",
# "image"=> "data:image/svg+xml;base64,...",
# "name"=>"CryptoPunk #0",
# "attributes"=>
# [{"trait_type"=>"Type", "value"=>"Female 2"},
# {"trait_type"=>"Accessory", "value"=>"Earring"},
# {"trait_type"=>"Accessory", "value"=>"Blonde Bob"},
# {"trait_type"=>"Accessory", "value"=>"Green Eye Shadow"}]}

## get image (base64-encoded)
str_image = data.delete( 'image' )
str_image = str_image.sub( 'data:image/svg+xml;base64,', '' )
image = Base64.decode64( str_image )
## cut-off inline leading data:image/svg+xml;utf8, too
image = image.sub( 'data:image/svg+xml;utf8,', '' )

write_json( "punksmeta/punk0.json", data )
write_text( "punksmeta/punk0.svg", image )
else
puts "!! ERROR - expected json base64-encoded; got:"
pp str
exit 1
end
end
```

Note: See [`punksmeta/punk0.json`](punksmeta/punk0.json)
and [`punksmeta/punk0.svg`](punksmeta/punk0.svg) for the saved copies of the data.

To be done



Expand Down
23 changes: 23 additions & 0 deletions ethlite-contracts/punksmeta/punk0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"description": "CryptoPunks launched as a fixed set of 10,000 items in mid-2017 and became one of the inspirations for the ERC-721 standard. They have been featured in places like The New York Times, Christie's of London, Art|Basel Miami, and The PBS NewsHour.",
"external_url": "https://cryptopunks.app/cryptopunks/details/0",
"name": "CryptoPunk #0",
"attributes": [
{
"trait_type": "Type",
"value": "Female 2"
},
{
"trait_type": "Accessory",
"value": "Earring"
},
{
"trait_type": "Accessory",
"value": "Blonde Bob"
},
{
"trait_type": "Accessory",
"value": "Green Eye Shadow"
}
]
}
Loading

0 comments on commit a7ce145

Please sign in to comment.