Created
January 8, 2021 03:22
-
-
Save samcamwilliams/dd947a755bf605daf4c9aac365704444 to your computer and use it in GitHub Desktop.
A Verto-compatible SmartWeave contract for trading physical assets.
This file contains hidden or 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 function handle (state, action) { | |
const owner = state.owner | |
const input = action.input | |
const caller = action.caller | |
const contact = action.contact | |
if (input.function === 'transfer') { | |
const target = input.target | |
if (!target || (caller === target)) { | |
throw new ContractError('Invalid target for transfer') | |
} | |
if (caller !== owner) { | |
throw new ContractError(`Caller does not own the item`) | |
} | |
owner = target | |
return { state } | |
} | |
if (input.function === 'redeem') { | |
if (caller !== owner) { | |
throw new ContractError(`Caller does not own the item`) | |
} | |
state.contact = input.contact | |
state.owner = undefined | |
return { state } | |
} | |
if (input.function === 'balance') { | |
const target = input.target | |
const ticker = state.ticker | |
if (typeof target !== 'string') { | |
throw new ContractError('Must specificy target to get balance for') | |
} | |
return { result: { target, ticker, balance: (target === owner) ? 1 : 0 } } | |
} | |
throw new ContractError(`No function supplied or function not recognised: "${input.function}"`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initialisation state should be something like...