Skip to main content

Deploy Smart Contract

The first step is to originate (deploy) the ownership Smart Contract with completium CLI.

Before anything, create a new account and provide it with some ghostnet tez:

  1. Generate a new account with the following completium-cli command in a VS code Terminal tab:
completium-cli generate account as first-dapp-account

where first-dapp-account is the logical name of the account; you may give it another name.

  1. Copy the tz1... address generated by command above, and go to ghostnet faucet; provide the account with ghostnet by setting the tz1... address in the "Or fund any address" section.

  2. Set default endpoint with:

completium-cli set endpoint https://ghostnet.ecadinfra.com
  1. Set default account to first-dapp-account:
completium-cli set account first-dapp-account
  1. Check that balance is strictly positive with:
completium-cli show account

Smart contract code

note

This section is for information only, no action is required.

The contract is written in Archetype language. The source code is available in the contract folder.

archetype asset_ownership(owner : address)

variable assetid : bytes =
0x68746ecbcd72793aefda48f1b67a3190fc380a7633055d2336fb90cd990582a2

variable bestbidder : address = owner
variable bestbid : tez = 0tz

variable endofbid : option<date> = none

states =
| Owned initial
| ForSale

transition upforsale (price : tez) {
called by owner
from Owned to ForSale
with effect {
bestbid := price;
endofbid := some((now + 5m));
}
}

entry bid() {
state is ForSale otherwise "Asset Not For Sale"
require {
r1: endofbid ? now < the : false otherwise "Bid Period Is Over";
r2: caller <> bestbidder otherwise "Called By Best Bidder";
r3: transferred > bestbid otherwise "Invalid Transferred Amount";
}
effect {
if balance <> transferred then
transfer bestbid to bestbidder;
bestbidder := caller;
bestbid := transferred;
endofbid := some((now + 2m));
}
}

transition claim () {
require { r5: endofbid ? now > the : false otherwise "Bid Period Is Still On" }
from ForSale to Owned
with effect {
if balance > 0tz then
transfer balance to owner;
owner := bestbidder;
}
}

Originate contract

From Archetype

Enter this command in the Terminal:

completium-cli deploy ./contract/ownership.arl --parameters '{ "owner" : "tz1h4CiqWxNe4UxSpkwXy617RM6DaK6NU76P" }'
warning

Replace address tz1h4CiqWxNe4UxSpkwXy617RM6DaK6NU76P by the first-dapp-account account you generated (run completium-cli show account to display the address - Public Key hash).

It displays the main origination parameters and asks for confirmation. Enter Y and press enter.

The output should look like:

$ completium-cli deploy ./contract/ownership.arl --parameters '{ "owner" : "tz1h4CiqWxNe4UxSpkwXy617RM6DaK6NU76P" }' --force
Originate settings:
network : ghostnet
contract : ownership
as : first-dapp-account
send : 0
storage : (Pair "tz1h4CiqWxNe4UxSpkwXy617RM6DaK6NU76P" (Pair 0x68746ecbcd72793aefda48f1b67a3190fc380a7633055d2336fb90cd990582a2 (Pair "tz1h4CiqWxNe4UxSpkwXy617RM6DaK6NU76P" (Pair 0 (Pair 1635064614 0)))))
total cost : 0.42918
Waiting for confirmation of origination for KT1PPMXvCQh2g3b4YP4ovha5ZwpbKhh5xNh5 ...
Origination completed for KT1PPMXvCQh2g3b4YP4ovha5ZwpbKhh5xNh5 named ownership.
https://better-call.dev/ghostnet/KT1PPMXvCQh2g3b4YP4ovha5ZwpbKhh5xNh5

Click on the generated link to display the contract in Better Call Dev indexer (it may take up to a dozen of seconds for BCD to synchronize with the blockchain). It shows the origination cost of 0.39ꜩ.

From Michelson

In order to originate from the Michelson version (available in contract folder), enter the following command :

completium-cli originate ./contract/ownership.tz --init '(Pair "tz1h4CiqWxNe4UxSpkwXy617RM6DaK6NU76P" (Pair 0x68746ecbcd72793aefda48f1b67a3190fc380a7633055d2336fb90cd990582a2 (Pair "tz1h4CiqWxNe4UxSpkwXy617RM6DaK6NU76P" (Pair 0 (Pair 1624952132 0)))))'
warning

Replace twice address tz1h4CiqWxNe4UxSpkwXy617RM6DaK6NU76P by the first-dapp-account account you generated (run completium-cli show account to display the address - Public Key hash).

Contract API

note

This section is for information only, no action is required.

This section presents the contract API.

Storage

The contract stores the asset id and the owner address:

NameTypeDescriptionInitial Value
assetIdbytesHash code of the asset.0x68746e... (see below)
owneraddressAddress of the current asset owner.tz1M...ACw (see below)

It stores 4 extra variables used to implement the transfer of ownership process:

NameTypeDescriptionInitial Value
bestbidderaddressAddress of the best bidder; it is equal to the owner address when asset is not for sale.owner
bestbidtezBest bid amount.0tz
endofbidoption of dateDate of the end of bid.none
_stateintValue is either 0 (not for sale) or 1 (for sale).0 (not for sale)

Entrypoints

The ownership contract provides three entrypoints:

NameParameter(s)Description
upforsaleminimum selling priceCalled by current owner to open bid process. Resulting contract state is ForSale.
bidCalled by anyone. It requires:
  • the asset to be up for sale
  • the bid period is not over
  • the transferred amount is above the current best bid amount
If these conditions are met, it transfers back the previous best bid amount to the previous best bidder, and updates the bestbidder and bestbid variables with new values; endofbid is incremented by 2 minutes.
claimCalled by anyone. It requires:
  • the asset to be up for sale
  • the bid period to be over
If these condidions are met, it transfers the best bid value to the previous owner, and sets the owner variable to the best bidder address. Contract state is set back to "not for sale".