Skip to content

Instantly share code, notes, and snippets.

@samcamwilliams
Created January 8, 2021 03:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samcamwilliams/dd947a755bf605daf4c9aac365704444 to your computer and use it in GitHub Desktop.
Save samcamwilliams/dd947a755bf605daf4c9aac365704444 to your computer and use it in GitHub Desktop.
A Verto-compatible SmartWeave contract for trading physical assets.
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}"`)
}
@samcamwilliams
Copy link
Author

Initialisation state should be something like...

{
    "ticker": "Sam's Verto Sticker (Jan 2021)",
    "owner": "vLRHFqCw1uHu75xqB4fCDW-QxpkpJxBtFD9g4QYUbfw"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment