Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              ### Interaction

<script src="https://cdn.cosmic.plus/stellar-sdk"></script>
<script src="https://cdn.cosmic.plus/cosmic-lib@2.x"></script>


<form>
  <fieldset><legend>#template</legend>
    <pre id="template"></pre>
  </fieldset>

  <fieldset><legend>#amount</legend>
    <input id="amount" type="number" step="1" placeholder="Amount (in Lumens)">
  </fieldset>

  <fieldset><legend>#alice</legend>
    <input id="alice" type="text" placeholder="Public Key or Federated Address">
  </fieldset>

  <fieldset><legend>#bob</legend>
    <input id="bob" type="text" placeholder="Public Key or Federated Address">
  </fieldset>

  <fieldset><legend>#submit</legend>
    <input id="submit" type="submit" value="Submit">
  </fieldset>
  
  <fieldset><legend>#cosmiclink_description</legend>
    <pre id="cosmiclink_description"></pre>
  </fieldset>
</form>
  

### Description

This pen fills a contract template with user-entered inputs and uses `cosmic-lib` to build and pass the transaction request to the user's wallet.

### Concepts

- [Stellar Transactions](https://www.stellar.org/developers/guides/concepts/transactions.html): commands that modify the Stellar ledger.
- [CosmicLink](https://cosmic.plus/#view:js-cosmic-lib/index.html#what-are-cosmiclinks): a protocol that passes transaction requests from applications to wallets.



### Keypoints

- By signing this contract, the user sends an equal **#amount** of Lumens to both **#alice** and **#bob**.
- **#alice** and **#bob** are just convenient variable names that can be set to any valid account.
- This kind of transaction is called "contract" because both operations either succeed or fail together. This is also known as _atomic transactions_.
- Strictly speaking, this is not a _smart contract_ yet, but a mere contract that source parties can either sign or reject.
- Nonetheless, this is a solid foundation for more sophisticated constructs.

### Challenges

- **easy/** Change this pen to make **#template** editable.
- **hard/** Edit this pen to have **#bob** opening a trustline for an `XYZ` asset emitted by **#alice**, then have **#alice** sends **#amount** `XYZ` to **#bob**. 

_Give it a try by editing the code, and be the first to post a solution on GalacticTalk!_

### Documentation

- [cosmic-lib documentation](https://cosmic.plus/#view:js-cosmic-lib)
- [Stellar Multisig](https://www.stellar.org/developers/guides/concepts/multi-sig.html) (for advanced constructs)
- [Stellar Smart Contracts](https://www.stellar.org/developers/guides/walkthroughs/stellar-smart-contracts.html) (for advanced constructs)


### Social

- [Discuss that pen on GalacticTalk](https://galactictalk.org/d/2150-pen-of-the-week-cosmic-lib-contract-template)
            
          
!

CSS

            
              
            
          
!

JS

            
              // $ npm install stellar-sdk cosmic-lib
// const cosmicLib = require("cosmic-lib")
const { CosmicLink } = cosmicLib
const $ = document.querySelector.bind(document)

/* Contract Template */
const template = `{
  "network": "test",
  "operations": [{
    "type": "payment",
    "destination": "#alice",
    "amount": "#amount"
  }, {
    "type": "payment",
    "destination": "#bob",
    "amount": "#amount"
  }]
}`

/* Logic */
function applyContract (template, parameters) {
  const bindings = Object.entries(parameters)
  const json = fillTemplate(template, bindings)
  const cosmicLink = new CosmicLink(json)
  cosmicLink.open()
}

/* Helper */
function fillTemplate (template, bindings) {
  bindings.forEach(([name, value]) => {
    template = template.split(`#${name}`).join(value)
  })
  return template
}

/* Interface */
$("#template").textContent = template
$("#submit").onclick = event => {
    event.preventDefault()
    applyContract(template, {
      amount: $("#amount").value,
      alice: $("#alice").value,
      bob: $("#bob").value
    })
}
            
          
!
999px

Console