Skip to content

Commit

Permalink
refactor: sdk examples
Browse files Browse the repository at this point in the history
  • Loading branch information
remiroyc committed Dec 8, 2023
1 parent 40542a4 commit 72845e2
Show file tree
Hide file tree
Showing 23 changed files with 459 additions and 205 deletions.
6 changes: 6 additions & 0 deletions crates/ark-contracts/arkchain/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = 1
name = "ark_common"
version = "0.1.0"
dependencies = [
"openzeppelin",
"snforge_std",
]

Expand All @@ -16,6 +17,11 @@ dependencies = [
"snforge_std",
]

[[package]]
name = "openzeppelin"
version = "0.8.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.8.0#c23e8e96de60e6e3159b1ff8591a1187269c0eb7"

[[package]]
name = "snforge_std"
version = "0.1.0"
Expand Down
33 changes: 20 additions & 13 deletions crates/ark-contracts/arkchain/src/orderbook.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -611,20 +611,27 @@ mod orderbook {

if order.token_id.is_some() {
let execute_info = ExecutionInfo {
route: order.route,

order_hash: order.compute_order_hash(),
token_address: order.token_address,
token_id: order.token_id.unwrap(),
quantity: order.quantity,
offerer_address: order.offerer,
fulfiller_address: fulfill_info.fulfiller,
price: 0,
creator_address: 0x0.try_into().unwrap(),
creator_fee: 0,
create_broker_address: 0x0.try_into().unwrap(),
create_broker_fee: 0,
fulfill_broker_address: 0x0.try_into().unwrap(),
fulfill_broker_fee: 0
nft_from: order.offerer,
nft_to: fulfill_info.fulfiller,
nft_token_id: order.token_id.unwrap(),
nft_address: order.token_address

// route: order.route,
// order_hash: order.compute_order_hash(),
// token_address: order.token_address,
// token_id: order.token_id.unwrap(),
// quantity: order.quantity,
// offerer_address: order.offerer,
// fulfiller_address: fulfill_info.fulfiller,
// price: 0,
// creator_address: 0x0.try_into().unwrap(),
// creator_fee: 0,
// create_broker_address: 0x0.try_into().unwrap(),
// create_broker_fee: 0,
// fulfill_broker_address: 0x0.try_into().unwrap(),
// fulfill_broker_fee: 0
};

execute_info.serialize(ref buf);
Expand Down
32 changes: 19 additions & 13 deletions crates/ark-contracts/common/src/protocol/order_types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,26 @@ struct FulfillInfo {
/// to execute an order.
#[derive(Drop, Serde, Copy)]
struct ExecutionInfo {
route: RouteType,
order_hash: felt252,
token_address: ContractAddress,
token_id: u256,
quantity: u256,
offerer_address: ContractAddress,
fulfiller_address: ContractAddress,
price: u256,
creator_address: ContractAddress,
creator_fee: u256,
create_broker_address: ContractAddress,
create_broker_fee: u256,
fulfill_broker_address: ContractAddress,
fulfill_broker_fee: u256
nft_address: ContractAddress,
nft_from: ContractAddress,
nft_to: ContractAddress,
nft_token_id: u256

// route: RouteType,
// order_hash: felt252,
// token_address: ContractAddress,
// token_id: u256,
// quantity: u256,
// offerer_address: ContractAddress,
// fulfiller_address: ContractAddress,
// price: u256,
// creator_address: ContractAddress,
// creator_fee: u256,
// create_broker_address: ContractAddress,
// create_broker_fee: u256,
// fulfill_broker_address: ContractAddress,
// fulfill_broker_fee: u256
}

/// The info sent from Starknet to the Arkchain to
Expand Down
84 changes: 42 additions & 42 deletions crates/ark-contracts/starknet/src/executor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ mod executor {
contract_address: self.eth_contract_address.read()
};

let nft_contract = IERC721Dispatcher { contract_address: execution_info.token_address };
// nft_contract.transfer_from(execution_info.fulfiller_address, execution_info.offerer_address, execution_info.token_id);
let nft_contract = IERC721Dispatcher { contract_address: execution_info.nft_address };
nft_contract.transfer_from(execution_info.nft_from, execution_info.nft_to, execution_info.nft_token_id);

// self._transfer_royalties(execution_info, eth_contract);

Expand Down Expand Up @@ -187,44 +187,44 @@ mod executor {
}
}

#[generate_trait]
impl InternalFunctions of InternalFunctionsTrait {
fn _transfer_royalties(
ref self: ContractState, execution_info: ExecutionInfo, eth_contract: IERC20Dispatcher
) {
// Royalties distribution
let arkchain_fee = self.arkchain_fee.read();
let total_fees = execution_info.create_broker_fee
+ execution_info.fulfill_broker_fee
+ execution_info.creator_fee
+ arkchain_fee;
assert(execution_info.price < total_fees, 'Invalid price');

match execution_info.route {
RouteType::Erc20ToErc721 => { // ...
},
RouteType::Erc721ToErc20 => {
eth_contract
.transfer_from(
execution_info.offerer_address, self.admin_address.read(), arkchain_fee
);
// eth_contract.transfer_from(execution_info.offerer_address, self.admin_address.read(), arkchain_fee);
},
};
// eth_contract
// .transfer_from(execution_info.taker_address, execution_info.creator_address, execution_info.creator_fee);

// eth_contract
// .transfer_from(
// execution_info.taker_address, execution_info.create_broker_address, execution_info.create_broker_fee
// );

// eth_contract
// .transfer_from(
// execution_info.taker_address, execution_info.fulfill_broker_address, execution_info.fulfill_broker_fee
// );

// eth_contract.transferFrom(execution_info.taker_address, execution_info.maker_address, execution_info.price);
}
}
// #[generate_trait]
// impl InternalFunctions of InternalFunctionsTrait {
// fn _transfer_royalties(
// ref self: ContractState, execution_info: ExecutionInfo, eth_contract: IERC20Dispatcher
// ) {
// // Royalties distribution
// let arkchain_fee = self.arkchain_fee.read();
// let total_fees = execution_info.create_broker_fee
// + execution_info.fulfill_broker_fee
// + execution_info.creator_fee
// + arkchain_fee;
// assert(execution_info.price < total_fees, 'Invalid price');

// match execution_info.route {
// RouteType::Erc20ToErc721 => { // ...
// },
// RouteType::Erc721ToErc20 => {
// eth_contract
// .transfer_from(
// execution_info.offerer_address, self.admin_address.read(), arkchain_fee
// );
// // eth_contract.transfer_from(execution_info.offerer_address, self.admin_address.read(), arkchain_fee);
// },
// };
// // eth_contract
// // .transfer_from(execution_info.taker_address, execution_info.creator_address, execution_info.creator_fee);

// // eth_contract
// // .transfer_from(
// // execution_info.taker_address, execution_info.create_broker_address, execution_info.create_broker_fee
// // );

// // eth_contract
// // .transfer_from(
// // execution_info.taker_address, execution_info.fulfill_broker_address, execution_info.fulfill_broker_fee
// // );

// // eth_contract.transferFrom(execution_info.taker_address, execution_info.maker_address, execution_info.price);
// }
// }
}
32 changes: 23 additions & 9 deletions packages/core/examples/cancelListing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,47 @@ import { RpcProvider } from "starknet";
import { createAccount } from "../src/actions/account/account";
import { cancelOrder, createListing } from "../src/actions/order";
import { getOrderHash } from "../src/actions/read";
import { STARKNET_NFT_ADDRESS } from "../src/constants";
import { ListingV1 } from "../src/types";

// Initialize the RPC provider with the ArkChain node URL
const provider = new RpcProvider({
const arkchainProvider = new RpcProvider({
nodeUrl: "http://0.0.0.0:7777"
});

const katana0 = {
privateKey: "0x1800000000300000180000000000030000000000003006001800006600",
publicKey:
"0x2b191c2f3ecf685a91af7cf72a43e7b90e2e41220175de5c4f7498981b10053",
accountAddress:
"0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
};

/**
* Creates a listing on the blockchain using provided order details.
*
* @param {RpcProvider} provider - The RPC provider instance.
*/
(async (provider: RpcProvider) => {
(async () => {
// Create a new account using the provider
const { account } = await createAccount(provider);
const { account: burnerAccount } = await createAccount(arkchainProvider);

// Define the order details
let order: ListingV1 = {
brokerId: 123, // The broker ID
tokenAddress:
"0x01435498bf393da86b4733b9264a86b58a42b31f8d8b8ba309593e5c17847672", // The token address
tokenAddress: STARKNET_NFT_ADDRESS, // The token address
tokenId: 6, // The ID of the token
startAmount: 600000000000000000 // The starting amount for the order
};

// Create the listing on the arkchain using the order details
await createListing(provider, account, order);
await createListing(
arkchainProvider,
burnerAccount,
order,
katana0.accountAddress,
katana0.privateKey
);

// wait 5 seconds for the transaction to be processed
await new Promise((resolve) => setTimeout(resolve, 5000));
Expand All @@ -44,7 +58,7 @@ const provider = new RpcProvider({
const { orderHash } = await getOrderHash(
order.tokenId,
order.tokenAddress,
provider
arkchainProvider
);

// Define the cancel details
Expand All @@ -55,5 +69,5 @@ const provider = new RpcProvider({
};

// Cancel the order
cancelOrder(provider, account, cancelInfo);
})(provider);
cancelOrder(arkchainProvider, burnerAccount, cancelInfo, katana0.privateKey);
})();
37 changes: 30 additions & 7 deletions packages/core/examples/createListing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,54 @@ import { RpcProvider } from "starknet";

import { createAccount } from "../src/actions/account/account";
import { createListing } from "../src/actions/order";
import { STARKNET_NFT_ADDRESS } from "../src/constants";
import { ListingV1 } from "../src/types";

// Initialize the RPC provider with the ArkChain node URL
const provider = new RpcProvider({
const arkchainProvider = new RpcProvider({
nodeUrl: "http://0.0.0.0:7777"
});

const katana0 = {
privateKey: "0x1800000000300000180000000000030000000000003006001800006600",
publicKey:
"0x2b191c2f3ecf685a91af7cf72a43e7b90e2e41220175de5c4f7498981b10053",
accountAddress:
"0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
};

const katana1 = {
privateKey:
"0x33003003001800009900180300d206308b0070db00121318d17b5e6262150b",
publicKey:
"0x4c0f884b8e5b4f00d97a3aad26b2e5de0c0c76a555060c837da2e287403c01d",
accountAddress:
"0x5686a647a9cdd63ade617e0baf3b364856b813b508f03903eb58a7e622d5855"
};

/**
* Creates a listing on the blockchain using provided order details.
*
* @param {RpcProvider} provider - The RPC provider instance.
*/
(async (provider: RpcProvider) => {
(async () => {
// Create a new account using the provider
const { account } = await createAccount(provider);
const { account: burnerAccount } = await createAccount(arkchainProvider);

// Define the order details
let order: ListingV1 = {
brokerId: 123, // The broker ID
tokenAddress:
"0x01435498bf393da86b4733b9264a86b58a42b31f8d8b8ba309593e5c17847672", // The token address
tokenAddress: STARKNET_NFT_ADDRESS, // The token address
tokenId: 909, // The ID of the token
startAmount: 600000000000000000 // The starting amount for the order
};

// Create the listing on the blockchain using the order details
await createListing(provider, account, order);
})(provider);
await createListing(
arkchainProvider,
burnerAccount,
order,
katana0.accountAddress,
katana0.privateKey
);
})();
Loading

0 comments on commit 72845e2

Please sign in to comment.