Skip to content

Commit

Permalink
chore: debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizAsFight committed Dec 17, 2024
1 parent 022d731 commit 61b6bf2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
40 changes: 39 additions & 1 deletion packages/common/src/PredicateConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,39 @@ export abstract class PredicateConnector extends FuelConnector {
]);
predicate.connect(fuelProvider);

console.log(
'asd --- PREPARE predicateSignatureIndex',
predicateSignatureIndex,
);
console.log(
'asd --- PREPARE transactionRequest.toJSON',
transactionRequest.toJSON(),
);

// remove witness so the SDK doesnt remove it (why you remove??)
const prevWitnesses = transactionRequest.witnesses;
transactionRequest.witnesses = [];
// To each input of the request, attach the predicate and its data
const requestWithPredicateAttached =
predicate.populateTransactionPredicateData(transactionRequest);

// needs to set witness again
// sdk also mess up witnessIndex of first input, so lets fix it
requestWithPredicateAttached.witnesses = prevWitnesses;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
(requestWithPredicateAttached.inputs[2] as any).witnessIndex = 1;

console.log(
'asd --- after AFTER PREPARE requestWithPredicateAttached.toJson()',
requestWithPredicateAttached.toJSON(),
);
const maxGasUsed =
await this.predicateAccount.getMaxPredicateGasUsed(fuelProvider);

let predictedGasUsedPredicate = bn(0);
requestWithPredicateAttached.inputs.forEach((input) => {
if ('predicate' in input && input.predicate) {
input.witnessIndex = 0;
input.witnessIndex = predicateSignatureIndex;
predictedGasUsedPredicate = predictedGasUsedPredicate.add(maxGasUsed);
}
});
Expand All @@ -212,6 +234,12 @@ export abstract class PredicateConnector extends FuelConnector {
ZeroBytes32,
]);

console.log('asd --- predicateSignatureIndex', predicateSignatureIndex);
console.log(
'asd --- requestWithPredicateAttached',
requestWithPredicateAttached.toJSON(),
);

const { gasPriceFactor } = predicate.provider.getGasConfig();
const { maxFee, gasPrice } = await predicate.provider.estimateTxGasAndFee({
transactionRequest: requestWithPredicateAttached,
Expand All @@ -231,11 +259,21 @@ export abstract class PredicateConnector extends FuelConnector {
requestWithPredicateAttached.maxFee = feeWithFat.add(10);
}

console.log(
'asd --- BEFORE estimateTxDependencies requestWithPredicateAttached',
requestWithPredicateAttached.toJSON(),
);

// Attach missing inputs (including estimated predicate gas usage) / outputs to the request
await predicate.provider.estimateTxDependencies(
requestWithPredicateAttached,
);

console.log(
'asd --- AFTER estimateTxDependencies requestWithPredicateAttached',
requestWithPredicateAttached.toJSON(),
);

return {
predicate,
request: requestWithPredicateAttached,
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/PredicateFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ export class PredicateFactory {
* predicate with the correct witness index argument.
*/
export const getMockedSignatureIndex = (witnesses: BytesLike[]) => {
const hasPlaceholderWitness = witnesses.some(
const placeholderWitnessIndex = witnesses.findIndex(
(item) =>
item instanceof Uint8Array &&
item.length === 64 &&
item.every((value) => value === 0),
);

const hasPlaceholderWitness = placeholderWitnessIndex !== -1;
// if it is a placeholder witness, we can safely replace it, otherwise we will consider a new element.
return hasPlaceholderWitness ? witnesses.length - 1 : witnesses.length;
return hasPlaceholderWitness ? placeholderWitnessIndex : witnesses.length;
};
2 changes: 1 addition & 1 deletion packages/connectors/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fuels/connectors",
"version": "0.36.1",
"version": "0.38.1",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down
20 changes: 20 additions & 0 deletions packages/walletconnect-connector/src/WalletConnectConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,21 @@ export class WalletConnectConnector extends PredicateConnector {
params?: SendTransactionParams,
): Promise<string> {
const { ethProvider, fuelProvider } = await this.getProviders();
console.log('asd inputs BEFORE prepare', transaction.inputs?.toString());
console.log(
'asd witnesses BEFORE prepare',
transaction.witnesses?.toString(),
);
const { request, transactionId, account, transactionRequest } =
await this.prepareTransaction(address, transaction);
console.log(
'asd inputs AFTER prepare',
transactionRequest.inputs?.toString(),
);
console.log(
'asd witnesses AFTER prepare',
transactionRequest.witnesses?.toString(),
);

const signature = (await ethProvider?.request({
method: 'personal_sign',
Expand All @@ -391,6 +404,13 @@ export class WalletConnectConnector extends PredicateConnector {

// Transform the signature into compact form for Sway to understand
const compactSignature = splitSignature(hexToBytes(signature)).compact;

console.log('asd predicateSignatureIndex', predicateSignatureIndex);
console.log(
'asd transactionRequest.witnesses',
transactionRequest.witnesses?.toString(),
);
console.log('asd compactSignature', compactSignature);
transactionRequest.witnesses[predicateSignatureIndex] = compactSignature;

const transactionWithPredicateEstimated =
Expand Down

0 comments on commit 61b6bf2

Please sign in to comment.