Skip to content

Commit

Permalink
Merge pull request #2232 from Emurgo/ergo-connector-large-token-value…
Browse files Browse the repository at this point in the history
…s-fix

Ergo Connector: Fix for large token amounts
  • Loading branch information
vsubhuman authored Jul 14, 2021
2 parents 050c0f2 + 63116bd commit 7bbf299
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 30 deletions.
22 changes: 10 additions & 12 deletions packages/yoroi-ergo-connector/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,23 @@ function initDapp() {
status.innerText = "Creating transaction";
const donationAddr = "9hD2Cw6yQL6zzrw3TFgKdwFkBdDdU3ro1xRFmjouDw4NYS2S5RD";
const creationHeight = 398959;
const amountToSend = parseInt(valueEntry.value, 10);
const amountToSend = BigInt(valueEntry.value);
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 rawUtxos = await ergo.get_utxos((amountToSend + BigInt(wasm.TxBuilder.SUGGESTED_TX_FEE().as_i64().to_str())).toString());
let utxosValue = BigInt(0);
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;
//utxo.value = parseInt(utxo.value, 10);
utxosValue += BigInt(utxo.value);
for (let asset of utxo.assets) {
asset.amount = parseInt(asset.amount);
//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();
const changeValue = utxosValue - amountToSend - BigInt(wasm.TxBuilder.SUGGESTED_TX_FEE().as_i64().to_str());
console.log(`${changeValue} | cv.ts() = ${changeValue.toString()}`);
const changeValueBoxValue = wasm.BoxValue.from_i64(wasm.I64.from_str(changeValue.toString()));
const changeAddr = await ergo.get_change_address();
Expand All @@ -88,12 +86,12 @@ function initDapp() {
new wasm.Tokens());
console.log(`boxes selected: ${boxSelection.boxes().len()}`);
const outputCandidates = wasm.ErgoBoxCandidates.empty();
const token = new wasm.Token(wasm.TokenId.from_box_id(wasm.BoxId.from_str(utxos[1].boxId)), wasm.TokenAmount.from_i64(wasm.I64.from_str("12345678")));
const token = new wasm.Token(wasm.TokenId.from_box_id(wasm.BoxId.from_str(utxos[2].boxId)), wasm.TokenAmount.from_i64(wasm.I64.from_str("1234567890123456789")));
const donationBoxBuilder = new wasm.ErgoBoxCandidateBuilder(
amountToSendBoxValue,
wasm.Contract.pay_to_address(wasm.Address.from_base58(donationAddr)),
creationHeight);
donationBoxBuilder.mint_token(token, "ECEDT", "Ergo Connector Example Dapp Token (for testing)", 5);
donationBoxBuilder.mint_token(token, "VLT", "Very Large Token", 2);
//donationBoxBuilder.add_token(token.id(), token.amount());
try {
outputCandidates.add(donationBoxBuilder.build());
Expand All @@ -102,7 +100,7 @@ function initDapp() {
throw e;
}
//outputCandidates.add(changeBoxBuilder.build());
console.log(`utxosval: ${utxosValue}`);
console.log(`utxosval: ${utxosValue.toString()}`);
const txBuilder = wasm.TxBuilder.new(
boxSelection,
outputCandidates,
Expand Down
18 changes: 0 additions & 18 deletions packages/yoroi-extension/chrome/extension/ergo-connector/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,6 @@ export type BoxLike = {
...
}

// TODO: look into sigma rust string value support
function processBoxesForSigmaRust<T: BoxLike>(boxes: T[]) {
for (const output of boxes) {
output.value = parseInt(output.value, 10);
if (output.value > Number.MAX_SAFE_INTEGER) {
throw new Error('large values not supported by sigma-rust\'s json parsing code');
}
for (const asset of output.assets) {
asset.amount = parseInt(asset.amount, 10);
if (asset.amount > Number.MAX_SAFE_INTEGER) {
throw new Error('large values not supported by sigma-rust\'s json parsing code');
}
}
}
}

export async function connectorSignTx(
publicDeriver: IPublicDeriver<ConceptualWallet>,
password: string,
Expand All @@ -243,7 +227,6 @@ export async function connectorSignTx(
await RustModule.load();
let wasmTx;
try {
processBoxesForSigmaRust(tx.outputs);
wasmTx = RustModule.SigmaRust.UnsignedTransaction.from_json(JSON.stringify(tx));
} catch (e) {
throw ConnectorError.invalidRequest(`Invalid tx - could not parse JSON: ${e}`);
Expand Down Expand Up @@ -274,7 +257,6 @@ export async function connectorSignTx(
const jsonBoxesToSign: Array<ErgoBoxJson> =
// $FlowFixMe[prop-missing]: our inputs are nearly like `ErgoBoxJson` just with one extra field
tx.inputs.filter((box, index) => indices.includes(index));
processBoxesForSigmaRust(jsonBoxesToSign);
const txBoxesToSign = RustModule.SigmaRust.ErgoBoxes.from_boxes_json(jsonBoxesToSign);
const dataBoxIds = tx.dataInputs.map(box => box.boxId);
const dataInputs = utxos.filter(
Expand Down

0 comments on commit 7bbf299

Please sign in to comment.