Skip to content

Commit

Permalink
feat: added marketplace to build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Jan 20, 2024
1 parent c4f5a21 commit 6330f45
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ args = ["fmt", "--all", "--", "--check"]

[tasks.did]
description = "Generate did files"
dependencies = ["deferred-did", "fly-did", "dfx-generate"]
dependencies = ["deferred-did", "fly-did", "marketplace-did", "dfx-generate"]
workspace = false

[tasks.deferred-did]
Expand All @@ -66,6 +66,11 @@ description = "Generate did files for fly"
script = "cargo run --bin fly-did --features did > src/fly/fly.did"
workspace = false

[tasks.marketplace-did]
description = "Generate did files for marketplace"
script = "cargo run --bin marketplace-did --features did > src/marketplace/marketplace.did"
workspace = false

[tasks.dfx-generate]
description = "Generate dfx did"
command = "dfx"
Expand All @@ -74,5 +79,5 @@ workspace = false

[tasks.dfx-setup]
description = "setup dfx"
script = "dfx stop; dfx start --background; dfx canister create deferred; dfx canister create fly"
script = "dfx stop; dfx start --background; dfx canister create deferred; dfx canister create fly; dfx canister create marketplace"
workspace = false
5 changes: 5 additions & 0 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"candid": "src/fly/fly.did",
"package": "fly",
"type": "rust"
},
"marketplace": {
"candid": "src/marketplace/marketplace.did",
"package": "marketplace",
"type": "rust"
}
},
"defaults": {
Expand Down
22 changes: 21 additions & 1 deletion integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::vec;
use candid::{CandidType, Decode, Encode, Nat, Principal};
use did::deferred::DeferredInitData;
use did::fly::{FlyInitData, PicoFly};
use did::marketplace::MarketplaceInitData;
use pocket_ic::{PocketIc, WasmResult};
use serde::de::DeserializeOwned;

Expand Down Expand Up @@ -88,7 +89,7 @@ impl TestEnv {
// install deferred canister
Self::install_deferred(&pic, deferred_id, fly_id, marketplace_id);
Self::install_fly(&pic, fly_id, deferred_id, marketplace_id);
// TODO: install marketplace
Self::install_marketplace(&pic, marketplace_id, deferred_id, fly_id);

TestEnv {
pic,
Expand Down Expand Up @@ -143,6 +144,25 @@ impl TestEnv {
pic.install_canister(fly_id, wasm_bytes, init_arg, None);
}

fn install_marketplace(
pic: &PocketIc,
marketplace_id: Principal,
deferred_id: Principal,
fly_id: Principal,
) {
pic.add_cycles(marketplace_id, DEFAULT_CYCLES);
let wasm_bytes = Self::load_wasm(Canister::Marketplace);

let init_arg = MarketplaceInitData {
admins: vec![actor::admin()],
deferred_canister: deferred_id,
fly_canister: fly_id,
};
let init_arg = Encode!(&init_arg).unwrap();

pic.install_canister(marketplace_id, wasm_bytes, init_arg, None);
}

fn load_wasm(canister: Canister) -> Vec<u8> {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("../.dfx/local/canisters");
Expand Down
19 changes: 19 additions & 0 deletions scripts/deploy_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,22 @@ deploy_fly() {
dfx deploy --mode=$INSTALL_MODE --yes --network="$NETWORK" --argument="$fly_init_args" fly

}

deploy_marketplace() {
INSTALL_MODE="$1"
NETWORK="$2"
MARKETPLACE_PRINCIPAL="$3"
DEFERRED_PRINCIPAL="$4"
FLY_PRINCIPAL="$5"
ADMINS="$6"

echo "deploying marketplace canister $MARKETPLACE_PRINCIPAL"

marketplace_init_args="(record {
deferred_canister = principal \"$DEFERRED_PRINCIPAL\";
fly_canister = principal \"$FLY_PRINCIPAL\";
admins = vec { $(for admin in $ADMINS; do echo "principal \"$admin\";"; done) };
})"

dfx deploy --mode=$INSTALL_MODE --yes --network="$NETWORK" --argument="$marketplace_init_args" marketplace
}
3 changes: 2 additions & 1 deletion scripts/deploy_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cd "$(dirname "$0")" || exit 1
CANISTER_IDS="../.dfx/local/canister_ids.json"
DEFERRED_PRINCIPAL="$(cat "$CANISTER_IDS" | jq -r '.deferred.local')"
FLY_PRINCIPAL="$(cat "$CANISTER_IDS" | jq -r '.fly.local')"
MARKETPLACE_PRINCIPAL="$(cat "$CANISTER_IDS" | jq -r '.deferred.local')" # TODO: fix
MARKETPLACE_PRINCIPAL="$(cat "$CANISTER_IDS" | jq -r '.marketplace.local')"

source ./deploy_functions.sh
source ./did.sh
Expand All @@ -23,6 +23,7 @@ cd ../

deploy_deferred "reinstall" "local" "$DEFERRED_PRINCIPAL" "$FLY_PRINCIPAL" "$MARKETPLACE_PRINCIPAL" "$ADMIN_PRINCIPAL"
deploy_fly "reinstall" "local" "$FLY_PRINCIPAL" "$ADMIN_PRINCIPAL" "$INITIAL_SUPPLY" "$FLY_INITIAL_BALANCES" "$DEFERRED_PRINCIPAL" "$MARKETPLACE_PRINCIPAL" "$SWAP_ACCOUNT" "$FLY_MINTING_ACCOUNT"
deploy_marketplace "reinstall" "local" "$MARKETPLACE_PRINCIPAL" "$DEFERRED_PRINCIPAL" "$FLY_PRINCIPAL" "$ADMIN_PRINCIPAL"

dfx stop

Expand Down
50 changes: 50 additions & 0 deletions src/declarations/marketplace/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type {
ActorSubclass,
HttpAgentOptions,
ActorConfig,
Agent,
} from "@dfinity/agent";
import type { Principal } from "@dfinity/principal";
import type { IDL } from "@dfinity/candid";

import { _SERVICE } from './marketplace.did';

export declare const idlFactory: IDL.InterfaceFactory;
export declare const canisterId: string;

export declare interface CreateActorOptions {
/**
* @see {@link Agent}
*/
agent?: Agent;
/**
* @see {@link HttpAgentOptions}
*/
agentOptions?: HttpAgentOptions;
/**
* @see {@link ActorConfig}
*/
actorOptions?: ActorConfig;
}

/**
* Intializes an {@link ActorSubclass}, configured with the provided SERVICE interface of a canister.
* @constructs {@link ActorSubClass}
* @param {string | Principal} canisterId - ID of the canister the {@link Actor} will talk to
* @param {CreateActorOptions} options - see {@link CreateActorOptions}
* @param {CreateActorOptions["agent"]} options.agent - a pre-configured agent you'd like to use. Supercedes agentOptions
* @param {CreateActorOptions["agentOptions"]} options.agentOptions - options to set up a new agent
* @see {@link HttpAgentOptions}
* @param {CreateActorOptions["actorOptions"]} options.actorOptions - options for the Actor
* @see {@link ActorConfig}
*/
export declare const createActor: (
canisterId: string | Principal,
options?: CreateActorOptions
) => ActorSubclass<_SERVICE>;

/**
* Intialized Actor using default settings, ready to talk to a canister using its candid interface
* @constructs {@link ActorSubClass}
*/
export declare const marketplace: ActorSubclass<_SERVICE>;
43 changes: 43 additions & 0 deletions src/declarations/marketplace/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Actor, HttpAgent } from "@dfinity/agent";

// Imports and re-exports candid interface
import { idlFactory } from "./marketplace.did.js";
export { idlFactory } from "./marketplace.did.js";

/* CANISTER_ID is replaced by webpack based on node environment
* Note: canister environment variable will be standardized as
* process.env.CANISTER_ID_<CANISTER_NAME_UPPERCASE>
* beginning in dfx 0.15.0
*/
export const canisterId =
process.env.CANISTER_ID_MARKETPLACE ||
process.env.MARKETPLACE_CANISTER_ID;

export const createActor = (canisterId, options = {}) => {
const agent = options.agent || new HttpAgent({ ...options.agentOptions });

if (options.agent && options.agentOptions) {
console.warn(
"Detected both agent and agentOptions passed to createActor. Ignoring agentOptions and proceeding with the provided agent."
);
}

// Fetch root key for certificate validation during development
if (process.env.DFX_NETWORK !== "ic") {
agent.fetchRootKey().catch((err) => {
console.warn(
"Unable to fetch root key. Check to ensure that your local replica is running"
);
console.error(err);
});
}

// Creates an actor with using the candid interface and the HttpAgent
return Actor.createActor(idlFactory, {
agent,
canisterId,
...options.actorOptions,
});
};

export const marketplace = createActor(canisterId);
123 changes: 123 additions & 0 deletions src/declarations/marketplace/marketplace.did.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import type { Principal } from '@dfinity/principal';
import type { ActorMethod } from '@dfinity/agent';

export type AllowanceError = { 'AllowanceNotFound' : null } |
{ 'BadSpender' : null } |
{ 'AllowanceChanged' : null } |
{ 'BadExpiration' : null } |
{ 'AllowanceExpired' : null } |
{ 'InsufficientFunds' : null };
export type BalanceError = { 'AccountNotFound' : null } |
{ 'InsufficientBalance' : null };
export type BuyError = { 'TokenHasNoOwner' : null } |
{ 'IcpAllowanceNotEnough' : null } |
{ 'CallerAlreadyOwnsToken' : null } |
{ 'IcpAllowanceExpired' : null };
export type ConfigurationError = { 'AdminsCantBeEmpty' : null } |
{ 'AnonymousAdmin' : null };
export type ConfigurationError_1 = { 'CustodialsCantBeEmpty' : null } |
{ 'AnonymousCustodial' : null };
export type DeferredError = { 'Fly' : FlyError } |
{ 'Nft' : NftError } |
{ 'Configuration' : ConfigurationError_1 } |
{ 'Unauthorized' : null } |
{ 'Token' : TokenError } |
{ 'StorageError' : null } |
{ 'CanisterCall' : [RejectionCode, string] };
export type FlyError = { 'Configuration' : ConfigurationError } |
{ 'Icrc1Transfer' : TransferError } |
{ 'Pool' : PoolError } |
{ 'Allowance' : AllowanceError } |
{ 'Register' : RegisterError } |
{ 'XrcError' : null } |
{ 'StorageError' : null } |
{ 'CanisterCall' : [RejectionCode, string] } |
{ 'Balance' : BalanceError } |
{ 'Icrc2Transfer' : TransferFromError };
export type MarketplaceError = { 'Buy' : BuyError } |
{ 'Configuration' : ConfigurationError } |
{ 'Icrc1Transfer' : TransferError } |
{ 'DeferredCanister' : DeferredError } |
{ 'TokenNotFound' : null } |
{ 'FlyCanister' : FlyError } |
{ 'XrcError' : null } |
{ 'StorageError' : null } |
{ 'CanisterCall' : [RejectionCode, string] } |
{ 'Dip721' : NftError } |
{ 'Icrc2Transfer' : TransferFromError };
export interface MarketplaceInitData {
'deferred_canister' : Principal,
'fly_canister' : Principal,
'admins' : Array<Principal>,
}
export type NftError = { 'UnauthorizedOperator' : null } |
{ 'SelfTransfer' : null } |
{ 'TokenNotFound' : null } |
{ 'UnauthorizedOwner' : null } |
{ 'TxNotFound' : null } |
{ 'SelfApprove' : null } |
{ 'OperatorNotFound' : null } |
{ 'ExistedNFT' : null } |
{ 'OwnerNotFound' : null } |
{ 'Other' : string };
export type PoolError = { 'PoolNotFound' : bigint } |
{ 'NotEnoughTokens' : null };
export type RegisterError = { 'TransactionNotFound' : null };
export type RejectionCode = { 'NoError' : null } |
{ 'CanisterError' : null } |
{ 'SysTransient' : null } |
{ 'DestinationInvalid' : null } |
{ 'Unknown' : null } |
{ 'SysFatal' : null } |
{ 'CanisterReject' : null };
export type Result = { 'Ok' : null } |
{ 'Err' : MarketplaceError };
export type Result_1 = { 'Ok' : bigint } |
{ 'Err' : MarketplaceError };
export type TokenError = { 'ContractAlreadySigned' : bigint } |
{ 'ContractValueIsNotMultipleOfInstallments' : null } |
{ 'TokenAlreadyExists' : bigint } |
{ 'TokensMismatch' : null } |
{ 'ContractAlreadyExists' : bigint } |
{ 'ContractTokensShouldBeEmpty' : null } |
{ 'TokenDoesNotBelongToContract' : bigint } |
{ 'TokenNotFound' : bigint } |
{ 'ContractSellerQuotaIsNot100' : null } |
{ 'ContractNotFound' : bigint } |
{ 'CannotCloseContract' : null } |
{ 'ContractNotSigned' : bigint } |
{ 'ContractHasNoSeller' : null } |
{ 'ContractHasNoTokens' : null } |
{ 'TokenIsBurned' : bigint } |
{ 'BadMintTokenOwner' : bigint } |
{ 'BadContractProperty' : null };
export type TransferError = {
'GenericError' : { 'message' : string, 'error_code' : bigint }
} |
{ 'TemporarilyUnavailable' : null } |
{ 'BadBurn' : { 'min_burn_amount' : bigint } } |
{ 'Duplicate' : { 'duplicate_of' : bigint } } |
{ 'BadFee' : { 'expected_fee' : bigint } } |
{ 'CreatedInFuture' : { 'ledger_time' : bigint } } |
{ 'TooOld' : null } |
{ 'InsufficientFunds' : { 'balance' : bigint } };
export type TransferFromError = {
'GenericError' : { 'message' : string, 'error_code' : bigint }
} |
{ 'TemporarilyUnavailable' : null } |
{ 'InsufficientAllowance' : { 'allowance' : bigint } } |
{ 'BadBurn' : { 'min_burn_amount' : bigint } } |
{ 'Duplicate' : { 'duplicate_of' : bigint } } |
{ 'BadFee' : { 'expected_fee' : bigint } } |
{ 'CreatedInFuture' : { 'ledger_time' : bigint } } |
{ 'TooOld' : null } |
{ 'InsufficientFunds' : { 'balance' : bigint } };
export interface _SERVICE {
'admin_cycles' : ActorMethod<[], bigint>,
'admin_set_admins' : ActorMethod<[Array<Principal>], Result>,
'admin_set_deferred_canister' : ActorMethod<[Principal], undefined>,
'admin_set_fly_canister' : ActorMethod<[Principal], Result>,
'admin_set_interest_rate_for_buyer' : ActorMethod<[number], undefined>,
'buy_token' : ActorMethod<[bigint, [] | [Uint8Array | number[]]], Result>,
'get_token_price_icp' : ActorMethod<[bigint], Result_1>,
}
Loading

0 comments on commit 6330f45

Please sign in to comment.