Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:use TBA V3 for account profile #127

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = ["Karst", "SocialFi", "tokenbound", "cairo", "contracts", "starknet"]
[dependencies]
starknet = "2.8.2"
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.17.0" }
token_bound_accounts= { git = "https://github.com/Starknet-Africa-Edu/TBA", tag = "v0.3.0" }
token_bound_accounts= { git = "https://github.com/horuslabsio/TBA", branch = "v3"}
alexandria_bytes = { git = "https://github.com/keep-starknet-strange/alexandria.git" }

[dev-dependencies]
Expand All @@ -23,5 +23,5 @@ url= "https://starknet-sepolia.public.blastapi.io"

[[target.starknet-contract]]
casm = true
build-external-contracts = ["token_bound_accounts::presets::account::Account"]
build-external-contracts = ["token_bound_accounts::components::presets::account_preset::AccountPreset"]
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
allowed-libfuncs-list.name = "experimental"
4 changes: 3 additions & 1 deletion src/hub/hub.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ pub mod KarstHub {
let dispatcher = IHandleRegistryDispatcher {
contract_address: self.handle_registry_contract_address.read()
};
dispatcher.get_handle(profile_address)

let handle_id = dispatcher.get_handle(profile_address);
handle_id
}

/// @notice returns the full handle of a user
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/IHub.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ pub trait IHub<TState> {
karstnft_contract_address: ContractAddress,
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252
salt: felt252,
chain_id: felt252
) -> ContractAddress;

