Difference between revisions of "Basic examples"
| Line 3: | Line 3: | ||
= Dragon = | = Dragon = | ||
| + | When one company makes a digital pet dragon, another can make a house to live in. Jigs interact with each other. | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class Dragon extends Jig { | |
| + | init(name) { | ||
| + | this.name = name | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | class DragonHouse extends Jig { | ||
| + | init(dragon) { | ||
| + | this.dragon = dragon | ||
| + | } | ||
| + | |||
| + | clean() { | ||
| + | this.dirty = false | ||
| + | } | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | + | = Axe = | |
| + | |||
| + | The base class is the parent of all weapons in the game. Each weapon subclass independently sets the damage it delivers. Then you check whether a particular weapon is the correct type to allow in the game. | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class Weapon extends Jig { | |
| + | init(damage) { | ||
| + | this.damage = damage | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | class Axe extends Weapon { | ||
| + | init() { | ||
| + | super.init(9001) | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | new Axe() instanceof Weapon // true | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | Loyalty Card | + | = Loyalty Card = |
| + | |||
| + | Any card you’d have in your iOS or Android wallet: boarding passes, prepaid cards. | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class LoyaltyCard extends Jig { | |
| + | init() { | ||
| + | this.stamps = 0 | ||
| + | } | ||
| + | |||
| + | stamp() { | ||
| + | this.stamps += 1 | ||
| + | } | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | Product | + | = Product = |
| + | |||
| + | Track a physical good from manufacturing through delivery to the customer. Store both the unique serial number and the shared model number. When you transfer ownership, the official record is on-chain. | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class Product extends Jig { | |
| + | init(model, serial, date) { | ||
| + | this.model = model | ||
| + | this.serial = serial | ||
| + | this.creation = date | ||
| + | } | ||
| + | |||
| + | transfer(to) { | ||
| + | this.owner = to | ||
| + | } | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | Vote | + | = Vote = |
| + | |||
| + | For any recorded decision, you can build a graphical user interface with a dropdown selection. Then, after a milestone, tally up the votes and set in motion the transition of power. | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class Vote extends Jig { | |
| + | vote(candidate) { | ||
| + | this.candidate = candidate | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | class Election extends Jig { | ||
| + | // ... | ||
| + | |||
| + | tally() { | ||
| + | const counts = { 'Alice': 0, 'Bob': 0, 'Carol': 0 } | ||
| + | this.voters.forEach((name, vote) => { | ||
| + | counts[vote.candidate] += 1 | ||
| + | }) | ||
| + | return counts | ||
| + | } | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | Dollar | + | = Dollar = |
| + | |||
| + | Directly or indirectly link backing to a token. Send numerical amounts and provide the functionality of SLP and ERC20 tokens. | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class Dollar extends Token { } | |
| + | |||
| + | Dollar.decimals = 2 | ||
| + | Dollar.currency = 'USD' | ||
| + | Dollar.backingBank = 'HSBC' | ||
| + | Dollar.accountNumber = '#103947000' | ||
| + | |||
| + | run.deploy(Dollar) | ||
| + | |||
| + | const coin = Dollar.mint(100000) | ||
| + | |||
| + | coin.send(address, 1000) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | Permissions | + | = Permissions = |
| + | |||
| + | Allow several people edit access to a website or raise a digital pet together with your friends. Also updates to the permissions are as easy as a cheap bitcoin transaction. | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| − | + | class User extends Jig { } | |
| + | |||
| + | |||
| + | class UserGroup extends Jig { | ||
| + | init() { | ||
| + | this.users = [] | ||
| + | } | ||
| + | |||
| + | addUser(user) { | ||
| + | if (!this.users.includes(user)) { | ||
| + | this.users.push(user) | ||
| + | } | ||
| + | } | ||
| + | |||
| + | removeUser(user) { | ||
| + | this.users = this.users.filter(u => user !== u) | ||
| + | } | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 18:42, 17 March 2023
These examples were originally found on run.network's homepage
Dragon
When one company makes a digital pet dragon, another can make a house to live in. Jigs interact with each other.
class Dragon extends Jig {
init(name) {
this.name = name
}
}
class DragonHouse extends Jig {
init(dragon) {
this.dragon = dragon
}
clean() {
this.dirty = false
}
}
Axe
The base class is the parent of all weapons in the game. Each weapon subclass independently sets the damage it delivers. Then you check whether a particular weapon is the correct type to allow in the game.
class Weapon extends Jig {
init(damage) {
this.damage = damage
}
}
class Axe extends Weapon {
init() {
super.init(9001)
}
}
new Axe() instanceof Weapon // true
Loyalty Card
Any card you’d have in your iOS or Android wallet: boarding passes, prepaid cards.
class LoyaltyCard extends Jig {
init() {
this.stamps = 0
}
stamp() {
this.stamps += 1
}
}
Product
Track a physical good from manufacturing through delivery to the customer. Store both the unique serial number and the shared model number. When you transfer ownership, the official record is on-chain.
class Product extends Jig {
init(model, serial, date) {
this.model = model
this.serial = serial
this.creation = date
}
transfer(to) {
this.owner = to
}
}
Vote
For any recorded decision, you can build a graphical user interface with a dropdown selection. Then, after a milestone, tally up the votes and set in motion the transition of power.
class Vote extends Jig {
vote(candidate) {
this.candidate = candidate
}
}
class Election extends Jig {
// ...
tally() {
const counts = { 'Alice': 0, 'Bob': 0, 'Carol': 0 }
this.voters.forEach((name, vote) => {
counts[vote.candidate] += 1
})
return counts
}
}
Dollar
Directly or indirectly link backing to a token. Send numerical amounts and provide the functionality of SLP and ERC20 tokens.
class Dollar extends Token { }
Dollar.decimals = 2
Dollar.currency = 'USD'
Dollar.backingBank = 'HSBC'
Dollar.accountNumber = '#103947000'
run.deploy(Dollar)
const coin = Dollar.mint(100000)
coin.send(address, 1000)
Permissions
Allow several people edit access to a website or raise a digital pet together with your friends. Also updates to the permissions are as easy as a cheap bitcoin transaction.
class User extends Jig { }
class UserGroup extends Jig {
init() {
this.users = []
}
addUser(user) {
if (!this.users.includes(user)) {
this.users.push(user)
}
}
removeUser(user) {
this.users = this.users.filter(u => user !== u)
}
}