-
Notifications
You must be signed in to change notification settings - Fork 5
/
sendTokens.py
120 lines (109 loc) · 3.72 KB
/
sendTokens.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import cardano_cli_helper as cli
import argparse
from os.path import exists
def main(paymentAddrFile, paymentSkeyFile, recipientAddr, lovelace_amount,
policyIDList, tokenAmountList, network, era):
if exists(paymentAddrFile):
with open(paymentAddrFile, 'r') as file:
paymentAddr = file.read().strip()
else:
paymentAddr = paymentAddrFile.strip()
assert exists(paymentSkeyFile), "ERROR: Payment skey file does not exist."
assert len(policyIDList) == len(tokenAmountList), \
"ERROR: Policy ID list does not match with Token amount List."
if exists(recipientAddr):
# If it doesn't exist assume it's a valid address
with open(recipientAddr, 'r') as file:
recipientAddr = file.read().strip()
# Create dictionary with tokens to send
sendTokensDict = {}
for tokenID, tokenAmount in zip(policyIDList, tokenAmountList):
sendTokensDict[tokenID] = tokenAmount
utxos_limit = 200 # Ensures that the tx can fit in a block
utxos = cli.getAddrUTxOs(paymentAddr, network, utxos_limit)
dictWallet = cli.getTokenListFromTxHash(utxos)
try:
for item in sendTokensDict:
dictWallet[item] = dictWallet[item] - sendTokensDict[item]
except Exception as e:
assert False, f"ERROR: Token amounts not found in wallet: {e}"
ttlSlot = cli.queryTip('slot', network) + 1000
txId = cli.buildSendTokensToOneDestinationTx(
utxos, paymentAddr, ttlSlot,
recipientAddr, lovelace_amount,
sendTokensDict, dictWallet,
network, era=era
)
txId = txId.strip()
cli.signTx([paymentSkeyFile], network=network)
submitted = cli.submitSignedTx(network=network)
assert (
submitted.strip() == 'Transaction successfully submitted.'
), f"ERROR: Transaction {txId} not submitted successfully"
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-A', '--payment-addr',
default='/home/christos/skepsis_withdraw/payment.addr',
dest='payment_addr',
help='Provide payment address or location of payment address file.',
type=str
)
parser.add_argument(
'-K', '--payment-skey-file',
default='/home/christos/skepsis_withdraw/payment.skey',
dest='payment_skey_file',
help='Provide location of payment skey file.',
type=str
)
parser.add_argument(
'-D', '--destination',
default='/home/christos/skepsis_withdraw/myYoroi.addr',
dest='destination',
help='Provide location destination address file or string.',
type=str
)
parser.add_argument(
'-L', '--amount-lovelace',
default=1604*10**6,
dest='amount',
help='Provide amount to send in lovelace.',
type=int
)
parser.add_argument(
'-T','--token-policy-id',
default=[],
dest='policyIDList',
nargs='+',
help='List of tokens to send',
type=str)
parser.add_argument(
'-M','--token-amount',
default=[],
dest='tokenAmountList',
nargs='+',
help='List of tokens to send',
type=int)
parser.add_argument(
'-N', '--network',
default='mainnet',
dest='network',
help='Provide cardano network.',
type=str
)
parser.add_argument(
'-E', '--era',
default='conway',
dest='era',
help='Provide cardano era.',
type=str
)
args = parser.parse_args()
main(args.payment_addr,
args.payment_skey_file,
args.destination,
args.amount,
args.policyIDList,
args.tokenAmountList,
args.network,
args.era)