Skip to content

Commit

Permalink
feat: follow and community token uris
Browse files Browse the repository at this point in the history
  • Loading branch information
Darlington02 committed Feb 8, 2025
1 parent fc19826 commit fb51bb9
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 22 deletions.
59 changes: 57 additions & 2 deletions src/base/token_uris/community_token_uri.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
pub mod CommunityTokenUri {
pub fn get_token_uri(token_id: u256, mint_timestamp: u64) -> ByteArray {
"TODO"
use coloniz::base::utils::byte_array_extra::FeltTryIntoByteArray;
use coloniz::base::utils::base64_extended::get_base64_encode;
use coloniz::base::token_uris::traits::community::community::get_svg_community;

pub fn get_token_uri(token_id: u256, community_id: u256, timestamp: u64) -> ByteArray {
let baseuri = 'data:image/svg+xml;base64,';

let mut svg_byte_array: ByteArray = get_svg_community(token_id, community_id);
let mut svg_encoded: ByteArray = get_base64_encode(svg_byte_array);

let mut attribute_byte_array: ByteArray = get_attributes(
token_id, ref svg_encoded, community_id, timestamp
);

let mut token_uri: ByteArray = baseuri.try_into().unwrap();
token_uri.append(@attribute_byte_array);
token_uri
}

fn get_attributes(
token_id: u256, ref svg_encoded_byteArray: ByteArray, community_id: u256, timestamp: u64
) -> ByteArray {
let token_id_to_felt: felt252 = token_id.low.into();
let community_id_to_felt: felt252 = community_id.low.into();
let timestamp_to_felt: felt252 = timestamp.try_into().unwrap();

let mut attributespre: ByteArray = Default::default();
let mut attributespost: ByteArray = Default::default();

attributespre.append(@"{\"name\":\"Community #");
attributespre.append(@format!("{}", community_id_to_felt));
attributespre.append(@"\",\"description\":\" ");
attributespre.append(@"Coloniz - #");
attributespre.append(@format!("{}", token_id_to_felt));
attributespre.append(@" of Community ");
attributespre.append(@format!("{}", community_id_to_felt));
attributespre.append(@"\",\"image\":\"data:image");
attributespre.append(@"/svg+xml;base64,");

//post base64 follow svg
attributespost.append(@"\",\"attributes\":[{\"display");
attributespost.append(@"_type\":\"number\",\"trait_type");
attributespost.append(@"\":\"ID\",\"value\":\"");
attributespost.append(@format!("{}", token_id_to_felt));
attributespost.append(@"\"},{\"trait_type\":\"mint");
attributespost.append(@"_timestamp\",\"value\":\"");
attributespost.append(@format!("{}", timestamp_to_felt));
attributespost.append(@"\"},{\"trait_type\":\"community");
attributespost.append(@"_id\",\"value\":\"");
attributespost.append(@format!("{}", community_id_to_felt));
attributespost.append(@"\"}]}");

attributespre.append(@svg_encoded_byteArray);
attributespre.append(@attributespost);

get_base64_encode(attributespre)
}
}

5 changes: 4 additions & 1 deletion src/base/token_uris/follow_token_uri.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ pub mod FollowTokenUri {
attributespost.append(@"\":\"ID\",\"value\":\"");
attributespost.append(@format!("{}", token_id_felt));
attributespost.append(@"\"},{\"trait_type\":\"mint");
attributespost.append(@"Timestamp\",\"value\":\"");
attributespost.append(@"_timestamp\",\"value\":\"");
attributespost.append(@format!("{}", follow_timestamp_felt));
attributespost.append(@"\"},{\"trait_type\":\"followed");
attributespost.append(@"_profile\",\"value\":\"");
attributespost.append(@format!("{}", followed_profile_address_felt));
attributespost.append(@"\"}]}");

attributespre.append(@svg_encoded_byteArray);
Expand Down
1 change: 1 addition & 0 deletions src/base/token_uris/traits.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod color;
pub mod handle;
pub mod follow;
pub mod community;
4 changes: 2 additions & 2 deletions src/base/token_uris/traits/color.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod colonizColors {
use core::array::ArrayTrait;

pub fn get_random_color(local_name: u256) -> felt252 {
pub fn get_random_color(seed: u256) -> felt252 {
let colors = array![
'#A0D170',
'#FFD2DD',
Expand All @@ -27,7 +27,7 @@ pub mod colonizColors {
];

let len: u256 = colors.len().try_into().unwrap();
let randomIndex: u32 = (local_name % len).try_into().unwrap();
let randomIndex: u32 = (seed % len).try_into().unwrap();

return *colors.at(randomIndex);
}
Expand Down
43 changes: 43 additions & 0 deletions src/base/token_uris/traits/community.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pub mod community {
use coloniz::base::utils::byte_array_extra::FeltTryIntoByteArray;
use coloniz::base::token_uris::traits::color::colonizColors::get_random_color;

pub fn get_svg_community(token_id: u256, community_id: u256) -> ByteArray {
let mut svg: ByteArray = Default::default();

let color_code: ByteArray = get_random_color(token_id).try_into().unwrap();

/// construct the SVG as ByteArray
svg
.append(
@"<svg width=\"300\" height=\"300\" viewBox=\"0 0 300 300\" xmlns=\"http://www.w3.org/2000/svg\">
<rect width=\"300\" height=\"300\" fill=\"white\"/>
<circle cx=\"150\" cy=\"150\" r=\"60\" fill=\""
);

svg.append(@color_code);

svg
.append(
@"\" stroke=\"black\" stroke-width=\"4\"/>
<circle cx=\"90\" cy=\"120\" r=\"15\" fill=\"black\" stroke=\"black\" stroke-width=\"3\"/>
<circle cx=\"210\" cy=\"120\" r=\"15\" fill=\"black\" stroke=\"black\" stroke-width=\"3\"/>
<circle cx=\"120\" cy=\"200\" r=\"15\" fill=\"black\" stroke=\"black\" stroke-width=\"3\"/>
<circle cx=\"180\" cy=\"200\" r=\"15\" fill=\"black\" stroke=\"black\" stroke-width=\"3\"/>
<line x1=\"150\" y1=\"150\" x2=\"90\" y2=\"120\" stroke=\"black\" stroke-width=\"3\"/>
<line x1=\"150\" y1=\"150\" x2=\"210\" y2=\"120\" stroke=\"black\" stroke-width=\"3\"/>
<line x1=\"150\" y1=\"150\" x2=\"120\" y2=\"200\" stroke=\"black\" stroke-width=\"3\"/>
<line x1=\"150\" y1=\"150\" x2=\"180\" y2=\"200\" stroke=\"black\" stroke-width=\"3\"/>
<text x=\"50%\" y=\"270\" font-family=\"Arial\" font-size=\"22\" fill=\"black\" text-anchor=\"middle\" font-weight=\"bold\">COMMUNITY #
"
);

let community_id_to_felt = community_id.into();
svg.append(@format!("{}", community_id_to_felt));

svg.append(@"</text>
</svg>");

svg
}
}
10 changes: 6 additions & 4 deletions src/base/token_uris/traits/follow.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ pub mod follow {

svg.append(@color_code);

svg.append(@"\" stroke=\"black\" stroke-width=\"4\"/>
svg
.append(
@"\" stroke=\"black\" stroke-width=\"4\"/>
<circle cx=\"115\" cy=\"140\" r=\"10\" fill=\"black\"/>
<circle cx=\"185\" cy=\"140\" r=\"10\" fill=\"black\"/>
<circle cx=\"70\" cy=\"150\" r=\"20\" fill=\"none\" stroke=\"white\" stroke-width=\"4\"/>
<circle cx=\"230\" cy=\"150\" r=\"20\" fill=\"none\" stroke=\"white\" stroke-width=\"4\"/>
<line x1=\"90\" y1=\"150\" x2=\"210\" y2=\"150\" stroke=\"white\" stroke-width=\"4\"/>
<text x=\"50%\" y=\"270\" font-family=\"Arial\" font-size=\"24\" fill=\"white\" text-anchor=\"middle\" font-weight=\"bold\">
");
"
);

let token_id_to_felt = token_id.into();
svg.append(@format!("{}", token_id_to_felt));
svg.append(@"Follower");

svg.append(@"</text>
</svg>");
Expand Down
13 changes: 0 additions & 13 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,3 @@ pub mod hub;
pub mod jolt;
pub mod community;
pub mod channel;


use coloniz::base::token_uris::follow_token_uri::FollowTokenUri::get_token_uri;
use starknet::ContractAddress;

fn main() {
let token_id = 232110;
let followed_profile_address: ContractAddress = 525242.try_into().unwrap();
let timestamp: u64 = 267;

let token_uri: ByteArray = get_token_uri(token_id, followed_profile_address, timestamp);
println!("{:?}", token_uri);
}

0 comments on commit fb51bb9

Please sign in to comment.