Created
October 25, 2020 23:39
-
-
Save graemecode/c97676f651f834cb67144b52a5b0fb32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface Entity { | |
id: string; | |
forSale: boolean, | |
price: number, | |
// A list of components | |
components: Component[], | |
} | |
export interface Component { | |
// STUB. | |
// static Schema | |
// public data | |
} | |
export interface NFTStateInterface { | |
name: string; | |
ticker: string; | |
entities: Entity[]; | |
ownership: OwnershipInterface; | |
} | |
interface ActionInterface { | |
input: InputInterface; | |
caller: string; | |
} | |
export interface InputInterface extends NFTInteractionInterface { | |
function: GetFunctionType | SetFunctionType; | |
} | |
export interface NFTInteractionInterface { | |
entityId?: string, | |
entity?: Entity; | |
price?: number; | |
} | |
interface OwnershipInterface { | |
[owner: string]: string; | |
} | |
export type GetFunctionType = 'owner' | 'entities'; | |
export type SetFunctionType = 'create' | 'purchase' | 'setPrice'; | |
function handle(state: NFTStateInterface, action: ActionInterface): { state: NFTStateInterface } | { result: any } { | |
const entities: Entity[] = state.entities; | |
const ownership: OwnershipInterface = state.ownership; | |
const caller: string = action.caller; | |
const input: InputInterface = action.input; | |
if (input.function === 'create') { | |
const { entity } = input; | |
const { id: entityId } = entity; | |
// Add to registry and assign ownership. | |
entities.push(entity); | |
ownership[entityId] = caller; | |
} | |
if (input.function === 'setPrice') { | |
const {entityId, price} = input; | |
if (ownership[entityId] === caller) { | |
const entity = entities.filter((entity) => entity.id === entityId)[0]; | |
entity.price = price; | |
} | |
return { state }; | |
} | |
if (input.function === 'purchase') { | |
const {entityId} = input; | |
const { | |
forSale, | |
price | |
} = entities.filter(entity => entity.id = entityId)[0]; | |
if (forSale) { | |
ownership[entityId] = caller; | |
} | |
return { state }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment