Skip to content

Commit

Permalink
✨ Improve interfaces and add ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Nov 28, 2023
1 parent 11b8c89 commit 9896620
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 15 deletions.
63 changes: 63 additions & 0 deletions token/src/erc1155/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,66 @@ trait IERC1155Metadata<TState> {
fn symbol(self: @TState) -> felt252;
fn uri(self: @TState, token_id: u256) -> felt252;
}

//
// ERC721 ABI
//

#[starknet::interface]
trait ERC1155ABI<TState> {
// IERC1155
fn balance_of(self: @TState, account: ContractAddress, id: u256) -> u256;
fn balance_of_batch(
self: @TState, accounts: Array<ContractAddress>, ids: Array<u256>
) -> Array<u256>;
fn set_approval_for_all(ref self: TState, operator: ContractAddress, approved: bool);
fn is_approved_for_all(
self: @TState, account: ContractAddress, operator: ContractAddress
) -> bool;
fn safe_transfer_from(
ref self: TState,
from: ContractAddress,
to: ContractAddress,
id: u256,
amount: u256,
data: Array<u8>
);
fn safe_batch_transfer_from(
ref self: TState,
from: ContractAddress,
to: ContractAddress,
ids: Array<u256>,
amounts: Array<u256>,
data: Array<u8>
);

// IERC1155Metadata
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn uri(self: @TState, token_id: u256) -> felt252;

// IERC1155CamelOnly
fn balanceOf(self: @TState, account: ContractAddress, id: u256) -> u256;
fn balanceOfBatch(
self: @TState, accounts: Array<ContractAddress>, ids: Array<u256>
) -> Array<u256>;
fn setApprovalForAll(ref self: TState, operator: ContractAddress, approved: bool);
fn isApprovedForAll(self: @TState, account: ContractAddress, operator: ContractAddress) -> bool;
fn safeTransferFrom(
ref self: TState,
from: ContractAddress,
to: ContractAddress,
id: u256,
amount: u256,
data: Array<u8>
);
fn safeBatchTransferFrom(
ref self: TState,
from: ContractAddress,
to: ContractAddress,
ids: Array<u256>,
amounts: Array<u256>,
data: Array<u8>
);
}

