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

Add minter component and refactoring project #11

Merged
merged 9 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
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: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ build

.DS_Store

# deployment
tmp
.tmp*
old*
old*
scripts/*.log
89 changes: 89 additions & 0 deletions scripts/deploy_minter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash
source ../.env

# Check if --debug parameter is passed
debug="false"
for arg in "$@"
do
if [ "$arg" == "--debug" ]
then
debug="true"
fi
done

SIERRA_FILE=../target/dev/carbon_v3_Minter.contract_class.json
PROJECT=0x06bdcca1d679f32955b1fd1c3116d0b72ae42b31c2833e22ba179fd0581d5a31
OWNER=0x01e2F67d8132831f210E19c5Ee0197aA134308e16F7f284bBa2c72E28FC464D2
ERC20=0x01bd7237484b074609961b53f7d8978e921127e53a2af358e59b4b80eb29d640
PUBLIC_SALE_OPEN=1
MAX_VALUE=20000000000
UNIT_PRICE=11

# build the solution
build() {
output=$(scarb build 2>&1)

if [[ $output == *"Error"* ]]; then
echo "Error: $output"
exit 1
fi
}

# declare the contract
declare() {
build
if [[ $debug == "true" ]]; then
printf "declare %s --keystore-password KEYSTORE_PASSWORD --watch\n" "$SIERRA_FILE" > debug_minter.log
fi
output=$(starkli declare $SIERRA_FILE --keystore-password $KEYSTORE_PASSWORD --watch 2>&1)

if [[ $output == *"Error"* ]]; then
echo "Error: $output"
exit 1
fi

address=$(echo "$output" | grep -oP '0x[0-9a-fA-F]+')
echo $address
}

# deploy the contract
# $1 - Name
# $2 - Symbol
# $3 - Decimals
# $4 - Owner
deploy() {
class_hash=$(declare | tail -n 1)
sleep 5

if [[ $debug == "true" ]]; then
printf "deploy %s %s %s %s u256:%s u256:%s %s --keystore-password KEYSTORE_PASSWORD --watch\n" "$class_hash" "$PROJECT" "$ERC20" "$PUBLIC_SALE_OPEN" "$MAX_VALUE" "$UNIT_PRICE" "$OWNER" >> debug_minter.log
fi
output=$(starkli deploy $class_hash "$PROJECT" "$ERC20" "$PUBLIC_SALE_OPEN" u256:"$MAX_VALUE" u256:"$UNIT_PRICE" "$OWNER" --keystore-password $KEYSTORE_PASSWORD --watch 2>&1)

if [[ $output == *"Error"* ]]; then
echo "Error: $output"
exit 1
fi

address=$(echo "$output" | grep -oP '0x[0-9a-fA-F]+' | tail -n 1)
echo $address
}

setup() {
contract=$(deploy)
sleep 5

if [[ $debug == "true" ]]; then
printf "invoke %s add_minter %s %s --keystore-password KEYSTORE_PASSWORD --watch\n" "$PROJECT" "$SLOT" "$contract" >> debug_minter.log
fi
output=$(starkli invoke $PROJECT add_minter u256:$SLOT $contract --keystore-password $KEYSTORE_PASSWORD --watch 2>&1)

if [[ $output == *"Error"* ]]; then
echo "Error: $output"
exit 1
fi

echo $contract
}
contract_address=$(deploy)
echo $contract_address
34 changes: 26 additions & 8 deletions scripts/deploy_project.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#!/bin/bash
source ../.env

# Check if --debug parameter is passed
debug="false"
for arg in "$@"
do
if [ "$arg" == "--debug" ]
then
debug="true"
fi
done

SIERRA_FILE=../target/dev/carbon_v3_Project.contract_class.json
NAME="TokCC"
SYMBOL="CARBT"
DECIMALS=6
OWNER=0x01e2F67d8132831f210E19c5Ee0197aA134308e16F7f284bBa2c72E28FC464D2
PROJECT_VALUE=121099000000
TIMES="21 1674579600 1706115600 1737738000 1769274000 1800810000 1832346000 1863968400 1895504400 1927040400 1958576400 1990198800 2021734800 2053270800 2084806800 2116429200 2147965200 2179501200 2211037200 2242659600 2274195600 2305731600"
ABSORPTIONS="21 0 29609535 47991466 88828605 118438140 370922507 623406874 875891241 1128375608 1380859976 2076175721 2771491466 3466807212 4162122957 4857438703 5552754448 6248070193 6943385939 7638701684 8000000000 8000000000"
TON_EQUIVALENT=1000000

# build the solution
build() {
Expand All @@ -24,6 +32,9 @@ build() {
# declare the contract
declare() {
build
if [[ $debug == "true" ]]; then
printf "declare %s\n" "$SIERRA_FILE" > debug_project.log
fi
output=$(starkli declare $SIERRA_FILE --keystore-password $KEYSTORE_PASSWORD --watch 2>&1)

if [[ $output == *"Error"* ]]; then
Expand All @@ -43,7 +54,9 @@ declare() {
deploy() {
class_hash=$(declare | tail -n 1)
sleep 5

if [[ $debug == "true" ]]; then
printf "deploy %s %s %s %s \n" "$class_hash" "$NAME" "$SYMBOL" "$OWNER" >> debug_project.log
fi
output=$(starkli deploy $class_hash str:"$NAME" str:"$SYMBOL" "$OWNER" --keystore-password $KEYSTORE_PASSWORD --watch 2>&1)

if [[ $output == *"Error"* ]]; then
Expand All @@ -56,18 +69,23 @@ deploy() {
}

setup() {
#contract=$(deploy)
#sleep 5
contract="0x0348de44e0109e364e15c85407950a6ed39393a49396ca83c02516c4390f81e8"

contract=$(deploy)
sleep 5

if [[ $debug == "true" ]]; then
printf "invoke %s set_project_value u256:%s \n" "$contract" "$PROJECT_VALUE" >> debug_project.log
fi
output=$(starkli invoke $contract set_project_value u256:$PROJECT_VALUE --keystore-password $KEYSTORE_PASSWORD --watch 2>&1)
if [[ $output == *"Error"* ]]; then
echo "Error: $output"
exit 1
fi
echo "Success: $output"

output=$(starkli invoke $contract set_absorptions $TIMES $ABSORPTIONS $TON_EQUIVALENT --keystore-password $KEYSTORE_PASSWORD --watch 2>&1)
if [[ $debug == "true" ]]; then
printf "invoke %s set_absorptions %s %s \n" "$contract" "$TIMES" "$ABSORPTIONS" >> debug_project.log
fi
output=$(starkli invoke $contract set_absorptions $TIMES $ABSORPTIONS --keystore-password $KEYSTORE_PASSWORD --watch 2>&1)
if [[ $output == *"Error"* ]]; then
echo "Error: $output"
exit 1
Expand Down
4 changes: 2 additions & 2 deletions src/components/absorber.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod module;
mod carbon;
mod interface;

use module::AbsorberComponent;
use carbon::AbsorberComponent;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ mod AbsorberComponent {

// Internal imports
use carbon_v3::components::absorber::interface::IAbsorber;
use carbon_v3::components::absorber::interface::ICarbonCredits;

// Constants

const YEAR_SECONDS: u64 = 31556925;
const MULT_ACCURATE_SHARE: u256 = 1_000_000;
cloudvenger marked this conversation as resolved.
Show resolved Hide resolved
const CREDIT_CARBON_TON: u256 = 1_000_000;
const CC_DECIMALS: u8 = 6;

#[storage]
struct Storage {
Expand Down Expand Up @@ -43,6 +51,10 @@ mod AbsorberComponent {
value: u256
}

mod Errors {
const INVALID_ARRAY_LENGTH: felt252 = 'ERC1155: invalid array length';
cloudvenger marked this conversation as resolved.
Show resolved Hide resolved
}

#[embeddable_as(AbsorberImpl)]
impl Absorber<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
Expand Down Expand Up @@ -116,15 +128,11 @@ mod AbsorberComponent {
* self.Absorber_ton_equivalent.read().into() != 0
}
fn set_absorptions(
ref self: ComponentState<TContractState>,
times: Span<u64>,
absorptions: Span<u64>,
ton_equivalent: u64
ref self: ComponentState<TContractState>, times: Span<u64>, absorptions: Span<u64>
) {
// [Check] Times and prices are defined
assert(times.len() == absorptions.len(), 'Times and absorptions mismatch');
assert(times.len() > 0, 'Inputs cannot be empty');
assert(ton_equivalent > 0, 'Ton equivalent must be positive');

// [Effect] Clean times and absorptions
let mut stored_times: List<u64> = self.Absorber_times.read();
Expand All @@ -150,9 +158,6 @@ mod AbsorberComponent {
stored_absorptions.append(*absorptions[index]);
};

// [Effect] Store new ton equivalent
self.Absorber_ton_equivalent.write(ton_equivalent);

// [Event] Emit event
let current_time = get_block_timestamp();
self.emit(AbsorptionUpdate { time: current_time });
Expand All @@ -166,6 +171,64 @@ mod AbsorberComponent {
}
}

#[embeddable_as(CarbonCreditsImpl)]
impl CarbonCredits<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of ICarbonCredits<ComponentState<TContractState>> {
fn get_cc_vintages(self: @ComponentState<TContractState>) -> Span<u256> {
let times = self.Absorber_times.read();
cloudvenger marked this conversation as resolved.
Show resolved Hide resolved
let mut cc_vintages: Array<u256> = Default::default();
let mut index = 0;
loop {
if index == times.len() {
break ();
}
cc_vintages.append(index.into() + 1);
index += 1;
};
cc_vintages.span()
}


fn compute_cc_distribution(
self: @ComponentState<TContractState>, share: u256
) -> Span<u256> {
let times = self.Absorber_times.read();
let absorptions = self.Absorber_absorptions.read();
let absorptions_u256 = self.__span_u64_into_u256(absorptions.array().span());

// [Check] list time and absorptions are equal size
assert(times.len() == absorptions.len(), Errors::INVALID_ARRAY_LENGTH);
cloudvenger marked this conversation as resolved.
Show resolved Hide resolved

let mut cc_distribution: Array<u256> = Default::default();
let mut index = 0;
loop {
if index == times.len() {
break ();
}
let mut current_absorption: u256 = 0;
if index == 0 {
current_absorption = *absorptions_u256[index];
} else {
current_absorption = *absorptions_u256[index] - *absorptions_u256[index - 1];
}

cc_distribution
.append(
(current_absorption * share / MULT_ACCURATE_SHARE)
.try_into()
cloudvenger marked this conversation as resolved.
Show resolved Hide resolved
.expect('CC: Distribution overflow')
);
index += 1;
};
cc_distribution.span()
}

fn get_cc_decimals(self: @ComponentState<TContractState>) -> u8 {
CC_DECIMALS
}
}

#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
Expand All @@ -184,6 +247,21 @@ mod AbsorberComponent {
};
array.span()
}

fn __span_u64_into_u256(
self: @ComponentState<TContractState>, span: Span<u64>
) -> Span<u256> {
let mut array = ArrayTrait::<u256>::new();
let mut index = 0;
loop {
if index == span.len() {
break ();
}
array.append((*span[index]).into());
index += 1;
};
array.span()
}
}
}

16 changes: 13 additions & 3 deletions src/components/absorber/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@ trait IAbsorber<TContractState> {
fn is_setup(self: @TContractState) -> bool;

/// Setup the absorption curve parameters.
fn set_absorptions(
ref self: TContractState, times: Span<u64>, absorptions: Span<u64>, ton_equivalent: u64
);
fn set_absorptions(ref self: TContractState, times: Span<u64>, absorptions: Span<u64>);

/// Setup the project value for the given slot.
fn set_project_value(ref self: TContractState, project_value: u256);
}

#[starknet::interface]
trait ICarbonCredits<TContractState> {
/// Returns the carbon credits vintage list.
fn get_cc_vintages(self: @TContractState) -> Span<u256>;

/// Compute number of Carbon Credit of each vintage for given value
fn compute_cc_distribution(self: @TContractState, share: u256) -> Span<u256>;

// Get number of decimal for total supply to have a carbon credit
fn get_cc_decimals(self: @TContractState) -> u8;
}
5 changes: 0 additions & 5 deletions src/components/mint.cairo

This file was deleted.

Loading
Loading