fn set_profile_metadata_uri(
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/IProfile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub trait IProfile<TState> {
karstnft_contract_address: ContractAddress,
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252
salt: felt252,
chain_id: felt252
) -> ContractAddress;
fn set_profile_metadata_uri(
ref self: TState, profile_address: ContractAddress, metadata_uri: ByteArray
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/IRegistry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub trait IRegistry<TContractState> {
implementation_hash: felt252,
token_contract: ContractAddress,
token_id: u256,
salt: felt252
salt: felt252,
chain_id: felt252
) -> ContractAddress;

// *************************************************************************
Expand Down
3 changes: 2 additions & 1 deletion src/mocks/interfaces/IComposable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub trait IComposable<TState> {
karstnft_contract_address: ContractAddress,
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252
salt: felt252,
chain_id: felt252,
) -> ContractAddress;
fn set_profile_metadata_uri(
ref self: TState, profile_address: ContractAddress, metadata_uri: ByteArray
Expand Down
44 changes: 17 additions & 27 deletions src/mocks/registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ pub mod Registry {
use core::hash::HashStateTrait;
use core::pedersen::PedersenTrait;
use starknet::{
ContractAddress, get_caller_address, syscalls::call_contract_syscall, class_hash::ClassHash,
syscalls::deploy_syscall, SyscallResultTrait,
ContractAddress, get_caller_address, get_contract_address, syscalls::call_contract_syscall,
class_hash::ClassHash, syscalls::deploy_syscall, SyscallResultTrait,
storage::{Map, StorageMapReadAccess, StorageMapWriteAccess}
};
use token_bound_accounts::interfaces::IRegistry::IRegistry;

#[storage]
struct Storage {
registry_deployed_accounts: Map<
(ContractAddress, u256), u8
>, // tracks no. of deployed accounts by registry for an NFT
}
pub struct Storage {}

#[event]
#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -53,27 +49,26 @@ pub mod Registry {
implementation_hash: felt252,
token_contract: ContractAddress,
token_id: u256,
salt: felt252
salt: felt252,
chain_id: felt252
) -> ContractAddress {
let owner = self._get_owner(token_contract, token_id);
assert(owner == get_caller_address(), 'CALLER_IS_NOT_OWNER');

let mut constructor_calldata: Array<felt252> = array![
token_contract.into(), token_id.low.into(), token_id.high.into()
token_contract.into(),
token_id.low.into(),
token_id.high.into(),
get_contract_address().into(),
implementation_hash,
salt
];

let class_hash: ClassHash = implementation_hash.try_into().unwrap();
let result = deploy_syscall(class_hash, salt, constructor_calldata.span(), true);
let (account_address, _) = result.unwrap_syscall();

let new_deployment_index: u8 = self
.registry_deployed_accounts
.read((token_contract, token_id))
+ 1_u8;
self.registry_deployed_accounts.write((token_contract, token_id), new_deployment_index);

self.emit(AccountCreated { account_address, token_contract, token_id, });

account_address
}

Expand All @@ -87,13 +82,17 @@ pub mod Registry {
implementation_hash: felt252,
token_contract: ContractAddress,
token_id: u256,
salt: felt252
salt: felt252,
chain_id: felt252
) -> ContractAddress {
let constructor_calldata_hash = PedersenTrait::new(0)
.update(token_contract.into())
.update(token_id.low.into())
.update(token_id.high.into())
.update(3)
.update(get_contract_address().into())
.update(implementation_hash)
.update(salt)
.update(6)
.finalize();

let prefix: felt252 = 'STARKNET_CONTRACT_ADDRESS';
Expand All @@ -108,15 +107,6 @@ pub mod Registry {

account_address.try_into().unwrap()
}

/// @notice returns the total no. of deployed tokenbound accounts for an NFT by the registry
/// @param token_contract the contract address of the NFT
/// @param token_id the ID of the NFT
fn total_deployed_accounts(
self: @ContractState, token_contract: ContractAddress, token_id: u256
) -> u8 {
self.registry_deployed_accounts.read((token_contract, token_id))
}
}

#[generate_trait]
Expand Down
8 changes: 5 additions & 3 deletions src/profile/profile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ pub mod ProfileComponent {
karstnft_contract_address: ContractAddress,
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252
salt: felt252,
chain_id: felt252,
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
) -> ContractAddress {
// mint karst nft
let recipient = get_caller_address();
Expand All @@ -77,12 +78,13 @@ pub mod ProfileComponent {
}
let token_id = IKarstNFTDispatcher { contract_address: karstnft_contract_address }
.get_user_token_id(recipient);

// create tokenbound account
let profile_address = IRegistryLibraryDispatcher {
class_hash: registry_hash.try_into().unwrap()
}
.create_account(implementation_hash, karstnft_contract_address, token_id, salt);
.create_account(
implementation_hash, karstnft_contract_address, token_id, salt, chain_id
);

// deploy follow nft contract
let mut constructor_calldata: Array<felt252> = array![
Expand Down
7 changes: 5 additions & 2 deletions tests/test_hub.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn __setup__() -> (ContractAddress, ContractAddress, ContractAddress, ContractAd
let registry_class_hash = declare("Registry").unwrap().contract_class();

// declare tokenbound account
let account_class_hash = declare("Account").unwrap().contract_class();
let account_class_hash = declare("AccountPreset").unwrap().contract_class();

// declare follownft
let follow_nft_classhash = declare("Follow").unwrap().contract_class();
Expand All @@ -69,6 +69,7 @@ fn __setup__() -> (ContractAddress, ContractAddress, ContractAddress, ContractAd
nft_contract_address,
(*registry_class_hash.class_hash).into(),
(*account_class_hash.class_hash).into(),
2478,
2478
);
stop_cheat_caller_address(hub_contract_address);
Expand All @@ -79,8 +80,10 @@ fn __setup__() -> (ContractAddress, ContractAddress, ContractAddress, ContractAd
nft_contract_address,
(*registry_class_hash.class_hash).into(),
(*account_class_hash.class_hash).into(),
2478,
2478
);

stop_cheat_caller_address(hub_contract_address);

start_cheat_caller_address(hub_contract_address, ADDRESS3.try_into().unwrap());
Expand All @@ -89,6 +92,7 @@ fn __setup__() -> (ContractAddress, ContractAddress, ContractAddress, ContractAd
nft_contract_address,
(*registry_class_hash.class_hash).into(),
(*account_class_hash.class_hash).into(),
2478,
2478
);
stop_cheat_caller_address(hub_contract_address);
Expand Down Expand Up @@ -249,7 +253,6 @@ fn test_set_block_status() {
#[test]
fn test_get_handle_id() {
let (hub_contract_address, user_one_profile_address, _, _, minted_handle_id) = __setup__();

let dispatcher = IHubDispatcher { contract_address: hub_contract_address };
let handle_id = dispatcher.get_handle_id(user_one_profile_address);
assert(handle_id == minted_handle_id, 'invalid handle id');
Expand Down
8 changes: 4 additions & 4 deletions tests/test_profile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn __setup__() -> (ContractAddress, ContractAddress, felt252, felt252, ContractA
let (registry_contract_address, _) = registry_class_hash.deploy(@array![]).unwrap_syscall();

// declare account
let account_class_hash = declare("Account").unwrap().contract_class();
let account_class_hash = declare("AccountPreset").unwrap().contract_class();

// declare follownft
let follow_nft_classhash = declare("Follow").unwrap().contract_class();
Expand Down Expand Up @@ -71,7 +71,7 @@ fn test_profile_creation() {
start_cheat_caller_address(profile_contract_address, USER.try_into().unwrap());
start_cheat_caller_address(nft_contract_address, USER.try_into().unwrap());
let profile_address = profileDispatcher
.create_profile(nft_contract_address, registry_class_hash, account_class_hash, 2456,);
.create_profile(nft_contract_address, registry_class_hash, account_class_hash, 2456, 2456);

// test a new karst nft is minted
let last_minted_id = karstNFTDispatcher.get_last_minted_id();
Expand Down Expand Up @@ -103,7 +103,7 @@ fn test_profile_metadata() {
start_cheat_caller_address(profile_contract_address, USER.try_into().unwrap());
start_cheat_caller_address(nft_contract_address, USER.try_into().unwrap());
let profile_address = profileDispatcher
.create_profile(nft_contract_address, registry_class_hash, account_class_hash, 2456);
.create_profile(nft_contract_address, registry_class_hash, account_class_hash, 2456, 2456);

profileDispatcher
.set_profile_metadata_uri(
Expand Down Expand Up @@ -137,7 +137,7 @@ fn test_profile_creation_event() {
start_cheat_caller_address(nft_contract_address, USER.try_into().unwrap());

let profile_address = profileDispatcher
.create_profile(nft_contract_address, registry_class_hash, account_class_hash, 2456,);
.create_profile(nft_contract_address, registry_class_hash, account_class_hash, 2456, 2456);

let token_id = karstNFTDispatcher.get_user_token_id(USER.try_into().unwrap());

Expand Down
11 changes: 7 additions & 4 deletions tests/test_publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn __setup__() -> (
.unwrap_syscall();

// declare account
let account_class_hash = declare("Account").unwrap().contract_class();
let account_class_hash = declare("AccountPreset").unwrap().contract_class();

//declare collectnft
let collect_nft_classhash = declare("CollectNFT").unwrap().contract_class();
Expand All @@ -80,7 +80,8 @@ fn __setup__() -> (
nft_contract_address,
(*registry_class_hash.class_hash).into(),
(*account_class_hash.class_hash).into(),
2478
2478,
2479
);
let content_URI: ByteArray = "ipfs://helloworld";
let mut spy = spy_events();
Expand All @@ -99,7 +100,8 @@ fn __setup__() -> (
nft_contract_address,
(*registry_class_hash.class_hash).into(),
(*account_class_hash.class_hash).into(),
2479
2479,
2482
);
let content_URI: ByteArray = "ipfs://helloworld";
dispatcher
Expand All @@ -118,7 +120,8 @@ fn __setup__() -> (
nft_contract_address,
(*registry_class_hash.class_hash).into(),
(*account_class_hash.class_hash).into(),
2480
2480,
2481
);
let content_URI: ByteArray = "ipfs://helloworld";
dispatcher
Expand Down
Loading