5 changes: 4 additions & 1 deletion token/src/erc20/erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod ERC20 {
//

#[external(v0)]
impl ERC20Impl of interface::IERC20<ContractState> {
impl ERC20MetadataImpl of interface::IERC20Metadata<ContractState> {
fn name(self: @ContractState) -> felt252 {
self.get_meta().name
}
Expand All @@ -75,7 +75,10 @@ mod ERC20 {
fn decimals(self: @ContractState) -> u8 {
18
}
}

#[external(v0)]
impl ERC20Impl of interface::IERC20<ContractState> {
fn total_supply(self: @ContractState) -> u256 {
self.get_meta().total_supply
}
Expand Down
70 changes: 68 additions & 2 deletions token/src/erc20/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,61 @@ use starknet::ContractAddress;

#[starknet::interface]
trait IERC20<TState> {
fn total_supply(self: @TState) -> u256;
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool;
fn transfer_from(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool;
}

#[starknet::interface]
trait IERC20Metadata<TState> {
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn decimals(self: @TState) -> u8;
}

#[starknet::interface]
trait ISafeAllowance<TState> {
fn increase_allowance(ref self: TState, spender: ContractAddress, added_value: u256) -> bool;
fn decrease_allowance(
ref self: TState, spender: ContractAddress, subtracted_value: u256
) -> bool;
}

#[starknet::interface]
trait IERC20Camel<TState> {
fn totalSupply(self: @TState) -> u256;
fn balanceOf(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool;
fn transferFrom(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool;
}

#[starknet::interface]
trait IERC20CamelOnly<TState> {
fn totalSupply(self: @TState) -> u256;
fn balanceOf(self: @TState, account: ContractAddress) -> u256;
fn transferFrom(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
}

#[starknet::interface]
trait ISafeAllowanceCamel<TState> {
fn increaseAllowance(ref self: TState, spender: ContractAddress, addedValue: u256) -> bool;
fn decreaseAllowance(ref self: TState, spender: ContractAddress, subtractedValue: u256) -> bool;
}

#[starknet::interface]
trait ERC20ABI<TState> {
// IERC20
fn total_supply(self: @TState) -> u256;
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
Expand All @@ -13,12 +65,26 @@ trait IERC20<TState> {
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool;
}

trait IERC20CamelOnly<TState> {
// IERC20Metadata
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn decimals(self: @TState) -> u8;

// IERC20SafeAllowance
fn increase_allowance(ref self: TState, spender: ContractAddress, added_value: u256) -> bool;
fn decrease_allowance(
ref self: TState, spender: ContractAddress, subtracted_value: u256
) -> bool;

// IERC20CamelOnly
fn totalSupply(self: @TState) -> u256;
fn balanceOf(self: @TState, account: ContractAddress) -> u256;
fn transferFrom(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;

// IERC20CamelSafeAllowance
fn increaseAllowance(ref self: TState, spender: ContractAddress, addedValue: u256) -> bool;
fn decreaseAllowance(ref self: TState, spender: ContractAddress, subtractedValue: u256) -> bool;
}
13 changes: 7 additions & 6 deletions token/src/erc20/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use token::tests::constants::{
};
use token::erc20::ERC20::Approval;
use token::erc20::ERC20::ERC20Impl;
use token::erc20::ERC20::ERC20MetadataImpl;
use token::erc20::ERC20::InternalImpl;
use token::erc20::ERC20::Transfer;
use token::erc20::ERC20;
Expand Down Expand Up @@ -57,9 +58,9 @@ fn test_initializer() {
let (world, mut state) = STATE();
InternalImpl::initializer(ref state, NAME, SYMBOL);

assert(ERC20Impl::name(@state) == NAME, 'Name should be NAME');
assert(ERC20Impl::symbol(@state) == SYMBOL, 'Symbol should be SYMBOL');
assert(ERC20Impl::decimals(@state) == DECIMALS, 'Decimals should be 18');
assert(ERC20MetadataImpl::name(@state) == NAME, 'Name should be NAME');
assert(ERC20MetadataImpl::symbol(@state) == SYMBOL, 'Symbol should be SYMBOL');
assert(ERC20MetadataImpl::decimals(@state) == DECIMALS, 'Decimals should be 18');
assert(ERC20Impl::total_supply(@state) == 0, 'Supply should eq 0');
}

Expand All @@ -74,9 +75,9 @@ fn test_constructor() {

assert(ERC20Impl::balance_of(@state, OWNER()) == SUPPLY, 'Should eq inital_supply');
assert(ERC20Impl::total_supply(@state) == SUPPLY, 'Should eq inital_supply');
assert(ERC20Impl::name(@state) == NAME, 'Name should be NAME');
assert(ERC20Impl::symbol(@state) == SYMBOL, 'Symbol should be SYMBOL');
assert(ERC20Impl::decimals(@state) == DECIMALS, 'Decimals should be 18');
assert(ERC20MetadataImpl::name(@state) == NAME, 'Name should be NAME');
assert(ERC20MetadataImpl::symbol(@state) == SYMBOL, 'Symbol should be SYMBOL');
assert(ERC20MetadataImpl::decimals(@state) == DECIMALS, 'Decimals should be 18');
}

//
Expand Down
82 changes: 76 additions & 6 deletions token/src/erc721/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const IERC721_RECEIVER_ID: felt252 =
trait IERC721<TState> {
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn owner_of(self: @TState, token_id: u256) -> ContractAddress;
fn transfer_from(ref self: TState, from: ContractAddress, to: ContractAddress, token_id: u256);
fn safe_transfer_from(
ref self: TState,
from: ContractAddress,
to: ContractAddress,
token_id: u256,
data: Span<felt252>
);
fn transfer_from(ref self: TState, from: ContractAddress, to: ContractAddress, token_id: u256);
fn approve(ref self: TState, to: ContractAddress, token_id: u256);
fn set_approval_for_all(ref self: TState, operator: ContractAddress, approved: bool);
fn get_approved(self: @TState, token_id: u256) -> ContractAddress;
Expand All @@ -26,35 +26,105 @@ trait IERC721<TState> {
) -> bool;
}

#[starknet::interface]
trait IERC721Metadata<TState> {
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn token_uri(self: @TState, token_id: u256) -> felt252;
}

#[starknet::interface]
trait IERC721CamelOnly<TState> {
fn balanceOf(self: @TState, account: ContractAddress) -> u256;
fn ownerOf(self: @TState, tokenId: u256) -> ContractAddress;
fn transferFrom(ref self: TState, from: ContractAddress, to: ContractAddress, tokenId: u256);
fn safeTransferFrom(
ref self: TState,
from: ContractAddress,
to: ContractAddress,
tokenId: u256,
data: Span<felt252>
);
fn transferFrom(ref self: TState, from: ContractAddress, to: ContractAddress, tokenId: u256);
fn setApprovalForAll(ref self: TState, operator: ContractAddress, approved: bool);
fn getApproved(self: @TState, tokenId: u256) -> ContractAddress;
fn isApprovedForAll(self: @TState, owner: ContractAddress, operator: ContractAddress) -> bool;
}

#[starknet::interface]
trait IERC721MetadataCamelOnly<TState> {
fn tokenURI(self: @TState, tokenId: u256) -> felt252;
}

//
// IERC721Metadata
// ERC721 ABI
//

#[starknet::interface]
trait IERC721Metadata<TState> {
trait ERC721ABI<TState> {
// IERC721
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn owner_of(self: @TState, token_id: u256) -> ContractAddress;
fn safe_transfer_from(
ref self: TState,
from: ContractAddress,
to: ContractAddress,
token_id: u256,
data: Span<felt252>
);
fn transfer_from(ref self: TState, from: ContractAddress, to: ContractAddress, token_id: u256);
fn approve(ref self: TState, to: ContractAddress, token_id: u256);
fn set_approval_for_all(ref self: TState, operator: ContractAddress, approved: bool);
fn get_approved(self: @TState, token_id: u256) -> ContractAddress;
fn is_approved_for_all(
self: @TState, owner: ContractAddress, operator: ContractAddress
) -> bool;

// IERC721Metadata
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn token_uri(self: @TState, token_id: u256) -> felt252;

// IERC721CamelOnly
fn balanceOf(self: @TState, account: ContractAddress) -> u256;
fn ownerOf(self: @TState, tokenId: u256) -> ContractAddress;
fn safeTransferFrom(
ref self: TState,
from: ContractAddress,
to: ContractAddress,
tokenId: u256,
data: Span<felt252>
);
fn transferFrom(ref self: TState, from: ContractAddress, to: ContractAddress, tokenId: u256);
fn setApprovalForAll(ref self: TState, operator: ContractAddress, approved: bool);
fn getApproved(self: @TState, tokenId: u256) -> ContractAddress;
fn isApprovedForAll(self: @TState, owner: ContractAddress, operator: ContractAddress) -> bool;

// IERC721MetadataCamelOnly
fn tokenURI(self: @TState, tokenId: u256) -> felt252;
}

//
// ERC721Receiver
//

#[starknet::interface]
trait IERC721MetadataCamelOnly<TState> {
fn tokenURI(self: @TState, tokenId: u256) -> felt252;
trait IERC721Receiver<TState> {
fn on_erc721_received(
self: @TState,
operator: ContractAddress,
from: ContractAddress,
token_id: u256,
data: Span<felt252>
) -> felt252;
}

#[starknet::interface]
trait IERC721ReceiverCamel<TState> {
fn onERC721Received(
self: @TState,
operator: ContractAddress,
from: ContractAddress,
tokenId: u256,
data: Span<felt252>
) -> felt252;
}

0 comments on commit 9896620

Please sign in to comment.