Skip to content

FigureTechnologies/restricted-marker-transfer-smart-contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Restricted Marker Transfer Smart Contact

This contract facilitates the transfer of restricted coin between addresses.

Status

Latest Release Apache 2.0 License Code Coverage

Background

As a holder of a restricted marker, there is no way to transfer those coins without being granted marker transfer permissions or having an account with the permissions to initiate the transfer. This contract allows an account that holds a restricted marker to initiate a transfer that can then be approved or rejected by an account with transfer permission.

Assumptions

This README assumes you are familiar with writing and deploying smart contracts to the provenance blockchain. See the provwasm tutorial for details.

Provenance Testnet Deployments

Contract Version Code ID
0.1.0 157
0.1.1 166
1.0.0 552
2.0.0 584

Blockchain Quickstart

Checkout provenance v1.16.0, install the provenanced command and start a 4-node localnet.

git clone https://github.com/provenance-io/provenance.git
cd provenance && git checkout v1.16.0
make install
make localnet-start

Accounts

Accounts need to be set up for example users and marker transfer.

User 1

provenanced keys add user1 \
    --home build/node0 --keyring-backend test --testnet --hd-path "44'/1'/0'/0/0" --output json | jq

{
  "name": "user1",
  "type": "local",
  "address": "tp10nnm70y8zc5m8yje5zx5canyqq639j3ph7mj8p",
  "pubkey": "tppub1addwnpepqf4feq9n484c6tvpcugkp0l78mffld8aphq8wqehx53pekcf2l5pkuajggq",
  "mnemonic": "seminar tape camp attract student make hollow pyramid obtain bamboo exit donate dish drip text foil news film assist access pride decline reason lonely"
}

User 2

provenanced keys add user2 \
    --home build/node0 --keyring-backend test --testnet --hd-path "44'/1'/0'/0/0" --output json | jq

{
  "name": "user2",
  "type": "local",
  "address": "tp1m4arun5y9jcwkatq2ey9wuftanm5ptzsg4ppfs",
  "pubkey": "tppub1addwnpepqgw8y7dpx4xmlaun5u55qrq4e05jtul6nu94afq3tvr7e8d4xx6ujzf79jz",
  "mnemonic": "immense ordinary august exclude loyal expire install tongue ski bounce sock buffalo range begin glory inch index float medal kid empty wheel badge find"
}

Admin 1

provenanced keys add admin1 \
    --home build/node0 --keyring-backend test --testnet --hd-path "44'/1'/0'/0/0" --output json | jq

{
  "name": "admin1",
  "type": "local",
  "address": "tp15nauudez3yvrma9mfve7t9hnnnlkgc7fwps85d",
  "pubkey": "{\"@type\":\"/cosmos.crypto.secp256k1.PubKey\",\"key\":\"AlOF+u9+kMmP3mLlny+u2S7WBgDnJqJOwzJVXCFJZOgI\"}",
  "mnemonic": "develop glory absurd glory march valve hunt barely inform luxury ahead miss eye minimum assault meat pair shoot magic develop argue exact believe faint"
}

If you want to use the addresses from this document, use the mnemonics above to restore the keys locally.

For example:

provenanced keys add user1 --recover \
    --home build/node0 --keyring-backend test --testnet --hd-path "44'/1'/0'/0/0"

Fee Payment

Fund the example accounts with nhash to pay network fees.

provenanced tx bank send \
    $(provenanced keys show -a node0 --home build/node0 --keyring-backend test --testnet) \
    $(provenanced keys show -a user1 --home build/node0 --keyring-backend test --testnet) \
    100000000000nhash \
    --from node0 \
    --keyring-backend test \
    --home build/node0 \
    --chain-id chain-local \
    --gas auto --gas-prices 1905nhash --gas-adjustment 2 \
    --broadcast-mode block \
    --yes \
    --testnet -o json  | jq
provenanced tx bank send \
    $(provenanced keys show -a node0 --home build/node0 --keyring-backend test --testnet) \
    $(provenanced keys show -a user2 --home build/node0 --keyring-backend test --testnet) \
    100000000000nhash \
    --from node0 \
    --keyring-backend test \
    --home build/node0 \
    --chain-id chain-local \
    --gas auto --gas-prices 1905nhash --gas-adjustment 2 \
    --broadcast-mode block \
    --yes \
    --testnet -o json  | jq
provenanced tx bank send \
    $(provenanced keys show -a node0 --home build/node0 --keyring-backend test --testnet) \
    $(provenanced keys show -a admin1 --home build/node0 --keyring-backend test --testnet) \
    100000000000nhash \
    --from node0 \
    --keyring-backend test \
    --home build/node0 \
    --chain-id chain-local \
    --gas auto --gas-prices 1905nhash --gas-adjustment 2 \
    --broadcast-mode block \
    --yes \
    --testnet -o json  | jq

Store the Wasm

Store the optimized smart contract Wasm on-chain. This assumes you've copied artifacts/restricted_marker_transfer to the provenance root dir (ie where the localnet was started from).

provenanced tx wasm store restricted_marker_transfer.wasm \
  --from admin1 \
  --home build/node0 --keyring-backend test \
  --chain-id chain-local \
  --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
  --testnet \
  --yes -o json  | jq

Instantiate the contract

Instantiate the contract using the code_id returned from storing the Wasm. Note the contract address returned

build/provenanced tx wasm instantiate 17 \
  '{"name":"marker-transfer-local1" }' \
  --label restricted-marker-transfer1 \
  --admin $(provenanced keys show -a admin1 --home build/node0 --keyring-backend test --testnet) \
  --from admin1 \
  --home build/node0 --keyring-backend test \
  --chain-id chain-local \
  --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
  --testnet \
  --yes
