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

refactored contract for NCD.L2 frontend repos #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
27 changes: 6 additions & 21 deletions assembly/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,27 @@ let schema = ["🟣", "🟡️️", "⚫️", "⭕️", "🔘"];

export function generate(seed: i32) : string {
let output : string = "";

let a : i32 = 0;

// TODO move this to index
if (seed == 0) {
a = <i32>randomNum();
logging.log(`\n\n\tCall claimMyDesign with the seed number ${a} to claim it.\n`)
} else {
a = <i32>seed;
}

let x : i32 = 0;
let y : i32 = 0;
let v : i32 = 0;
let value : string = "";
let mod = (a % 11) + 5;
let mod = (seed % 11) + 5;

for (let i = 0; i < SIZE; i++) {
y = (2 * (i - HALF_SIZE) + 1);
if (a % 3 == 1) {
if (seed % 3 == 1) {
y = -y;
} else if (a % 3 == 2) {
} else if (seed % 3 == 2) {
y = <i32>abs(y);
}
y = y * a;
y = y * seed;
for (let j = 0; j < SIZE; j++) {
x = (2 * (j - HALF_SIZE) + 1);
if (a % 2 == 1) {
if (seed % 2 == 1) {
x = <i32>abs(x);
}
x = x * a;
x = x * seed;
v = <i32>(abs(x * y / ONE) % mod);
if (v < 5) {
value = schema[v];
Expand All @@ -57,8 +47,3 @@ function abs(n: i32) : i32 {
if (n >= 0) return n;
return -n;
}

function randomNum(): u32 {
const rng = new RNG<u32>(1, <u32>context.blockIndex);
return rng.next()
}
70 changes: 59 additions & 11 deletions assembly/index.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,92 @@
import { logging, context } from 'near-sdk-as';
import { logging, RNG, context, PersistentSet } from 'near-sdk-as';
import { generate } from './generate';
import { Design, designs, owners } from './models';
import { Design, designs, owners, tempDesigns } from './models';


function randomNum(): u32 {
const rng = new RNG<u32>(1, <u32>context.blockIndex);
return rng.next()
}

export function claimMyDesign(seed: i32) : void {
assert(seed >= 0, "Seed needs to be valid.");
assert(!designs.contains(context.sender), "You can only own one design.")

let instructions = generate(seed);

let design = new Design(instructions);
let design = new Design(seed, instructions);

logging.log(`\n\n\t> ART / Seed: ${seed} \n\n\t` + instructions.replaceAll("\n", "\n\t") + "\n")

logging.log("\n\n\tClaimed Art")

designs.set(context.sender, design);

owners.add(context.sender);
}

export function viewMyDesign() : void {
let design = designs.getSome(context.sender);

logging.log(`\n\n\t> Your Art \n\n\t` + design.instructions.replaceAll("\n", "\n\t") + "\n")
export function viewMyDesign(accountId: string) : Design | null{
if (designs.contains(accountId)) {
let design = designs.getSome(accountId);
logging.log(`\n\n\t> Your Art \n\n\t` + design.instructions.replaceAll("\n", "\n\t") + "\n")
return design;
}

return null
}

export function burnMyDesign() : void {
assert(designs.contains(context.sender), "No design to burn here.");

logging.log("\n\n\t Found design")
designs.delete(context.sender);
owners.delete(context.sender);

logging.log("\n\n\t Error is not in delete design")

if (owners.has(context.sender)) {
owners.delete(context.sender);
}

logging.log(`\n\n\t> Design burned \n\n\t`)
}

export function design(seed : i32 = 0) : void {
let instructions = generate(seed);
export function getOwners() : number {
return owners.size;
}

export function design( seed : i32 = 0) : void {
let a : i32 = 0;

if (seed == 0) {
a = <i32>randomNum();
logging.log(`\n\n\tCall claimMyDesign with the seed number ${a} to claim it.\n`)
} else {
a = <i32>seed;
}

let instructions = generate(a);

logging.log(`\n\n\t> ART \n\n\t` + instructions.replaceAll("\n", "\n\t") + "\n")

logging.log('new one')

if (tempDesigns.contains(context.sender)) {
tempDesigns.delete(context.sender)
}
tempDesigns.set(context.sender, new Design(a, instructions))
}

export function getTempDesign( accountId : string) : Design | null{
if (tempDesigns.contains(accountId)) {
logging.log('You already have generated design')
} else {
logging.log('You don`t have generated design yet')
}

//assert(tempDesigns.contains(accountId), "No designs generated for user with such accountId")
if (tempDesigns.contains(accountId)) {
return tempDesigns.getSome(accountId)
}
return null
}

export function viewDesigns() : void {
Expand Down
5 changes: 4 additions & 1 deletion assembly/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type AccountId = string;
export class Design {
owner: string;
constructor(
public seed: i32,
public instructions: string
) {
this.owner = context.sender;
Expand All @@ -14,4 +15,6 @@ export class Design {

export const designs = new PersistentMap<AccountId, Design>("d");

export const owners = new PersistentSet<AccountId>("o")
export const owners = new PersistentSet<AccountId>("o")

export const tempDesigns = new PersistentMap<AccountId, Design>("temp");