Skip to content

Commit

Permalink
Merge branch 'develop' into yushi/catalyst-ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
yushih committed May 28, 2021
2 parents 334cc11 + 583ec3d commit b2d933d
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Publish to Nightly
on:
push:
branches:
- develop
- nightly

jobs:
build:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
npm-debug.log
.DS_Store
.vscode/
.idea/
59 changes: 42 additions & 17 deletions packages/yoroi-ergo-connector/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function initDapp() {
// // console.log('}');
// console.log(`get_unused_addresses() = ${JSON.stringify(addresses)}`);
// });
function pagedUsedAddresses(page, limit) {
/*function pagedUsedAddresses(page, limit) {
ergo.get_used_addresses({ page, limit }).then(usedAddresses => {
console.log(`usedAddresses[${page * limit} - ${page * limit + usedAddresses.length - 1}] = ${JSON.stringify(usedAddresses)}`);
pagedUsedAddresses(page + 1, limit);
Expand All @@ -44,7 +44,7 @@ function initDapp() {
});
ergo.get_utxos(5000, assetId).then(result => {
console.log(`get_utxos(5000, custom asset) = ${JSON.stringify(result)}`);
});
});*/
ergo.get_balance().then(async function(result) {
let tx = {};
const div = document.getElementById("balance");
Expand All @@ -56,21 +56,24 @@ function initDapp() {
button.textContent = "Send";
button.onclick = async function() {
status.innerText = "Creating transaction";
const donationAddr = "9fp6ERwLEF8u3Jvbii2msogFDUa9edxmvQKbwbwogXjLg7oXZSo";
const donationAddr = "9hD2Cw6yQL6zzrw3TFgKdwFkBdDdU3ro1xRFmjouDw4NYS2S5RD";
const creationHeight = 398959;
const amountToSend = parseInt(valueEntry.value, 10);
const amountToSendBoxValue = wasm.BoxValue.from_i64(wasm.I64.from_str(amountToSend.toString()));
const rawUtxos = await ergo.get_utxos(amountToSend + wasm.TxBuilder.SUGGESTED_TX_FEE().as_i64().as_num());
let utxosValue = 0;
const utxos = rawUtxos.map(utxo => {
let utxos = rawUtxos.map(utxo => {
// need to convert strings to numbers for sigma-rust for now
utxo.value = parseInt(utxo.value, 10);
utxosValue += utxo.value;
for (let asset of utxo.assets) {
asset.amount = parseInt(asset.amount);
}
return utxo;
})
});
// Testing with p2S inputs since Yoroi won't return those as they don't belong to anyone's wallet
//while (utxos.length > 1) { utxos.pop(); }
//utxos.unshift({"boxId":"6dd679cc32afd1f56ad74696c7af53c45330148a703da29b3f6b3ca3b09851c3","value":1331719,"ergoTree":"1002040004f2c001d193e4c6b2a573000004047301","assets":[],"additionalRegisters":{},"creationHeight":398959,"transactionId":"d2fbf4b62f262f4bce7973924ae06685aa5ec2313e24716e8b1d86d62789c89b","index":0});
console.log(`utxosValue: ${utxosValue}`);
console.log(`${utxosValue} - ${amountToSend} - ${wasm.TxBuilder.SUGGESTED_TX_FEE().as_i64().as_num()}`);
const changeValue = utxosValue - amountToSend - wasm.TxBuilder.SUGGESTED_TX_FEE().as_i64().as_num();
Expand All @@ -89,11 +92,12 @@ function initDapp() {
amountToSendBoxValue,
wasm.Contract.pay_to_address(wasm.Address.from_base58(donationAddr)),
creationHeight);
// const changeBoxBuilder = new wasm.ErgoBoxCandidateBuilder(
// changeValueBoxValue,
// wasm.Contract.pay_to_address(wasm.Address.from_base58(changeAddr)),
// creationHeight);
outputCandidates.add(donationBoxBuilder.build());
try {
outputCandidates.add(donationBoxBuilder.build());
} catch (e) {
console.log(`building error: ${e}`);
throw e;
}
//outputCandidates.add(changeBoxBuilder.build());
console.log(`utxosval: ${utxosValue}`);
const txBuilder = wasm.TxBuilder.new(
Expand All @@ -104,15 +108,36 @@ function initDapp() {
wasm.Address.from_base58(changeAddr),
wasm.BoxValue.SAFE_USER_MIN());
//changeValueBoxValue);

const dataInputs = new wasm.DataInputs();
// random tx we sent via the connector before - not referenced in any smart contract right now
//dataInputs.add(new wasm.DataInput(wasm.BoxId.from_str("0f0e4c71ccfbe7e749591ef2a906607b415deadee8c23a8d822517c4cd55374e")));
txBuilder.set_data_inputs(dataInputs);
const tx = txBuilder.build().to_json();
console.log(`tx: ${tx}`);
console.log(`tx: ${JSON.stringify(tx)}`);
console.log(`original id: ${tx.id}`);
// sigma-rust doesn't support most compilation so manually insert it here
// this is HEIGHT > 1337 but in hex and without the checksum/etc for the address of the contract
//tx.outputs[0].ergoTree = "100104f214d191a37300";
// and this is a register-using one
//tx.outputs[0].ergoTree = "1002040004f2c001d193e4c6b2a573000004047301";
// and we rebuild it using
const correctTx = wasm.UnsignedTransaction.from_json(JSON.stringify(tx)).to_json();
console.log(`new id: ${correctTx.id}`);
// we must use the exact order chosen as after 0.4.3 in sigma-rust
// this can change and might not use all the utxos as the coin selection
// might choose a more optimal amount
correctTx.inputs = correctTx.inputs.map(box => {
console.log(`box: ${JSON.stringify(box)}`);
const fullBoxInfo = utxos.find(utxo => utxo.boxId === box.boxId);
return {
...fullBoxInfo,
extension: {}
};
});
status.innerText = "Awaiting transaction signing";
console.log(`${JSON.stringify(correctTx)}`);
ergo
.sign_tx({
...tx,
inputs: utxos.map(utxo => ({ ...utxo, extension: {} })),
})
.sign_tx(correctTx)
.then(async signedTx => {
status.innerText = "Transaction signed - awaiting submission"
try {
Expand Down Expand Up @@ -153,4 +178,4 @@ if (typeof ergo_request_read_access === "undefined") {
div.appendChild(button);
});
initDapp();
}
}
121 changes: 55 additions & 66 deletions packages/yoroi-ergo-connector/example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/yoroi-ergo-connector/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"start": "webpack-dev-server"
},
"devDependencies": {
"ergo-lib-wasm-browser": "0.4.3",
"ergo-lib-wasm-browser": "0.10.0",
"webpack": "^4.29.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/yoroi-ergo-connector/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b2d933d

Please sign in to comment.