logs:
- events:
  - attributes:
    - key: _contract_address
      value: tp15fnweczx7273jc6tmuuacmkl6zk6mq8ffh8r0artxp9srdpctcesek7uac
    - key: code_id
      value: "17"
    type: instantiate
  - attributes:
    - key: action
      value: /cosmwasm.wasm.v1.MsgInstantiateContract
    - key: module
      value: wasm
    - key: sender
      value: tp15nauudez3yvrma9mfve7t9hnnnlkgc7fwps85d
    type: message

Marker creation

Create a restricted marker representing private company stock for a company named example-co

provenanced tx marker new "50000example-co.stock" \
  --type RESTRICTED \
  --from admin1 \
  --home build/node0 --keyring-backend test \
  --chain-id chain-local \
  --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
  --testnet \
  --yes

Grant marker admin and transfer access to admin1

provenanced tx marker grant $(provenanced keys show -a admin1 --home build/node0 --keyring-backend test --testnet) example-co.stock admin,withdraw,burn,mint,transfer \
  --from admin1 \
  --home build/node0 --keyring-backend test \
  --chain-id chain-local \
  --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
  --testnet \
  --yes

Finalize the marker

provenanced tx marker finalize example-co.stock \
  --from admin1 \
  --home build/node0 --keyring-backend test \
  --chain-id chain-local \
  --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
  --testnet \
  --yes

Activate the marker

provenanced tx marker activate example-co.stock \
  --from admin1 \
  --home build/node0 --keyring-backend test \
  --chain-id chain-local \
  --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
  --testnet \
  --yes

Grant marker transfer permission to the smart contract, so it can move coin.

provenanced tx marker grant tp15fnweczx7273jc6tmuuacmkl6zk6mq8ffh8r0artxp9srdpctcesek7uac example-co.stock transfer \
  --from admin1 \
  --home build/node0 --keyring-backend test \
  --chain-id chain-local \
  --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
  --testnet \
  --yes

Now distribute shares of example-co stock to user1

provenanced tx marker withdraw example-co.stock 1000example-co.stock $(provenanced keys show -a user1 --home build/node0 --keyring-backend test --testnet)  \
  --from admin1 \
  --home build/node0 --keyring-backend test \
  --chain-id chain-local \
  --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
  --testnet \
  --yes

Contract execution example

Transfer

user1 can now transfer shares of example-co stock to user2 using the smart contract.

NOTE: you can get the address for user2 with the following command:

provenanced keys show -a user2 --home build/node0 -t

Transfer 5 shares of example-co.stock from user1 to user2:

# first grant authz permission so the contract can escrow the coin for the transfer
  provenanced tx marker grant-authz \
    tp15fnweczx7273jc6tmuuacmkl6zk6mq8ffh8r0artxp9srdpctcesek7uac \
    "transfer" \
    --transfer-limit 5example-co.stock \
    --from user1 \
    --home build/node0 --keyring-backend test \
    --chain-id chain-local \
    --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
    --testnet \
    --yes -o json | jq

# initiate transfer using a unique uuid as the id attribute
provenanced tx wasm execute tp15fnweczx7273jc6tmuuacmkl6zk6mq8ffh8r0artxp9srdpctcesek7uac \
    '{"transfer":{"id":"54c4f5d9-5253-43ac-9011-bbc52465581e", "denom":"example-co.stock",  "amount":"5", "recipient": "tp1m4arun5y9jcwkatq2ey9wuftanm5ptzsg4ppfs"}}' \
    --from user1 \
    --home build/node0 --keyring-backend test \
    --chain-id chain-local \
    --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
    --testnet \
    --yes -o json | jq

Query transfers

query all pending transfers

provenanced q wasm contract-state smart tp15fnweczx7273jc6tmuuacmkl6zk6mq8ffh8r0artxp9srdpctcesek7uac \
    '{"get_all_transfers":{}}' \
    --ascii -o json \
    --chain-id chain-local \
    --testnet | jq

Approve

Now the account with transfer permission can approve the transfer

provenanced tx wasm execute tp15fnweczx7273jc6tmuuacmkl6zk6mq8ffh8r0artxp9srdpctcesek7uac \
    '{"approve_transfer":{"id":"54c4f5d9-5253-43ac-9011-bbc52465581e"}}' \
    --from admin1 \
    --home build/node0 --keyring-backend test \
    --chain-id chain-local \
    --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
    --testnet \
    --yes -o json | jq

You can check the balance of user2 to see that the transfer was successful

provenanced q bank balances $(provenanced keys show -a user2 --home build/node0 --keyring-backend test --testnet) -t

Other contract actions

Cancel

A user can cancel a transfer before it is approved or rejected:

provenanced tx wasm execute tp15fnweczx7273jc6tmuuacmkl6zk6mq8ffh8r0artxp9srdpctcesek7uac \
    '{"cancel_transfer":{"id":"54c4f5d9-5253-43ac-9011-bbc52465581e"}}' \
    --from user1 \
    --home build/node0 --keyring-backend test \
    --chain-id chain-local \
    --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
    --testnet \
    --yes -o json | jq

Reject

The account with transfer permission can reject a transfer:

provenanced tx wasm execute tp15fnweczx7273jc6tmuuacmkl6zk6mq8ffh8r0artxp9srdpctcesek7uac \
    '{"reject_transfer":{"id":"54c4f5d9-5253-43ac-9011-bbc52465581e"}}' \
    --from admin1 \
    --home build/node0 --keyring-backend test \
    --chain-id chain-local \
    --gas auto --gas-prices 1905nhash --gas-adjustment 1.3 \
    --testnet \
    --yes -o json | jq