-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (159 loc) · 7.29 KB
/
index.js
File metadata and controls
182 lines (159 loc) · 7.29 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import * as wasm from "ergo-lib-wasm-browser";
import JSONBigInt from "json-bigint";
const DEFAULT_EXPLORER_URL = "https://api-testnet.ergoplatform.com";
const ERGONAMES_CONTRACT_ADDRESS = "3WycHxEz8ExeEWpUBwvu1FKrpY8YQCiH1S9PfnAvBX1K73BXBXZa";
const ROYALTY_PERCENTAGE = 20;
async function get_current_height(explorer_url = DEFAULT_EXPLORER_URL) {
let url = explorer_url + "/api/v1/blocks?limit=1";
return await fetch(url)
.then(res => res.json())
.then(data => { return data["total"]; })
}
export async function send_transaction(ergoname_price, ergoname_name, reciever_address, explorer_url = DEFAULT_EXPLORER_URL) {
ergoConnector.nautilus.connect().then(() => {
ergo.get_balance().then(async function() {
async function getUtxos(amountToSend) {
const fee = BigInt(wasm.TxBuilder.SUGGESTED_TX_FEE().as_i64().to_str());
const fullAmount = BigInt(1000) * amountToSend + fee;
console.log(fullAmount);
const utxos = await ergo.get_utxos(fullAmount.toString());
const filteredUtxos = [];
for (const utxo of utxos) {
try {
await wasm.ErgoBox.from_json(JSONBigInt.stringify(utxo));
filteredUtxos.push(utxo);
} catch (e) {
console.log('[getUtxos] UTXO failed parsing: ', utxo, e);
}
}
return filteredUtxos;
}
const creationHeight = await get_current_height(explorer_url);
console.log(creationHeight);
const amountToSend = BigInt(ergoname_price);
const amountToSendBoxValue = wasm.BoxValue.from_i64(wasm.I64.from_str(amountToSend.toString()));
const utxos = await getUtxos(amountToSend);
let utxosValue = utxos.reduce((acc, utxo) => acc += BigInt(utxo.value), BigInt(0));
console.log('utxos', utxosValue, utxos);
const changeValue = utxosValue - amountToSend - BigInt(wasm.TxBuilder.SUGGESTED_TX_FEE().as_i64().to_str());
console.log(`${changeValue} | cv.ts() = ${changeValue.toString()}`);
const changeAddr = await ergo.get_change_address();
console.log(`changeAddr = ${JSON.stringify(changeAddr)}`);
const selector = new wasm.SimpleBoxSelector();
const boxSelection = selector.select(
wasm.ErgoBoxes.from_boxes_json(utxos),
wasm.BoxValue.from_i64(amountToSendBoxValue.as_i64().checked_add(wasm.TxBuilder.SUGGESTED_TX_FEE().as_i64())),
new wasm.Tokens());
console.log(`boxes selected: ${boxSelection.boxes().len()}`);
const outputCandidates = wasm.ErgoBoxCandidates.empty();
let outBoxBuilder = new wasm.ErgoBoxCandidateBuilder(
amountToSendBoxValue,
wasm.Contract.pay_to_address(wasm.Address.from_base58(ERGONAMES_CONTRACT_ADDRESS)),
creationHeight);
outBoxBuilder.set_register_value(4, wasm.Constant.from_i32(ROYALTY_PERCENTAGE));
outBoxBuilder.set_register_value(5, wasm.Constant.from_byte_array(ergoname_name));
outBoxBuilder.set_register_value(6, wasm.Constant.from_i32(ergoname_price));
outBoxBuilder.set_register_value(7, wasm.Constant.from_byte_array(reciever_address));
try {
outputCandidates.add(outBoxBuilder.build());
} catch (e) {
console.log(`building error: ${e}`);
throw e;
}
console.log(`utxosvalue: ${utxosValue.toString()}`);
const txBuilder = wasm.TxBuilder.new(
boxSelection,
outputCandidates,
creationHeight,
wasm.TxBuilder.SUGGESTED_TX_FEE(),
wasm.Address.from_base58(changeAddr),
wasm.BoxValue.SAFE_USER_MIN());
const dataInputs = new wasm.DataInputs();
txBuilder.set_data_inputs(dataInputs);
console.log(txBuilder.build().to_json());
const tx = parseTransactionData(txBuilder.build().to_json());
console.log(`tx: ${JSONBigInt.stringify(tx)}`);
console.log(`original id: ${tx.id}`);
const correctTx = parseTransactionData(wasm.UnsignedTransaction.from_json(JSONBigInt.stringify(tx)).to_json());
console.log(`correct tx: ${JSONBigInt.stringify(correctTx)}`);
console.log(`new id: ${correctTx.id}`);
correctTx.inputs = correctTx.inputs.map(box => {
console.log(`box: ${JSONBigInt.stringify(box)}`);
const fullBoxInfo = utxos.find(utxo => utxo.boxId === box.boxId);
return {
...fullBoxInfo,
extension: {}
};
});
console.log(`${JSONBigInt.stringify(correctTx)}`);
async function signTx(txToBeSigned) {
try {
return await ergo.sign_tx(txToBeSigned);
} catch (err) {
const msg = `[signTx] Error: ${JSON.stringify(err)}`;
console.error(msg, err);
return null;
}
}
async function submitTx(txToBeSubmitted) {
try {
return await ergo.submit_tx(txToBeSubmitted);
} catch (err) {
const msg = `[submitTx] Error: ${JSON.stringify(err)}`;
console.error(msg, err);
return null;
}
}
async function processTx(txToBeProcessed) {
const msg = s => {
console.log('[processTx]', s);
};
const signedTx = await signTx(txToBeProcessed);
if (!signedTx) {
console.log(`No signed tx`);
return null;
}
msg("Transaction signed - awaiting submission");
const txId = await submitTx(signedTx);
if (!txId) {
console.log(`No submotted tx ID`);
return null;
}
msg("Transaction submitted - thank you for your donation!");
return txId;
}
let txId = processTx(correctTx).then(txId => {
return txId;
});
return txId;
});
});
}
function parseTransactionData(str) {
let json = JSONBigInt.parse(str);
return {
id: json.id,
inputs: json.inputs,
dataInputs: json.dataInputs,
outputs: json.outputs.map(output => parseUTXO(output)),
}
}
function parseUTXO(json) {
var newJson = { ...json };
if (newJson.assets === null) {
newJson.assets = [];
}
return {
boxId: newJson.boxId,
value: newJson.value.toString(),
ergoTree: newJson.ergoTree,
assets: newJson.assets.map(asset => ({
tokenId: asset.tokenId,
amount: asset.amount.toString(),
})),
additionalRegisters: newJson.additionalRegisters,
creationHeight: newJson.creationHeight,
transactionId: newJson.transactionId,
index: newJson.index
};
}