The transaction example demonstrates how you can create a transaction. This example consists of one python script /exampleCreateTx.py and manifest file /manifest.json.
from DAP import configGetItem
from DAP.Core import logIt
from CellFrame.Chain import Mempool, ChainAddr, Wallet
from CellFrame.Network import Net
def init():
wallet_path = configGetItem("resources", "wallets_path")
logIt.notice("wallet path: "+wallet_path)
wallet = Wallet.open("mywallet1", wallet_path)
chain_net = Net.byName("subzero")
chain = chain_net.getChainByName("support")
key = wallet.getKey(0)
addr_from = wallet.getAddr(chain_net.id)
addr_to = ChainAddr.fromStr("mJUUJk6Yk2gBSTjcCmbwJ2ozjDTLPKZTmTVM9AUysMfWt4oQcNwjtCNEbkGo1dWVmkNXaQYbPtMqdHD4ftF5Sfx4mo9ss9r4jauSXDhV")
addr_fee = None
value = 50
value_fee = 0
# tx_hash = Mempool.txCreate(chain, key, addr_from, addr_to, addr_fee, "tCELL", value, value_fee)
# tx_hash_str = str(tx_hash)
# logIt.notice("Created transaction with hash: " + tx_hash_str)
return 0
{
"name": "exampleCreateTx",
"version": "1.0",
"author": "DEMLABS (C) 2022",
"dependencies": [],
"description": "This plugin is an example of creating transaction."
}
This /exampleCreateTx.py script contains only one function: init
.
The execution of the plugin starts with the init
.
1. The plugin is initialized by the init
function:
def init():
2. Getting a path to the wallet directory from the configuration file using a configGetItem
function:
wallet_path = configGetItem("resources", "wallets_path")
The "resources"
and the "wallets_path"
strings are a section and a value specified in the configuration file.
3. Logging a wallet directory:
logIt.notice("wallet path: "+wallet_path)
The "wallet path: " + wallet_path
is a message that will be logged.
4. Openning an existing wallet using the open
static method of the Wallet
class:
wallet = Wallet.open("mywallet1", wallet_path)
The "mywallet1"
string is a name of a wallet and the wallet_path
argument is the path to the directory where this file is stored. To run the example, a wallet with the specified name must exist. The wallet
is an instance of a Wallet
object.
5. Getting a net by a name using the byName
method of the Net
class:
chain_net = Net.byName("subzero")
The "subzero"
is a net name. The chain_net
is an instance of a Net
object.
6. Getting a chain by a chain name using the getChainByName
method of the Net
class:
chain = chain_net.getChainByName("support")
The "support"
is a chain name. The chain
is an instance of a Chain
object.
7. Getting a wallet key by an index using the getkey
method of the Wallet
class:
key = wallet.getKey(0)
The 0
is an index of a key in the wallet list. The key
is an instance of a CryptoKey
object
8. Getting an address of the wallet from which the transfer will be made using the getAddr
method of the Wallet
class:
addr_from = wallet.getAddr(chain_net.id)
The getAddr
function takes as an argument an NetID
object that we get using id
attribute of the Net
object. The addr_from
is a ChainAddr
object.
9. Getting an address of the wallet to which the transfer will be made using fromStr
method:
addr_to = addr_to = ChainAddr.fromStr("mJUUJk6Yk2gBSTjcCmbwJ2ozjDTLPKZTmTVM9AUysMfWt4oQcNwjtCNEbkGo1dWVmkNXaQYbPtMqdHD4ftF5Sfx4mo9ss9r4jauSXDhV")
The "mJUUJk6Yk2gBSTjcDdb..."
is the wallet address of one of our programmers, who is always happy to receive new transactions.
10. In this example, we will not charge a fee for the transfer:
addr_fee = None
value_fee = 0
11. Setting a value of the transaction.
value = 500
12. And finally creating the transaction using txCreate
static method of the Mempool
class:
tx_hash = Mempool.txCreate(chain, key, addr_from, addr_to, addr_fee, "tCELL", value, value_fee)
The tCELL
is a token ticket. Other arguments of the method are described above. The tx_hash
is an instance of a HashFast
object.
13. At the end of the script, converting a tx_hash
value to a string, which we print and log:
tx_hash_str = str(tx_hash)
logIt.notice("Created transaction with hash: " + tx_hash_str)
14. Returning 0. This means that the plugin has been successfully initialized:
return 0