Skip to content

Commit

Permalink
remove legacyPlaceholder and other legacy code
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenVoich committed Aug 7, 2023
1 parent bfaa99e commit c39d48b
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 194 deletions.
9 changes: 0 additions & 9 deletions INIT_ARGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,6 @@ dutchAuction = opt record {
};
```

## Legacy Placeholders
Default `null`

```candid
// true - the collection has the placeholder stored at the 0 index of the asset array
// false - the placeholder has its on stable variable in the canister
legacyPlaceholder = opt false
```

## Restore
Default `null`

Expand Down
8 changes: 0 additions & 8 deletions declarations/main/staging.did
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ type InitArgs =
airdrop: vec AccountIdentifier;
dutchAuction: opt DutchAuction;
escrowDelay: opt Duration;
legacyPlaceholder: opt bool;
marketDelay: opt Duration;
marketplaces: vec record {
text;
Expand Down Expand Up @@ -436,11 +435,6 @@ type HttpStreamingCallbackToken =
key: text;
sha256: opt blob;
};
type HttpStreamingCallbackResponse =
record {
body: blob;
token: opt HttpStreamingCallbackToken;
};
type HttpResponse =
record {
body: blob;
Expand Down Expand Up @@ -665,8 +659,6 @@ type Canister =
}) query;
grow: (nat) -> (nat);
http_request: (HttpRequest) -> (HttpResponse) query;
http_request_streaming_callback: (HttpStreamingCallbackToken) ->
(HttpStreamingCallbackResponse) query;
initCap: () -> (Result_4);
initMint: () -> (Result_4);
list: (ListRequest) -> (Result_3);
Expand Down
9 changes: 0 additions & 9 deletions declarations/main/staging.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ export interface Canister {
'getTokens' : ActorMethod<[], Array<[TokenIndex, Metadata__1]>>,
'grow' : ActorMethod<[bigint], bigint>,
'http_request' : ActorMethod<[HttpRequest], HttpResponse>,
'http_request_streaming_callback' : ActorMethod<
[HttpStreamingCallbackToken],
HttpStreamingCallbackResponse
>,
'initCap' : ActorMethod<[], Result_4>,
'initMint' : ActorMethod<[], Result_4>,
'list' : ActorMethod<[ListRequest], Result_3>,
Expand Down Expand Up @@ -241,10 +237,6 @@ export interface HttpResponse {
'streaming_strategy' : [] | [HttpStreamingStrategy],
'status_code' : number,
}
export interface HttpStreamingCallbackResponse {
'token' : [] | [HttpStreamingCallbackToken],
'body' : Uint8Array | number[],
}
export interface HttpStreamingCallbackToken {
'key' : string,
'sha256' : [] | [Uint8Array | number[]],
Expand All @@ -260,7 +252,6 @@ export type HttpStreamingStrategy = {
export interface InitArgs {
'timersInterval' : [] | [Duration],
'dutchAuction' : [] | [DutchAuction],
'legacyPlaceholder' : [] | [boolean],
'whitelists' : Array<Whitelist>,
'marketplaces' : Array<[string, AccountIdentifier, bigint]>,
'name' : string,
Expand Down
11 changes: 0 additions & 11 deletions declarations/main/staging.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const idlFactory = ({ IDL }) => {
const InitArgs = IDL.Record({
'timersInterval' : IDL.Opt(Duration),
'dutchAuction' : IDL.Opt(DutchAuction),
'legacyPlaceholder' : IDL.Opt(IDL.Bool),
'whitelists' : IDL.Vec(Whitelist),
'marketplaces' : IDL.Vec(IDL.Tuple(IDL.Text, AccountIdentifier, IDL.Nat64)),
'name' : IDL.Text,
Expand Down Expand Up @@ -421,10 +420,6 @@ export const idlFactory = ({ IDL }) => {
'streaming_strategy' : IDL.Opt(HttpStreamingStrategy),
'status_code' : IDL.Nat16,
});
const HttpStreamingCallbackResponse = IDL.Record({
'token' : IDL.Opt(HttpStreamingCallbackToken),
'body' : IDL.Vec(IDL.Nat8),
});
const Result_4 = IDL.Variant({ 'ok' : IDL.Null, 'err' : IDL.Text });
const ListRequest = IDL.Record({
'token' : TokenIdentifier__1,
Expand Down Expand Up @@ -564,11 +559,6 @@ export const idlFactory = ({ IDL }) => {
),
'grow' : IDL.Func([IDL.Nat], [IDL.Nat], []),
'http_request' : IDL.Func([HttpRequest], [HttpResponse], ['query']),
'http_request_streaming_callback' : IDL.Func(
[HttpStreamingCallbackToken],
[HttpStreamingCallbackResponse],
['query'],
),
'initCap' : IDL.Func([], [Result_4], []),
'initMint' : IDL.Func([], [Result_4], []),
'list' : IDL.Func([ListRequest], [Result_3], []),
Expand Down Expand Up @@ -682,7 +672,6 @@ export const init = ({ IDL }) => {
const InitArgs = IDL.Record({
'timersInterval' : IDL.Opt(Duration),
'dutchAuction' : IDL.Opt(DutchAuction),
'legacyPlaceholder' : IDL.Opt(IDL.Bool),
'whitelists' : IDL.Vec(Whitelist),
'marketplaces' : IDL.Vec(IDL.Tuple(IDL.Text, AccountIdentifier, IDL.Nat64)),
'name' : IDL.Text,
Expand Down
15 changes: 0 additions & 15 deletions deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,10 @@ let filesByName = new Map(files.map((file) => {
return [path.parse(file).name, file];
}));

// TODO: remove
// populate with fake data for cherries
let assets = JSON.parse(fs.readFileSync(path.resolve(assetsDir, 'metadata.json')).toString());
for (let i = 0; i < assets.length; i++) {
filesByName.set(String(i + 1), `${i + 1}.svg`);
filesByName.set(String(i + 1) + '_thumbnail', `${i + 1}_thumbnail.png`);
}
filesByName.set('placeholder', 'placeholder.mp4');

let run = async () => {
deployNftCanister();
await uploadAssetsMetadata();
launch();
// deployAssetsCanister();
}

let getAssetUrl = (filename) => {
Expand All @@ -87,11 +77,6 @@ let deployNftCanister = () => {
execSync(`dfx deploy ${nftCanisterName} --no-wallet --argument "$(cat ${initArgsFile})" --network ${dfxNetwork} ${modeArg} ${withCyclesArg}`, execOptions);
}

// let deployAssetsCanister = () => {
// console.log(chalk.green('Deploying assets canister...'));
// execSync(`dfx deploy assets --no-wallet --network ${dfxNetwork} ${withCyclesArg}`, execOptions);
// }

let uploadAssetsMetadata = async () => {
let assets = JSON.parse(fs.readFileSync(path.resolve(assetsDir, 'metadata.json')).toString());

Expand Down
124 changes: 16 additions & 108 deletions src/CanisterAssets/lib.mo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Buffer "mo:base/Buffer";
import Types "types";
import RootTypes "../types";
import Utils "../utils";
import Debug "mo:base/Debug";

module {

Expand All @@ -33,25 +34,20 @@ module {
thumbnailUrl = null;
};

let _bytesPerChunk = 500_000; // 500kb
var _biggestAssetSize = 10_000; // 10kb

public func getChunkCount() : Nat {
var count = _assets.size() * _biggestAssetSize / _bytesPerChunk;
if (_assets.size() * _biggestAssetSize % _bytesPerChunk != 0) {
public func getChunkCount(chunkSize : Nat) : Nat {
var count = _assets.size() / chunkSize;
if (_assets.size() % chunkSize != 0) {
count += 1;
};
return Nat.max(1, count);
Nat.max(1, count);
};

public func toStableChunk(chunkSize_ignored : Nat, chunkIndex : Nat) : Types.StableChunk {
let chunkSize = _bytesPerChunk / _biggestAssetSize;
public func toStableChunk(chunkSize : Nat, chunkIndex : Nat) : Types.StableChunk {
let start = Nat.min(_assets.size(), chunkSize * chunkIndex);
let count = Nat.min(chunkSize, _assets.size() - start);
let assetsChunk = if (_assets.size() == 0 or count == 0) {
[]
}
else {
} else {
Buffer.toArray(Buffer.subBuffer(_assets, start, count));
};

Expand All @@ -61,11 +57,9 @@ module {
assetsCount = _assets.size();
assetsChunk;
});
}
else if (chunkIndex < getChunkCount()) {
} else if (chunkIndex < getChunkCount(chunkSize)) {
return ?#v2_chunk({ assetsChunk });
}
else {
} else {
null;
};
};
Expand All @@ -86,119 +80,31 @@ module {
case (?#v1(data)) {
_assets := Buffer.Buffer<Types.AssetV2>(data.assetsCount);
_assets.append(Buffer.fromArray(toV2(data.assetsChunk)));
_updateBiggestAssetSize(data.assetsChunk);
};
case (?#v1_chunk(data)) {
_assets.append(Buffer.fromArray(toV2(data.assetsChunk)));
_updateBiggestAssetSize(data.assetsChunk);
};
// v2
case (?#v2(data)) {
_placeholder := data.placeholder;
_assets := Buffer.Buffer<Types.AssetV2>(data.assetsCount);
_assets.append(Buffer.fromArray(data.assetsChunk));
_updateBiggestAssetSize(data.assetsChunk);
};
case (?#v2_chunk(data)) {
_assets.append(Buffer.fromArray(data.assetsChunk));
_updateBiggestAssetSize(data.assetsChunk);
};
case (null) {};
};
};

func _updateBiggestAssetSize(assets : [Types.Asset]) {
for (asset in assets.vals()) {
var assetSize = asset.payload.data.size();
switch (asset.thumbnail) {
case (?thumbnail) {
assetSize += thumbnail.data.size();
};
case (null) {};
};
_biggestAssetSize := Nat.max(_biggestAssetSize, assetSize);
public func _checkLegacyAsset(asset : Types.AssetV2) {
if (asset.thumbnail != null or asset.metadata != null or asset.payload.data.size() != 0) {
Debug.trap("Legacy asset detected. Please use separate asset canister and add only redirect URLs to the collection");
};
};

//*** ** ** ** ** ** ** ** ** * * PUBLIC INTERFACE * ** ** ** ** ** ** ** ** ** ** /

// legacy
public func streamAsset(caller : Principal, id : Nat, isThumb : Bool, payload : Blob) : () {
assert (caller == config.minter);
var asset : Types.AssetV2 = _assets.get(id);
if (isThumb) {
switch (asset.thumbnail) {
case (?t) {
asset := {
name = asset.name;
thumbnail = ?{
ctype = t.ctype;
data = Array.append(t.data, [payload]);
};
payload = asset.payload;
metadata = asset.metadata;
payloadUrl = asset.payloadUrl;
thumbnailUrl = asset.thumbnailUrl;
};
};
case (_) {};
};
} else {
asset := {
name = asset.name;
thumbnail = asset.thumbnail;
payload = {
ctype = asset.payload.ctype;
data = Array.append(asset.payload.data, [payload]);
};
metadata = asset.metadata;
payloadUrl = asset.payloadUrl;
thumbnailUrl = asset.thumbnailUrl;
};
};
_assets.put(id, asset);
_updateBiggestAssetSize([asset]);
};

// legacy
public func updateThumb(caller : Principal, name : Text, file : Types.File) : ?Nat {
assert (caller == config.minter);
var i : Nat = 0;
for (a in _assets.vals()) {
if (a.name == name) {
var asset : Types.AssetV2 = _assets.get(i);
asset := {
name = asset.name;
thumbnail = ?file;
payload = asset.payload;
metadata = asset.metadata;
payloadUrl = asset.payloadUrl;
thumbnailUrl = asset.thumbnailUrl;
};
_assets.put(i, asset);
_updateBiggestAssetSize([asset]);
return ?i;
};
i += 1;
};
return null;
};

// legacy
public func addAsset(caller : Principal, asset : Types.AssetV2) : Nat {
assert (caller == config.minter);
if (config.singleAssetCollection == ?true) {
if (Utils.toNanos(config.revealDelay) > 0) {
assert (_assets.size() < 2);
} else {
assert (_assets.size() == 0);
};
};
_assets.add(asset);
_updateBiggestAssetSize([asset]);
_assets.size() - 1;
};

public func addAssets(caller : Principal, assets : [Types.AssetV2]) : Nat {
assert (caller == config.minter);
if (config.singleAssetCollection == ?true) {
Expand All @@ -208,13 +114,16 @@ module {
assert (_assets.size() == 0);
};
};
for (asset in assets.vals()) {
_checkLegacyAsset(asset);
};
_assets.append(Buffer.fromArray(assets));
_updateBiggestAssetSize(assets);
_assets.size() - 1;
};

public func addPlaceholder(caller : Principal, asset : Types.AssetV2) {
assert (caller == config.minter);
_checkLegacyAsset(asset);
_placeholder := asset;
};

Expand Down Expand Up @@ -245,6 +154,5 @@ module {
public func vals() : Iter.Iter<Types.AssetV2> {
_assets.vals();
};

};
};
4 changes: 1 addition & 3 deletions src/Ext/lib.mo
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ module {

public func getTokenToAssetMapping() : [(Types.TokenIndex, Text)] {
var resp : Buffer.Buffer<(Types.TokenIndex, Text)> = Buffer.Buffer(0);
// legacy placeholder is stored in asset 0
let startIndex = if (config.legacyPlaceholder == ?true and Utils.toNanos(config.revealDelay) > 0) { 1 } else { 0 };
for (e in deps._Tokens.getTokenMetadata().entries()) {
let assetid = deps._Assets.get(if (config.singleAssetCollection == ?true) startIndex else Nat32.toNat(e.0) + startIndex).name;
let assetid = deps._Assets.get(if (config.singleAssetCollection == ?true) 0 else Nat32.toNat(e.0)).name;
resp.add((e.0, assetid));
};
Buffer.toArray(resp);
Expand Down
5 changes: 2 additions & 3 deletions src/Http/lib.mo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module {
let tokenId = _getParam(request, "tokenid")!;

// if not revealed yet, return placeholder
// only when accessed by tokenid??
if (Utils.toNanos(config.revealDelay) > 0 and not deps._Shuffle.isShuffled()) {
let placeholder = deps._Assets.getPlaceholder();
switch (placeholder.payloadUrl) {
Expand All @@ -46,7 +45,7 @@ module {
return _processAsset(request, asset);
};

// by asset id (same as token id??)
// by asset id
// https://.../?asset=<assetId>
ignore do ? {
let atext = _getParam(request, "asset")!;
Expand Down Expand Up @@ -87,7 +86,7 @@ module {
func _processAsset(request : Types.HttpRequest, asset : AssetTypes.AssetV2) : Types.HttpResponse {
let t = switch (_getParam(request, "type")) {
case (?t) { t };
case (null) { ""};
case (null) { "" };
};

// return metadata
Expand Down
3 changes: 1 addition & 2 deletions src/Marketplace/lib.mo
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ module {
let count = Nat.min(chunkSize, _transactions.size() - start);
let transactionChunk = if (_transactions.size() == 0 or count == 0) {
[]
}
else {
} else {
Buffer.toArray(Buffer.subBuffer(_transactions, start, count));
};

Expand Down
Loading

0 comments on commit c39d48b

Please sign in to comment.