Skip to content

Commit

Permalink
feat: show simulation result
Browse files Browse the repository at this point in the history
  • Loading branch information
juliopavila committed Nov 27, 2023
1 parent 017e42f commit 196f91f
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 106 deletions.
16 changes: 14 additions & 2 deletions src/helpers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BigNumber } from '@ethersproject/bignumber';
import { Fragment, JsonFragment } from '@ethersproject/abi';
import { Network } from '@/plugins/safeSnap/types';


export interface Strategy {
id: string;
spacesCount: number;
Expand Down Expand Up @@ -272,14 +273,26 @@ export interface PendingTransaction {
hash: string | null;
}

export type SimulationLog = {
type: string;
message: string;
url?: string;
};

export type SimulationState = {
status: 'success' | 'error' | 'idle';
logs: SimulationLog[];
};

export interface SafeTransaction {
to: string;
value: string;
data: string;
operation: string;
nonce: string;
type?: string;
transactionBatchType?: "connext" | "standard"
transactionBatchType?: 'connext' | 'standard';
simulation?: SimulationState;
}

export interface SafeTransactionConfig {
Expand Down Expand Up @@ -382,7 +395,6 @@ export interface SafeExecutionData {
txs: SafeModuleTransactionBatch[];
network: string;
realityModule: string;

}

export interface Plugin {
Expand Down
30 changes: 9 additions & 21 deletions src/plugins/safeSnap/components/Form/ConnextTransaction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ type ConnextTransactionProps = {
modelValue: SafeTransaction[];
config: SafeTransactionConfig;
isDetails: boolean;
simulationState?: SimulationState;
nonce: number;
};
Expand All @@ -27,12 +26,12 @@ import { FunctionFragment } from '@ethersproject/abi';
import {
SafeTransaction,
SafeTransactionConfig,
SimulationState,
TokenAsset
} from '@/helpers/interfaces';
import { ETH_CONTRACT } from '@/helpers/constants';
import { shorten } from '@/helpers/utils';
import { formatUnits } from '@ethersproject/units';
import { SimulationState } from '../Simulation/Tenderly.vue';
import SafeSnapSimulationTenderly from '../Simulation/Tenderly.vue';
const props = defineProps<ConnextTransactionProps>();
Expand Down Expand Up @@ -60,9 +59,9 @@ const getNetworkKeyByDomainId = (domainId: number): string | undefined => {
};
const setTokens = () => {
if (!props.config.preview && props.config.tokens) {
if (props.config.tokens) {
tokens.value = [
getNativeAsset(props.config.network),
// getNativeAsset(props.config.network),
...props.config.tokens
];
}
Expand All @@ -74,6 +73,7 @@ const setInitialModelValue = async (modelValue: SafeTransaction[]) => {
transactions.forEach(async tx => {
if (tx.type === 'raw') {
simulation.value = tx.simulation;
const transaction = await plugin.decodeConnextXcallData(tx.data);
selectedNetwork.value = transaction[0];
destinationAddress.value = transaction[1];
Expand Down Expand Up @@ -155,7 +155,6 @@ const generateApproveTransaction = async (connext: string, amount: string) => {
);
if (plugin.validateTransaction(transaction)) {
transaction.transactionBatchType = 'connext';
// emit('update:modelValue', transaction);
return transaction;
}
}
Expand Down Expand Up @@ -230,6 +229,7 @@ const format = (amount: string, decimals: number) => {
}
};
const simulation = ref<SimulationState | undefined>(undefined);
const isEditing = ref<boolean>(false);
const tokens = ref<TokenAsset[]>();
const modalTokensOpen = ref<boolean>(false);
Expand All @@ -252,25 +252,14 @@ watch(
() => props.modelValue,
async newVal => {
if (newVal && newVal.length) {
// Estableciendo modo de edición
isEditing.value = true;
await setInitialModelValue(newVal);
} else {
// No es modo de edición
isEditing.value = false;
}
},
{ immediate: true }
);
// watch(
// () => props.modelValue,
// async newVal => {
// if (newVal && newVal.length) {
// await setInitialModelValue(newVal);
// }
// },
// { immediate: true }
// );
watchEffect(async () => {
if (
Expand All @@ -282,8 +271,6 @@ watchEffect(async () => {
await updateTransaction();
}
});
console.log('props.nonce', props.nonce);
</script>

<template>
Expand Down Expand Up @@ -329,13 +316,13 @@ console.log('props.nonce', props.nonce);
{{ shorten(destinationAddress) }}
</p>
</div>
{{ console.log('simulationState', simulationState) }}
<SafeSnapSimulationTenderly
v-if="simulationState"
v-if="simulation"
:is-details="props.isDetails"
:model-value-to-simulate="modelValue"
:config="props.config"
:run-simulation="false"
:default-simulation-result="simulationState"
:default-simulation-result="simulation"
/>
</div>
<div v-if="!isDetails">
Expand Down Expand Up @@ -423,6 +410,7 @@ console.log('props.nonce', props.nonce);
@validAddress="handleInput($event, 'destinationAddress')"
/>


<teleport to="#modal">
<SafeSnapTokensModal
v-if="tokens?.length"
Expand Down
26 changes: 13 additions & 13 deletions src/plugins/safeSnap/components/Form/Transaction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export default {
'nonce',
'config',
'isDetails',
'transactionType',
'simulationState'
'transactionType'
],
emits: ['update:modelValue', 'remove', 'edit'],
data() {
Expand All @@ -57,23 +56,26 @@ export default {
modelValue: {
immediate: true,
async handler(newVal, oldVal) {
if (!newVal) return;
if (newVal !== oldVal || (newVal && !oldVal)) {
this.resolvedTitle = await this.computeTitle();
}
if (newVal.type) {
this.type = this.modelValue.type;
}
if (newVal.type) {
this.type = this.modelValue.type;
}
}
},
connextModelValue: {
immediate: true,
async handler(newVal, oldVal) {
if (!newVal) return;
if (newVal !== oldVal || (newVal && !oldVal)) {
this.resolvedTitle = await this.computeTitle();
}
if (newVal.type) {
this.type = this.modelValue.type;
}
}
},
transactionType(newVal, oldVal) {
Expand All @@ -94,16 +96,15 @@ export default {
},
methods: {
async computeTitle() {
console.log('type', this.type);
if (!this.modelValue && !this.connextModelValue) {
return this.getLabel(this.type);
}
if (this.type === 'connext' && this.connextModelValue.length) {
const txData = this.connextModelValue[1].data;
const decoded = await plugin.decodeConnextXcallData(txData);
const destinationChain = getNetworkKeyByDomainId(decoded[0]);
const destinationAddress = shorten(decoded[1]);
return `Connext transaction to ${destinationAddress} (${destinationChain})`;
}
if (!this.modelValue) {
return this.getLabel(this.type);
return `Cross-chain to ${destinationAddress} (${destinationChain})`;
}
if (this.modelValue) {
Expand Down Expand Up @@ -140,7 +141,7 @@ export default {
});
}
} catch (error) {
console.log('could not determine title', error);
console.warning('could not determine title', error);
}
}
Expand Down Expand Up @@ -173,7 +174,7 @@ export default {
};
</script>

<template>
<template>
<UiCollapsible
:hide-remove="config.preview"
:number="showNonce()"
Expand Down Expand Up @@ -272,7 +273,6 @@ export default {
:model-value="connextModelValue"
:config="config"
:is-details="isDetails"
:simulation-state="simulationState"
@update:modelValue="handleFormUpdate"
/>
</UiCollapsible>
Expand Down
33 changes: 14 additions & 19 deletions src/plugins/safeSnap/components/Form/TransactionBatchItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { watch, ref } from 'vue';
import { SafeTransaction, SafeTransactionConfig } from '@/helpers/interfaces';
import SafeSnapFormTransaction from './Transaction.vue';
import SafeSnapModalSingleTransaction from '../Modal/SingleTransaction.vue';
import { SimulationState } from '../Simulation/Tenderly.vue';
type TransactionWithType = {
type: 'standard' | 'connext';
Expand All @@ -28,9 +27,8 @@ const transactionToEdit = ref<TransactionToEditType>({
nonce: props.transactions.length > 0 ? props.transactions.length : 0
});
const allTransactions = ref<TransactionWithType[]>([]);
const showModal = ref(props.showSingleTransactionModal);
const showModal = ref<boolean>(props.showSingleTransactionModal);
const transactionBatchType = ref<'standard' | 'connext'>('standard');
const simulationState = ref<SimulationState>();
watch(
() => props.showSingleTransactionModal,
Expand Down Expand Up @@ -143,24 +141,24 @@ const handleStandardEdit = (transaction: SafeTransaction, nonce: string) => {
};
const handleConnextEdit = (transactions: SafeTransaction[]) => {
console.log('handleConnextEdit -', transactions);
transactionToEdit.value = {
transaction: transactions
};
showModal.value = true;
};
const handleCloseModal = () => {
showModal.value = false;
resetTransactionToEdit();
emits('onCloseModal');
transactionBatchType.value = 'standard';
};
const handleFormUpdate = (
index: number,
transaction: SafeTransaction,
oldTransaction: SafeTransaction | SafeTransaction[]
) => {
console.log('oldTransaction', oldTransaction);
if (transaction.transactionBatchType) {
transactionBatchType.value = transaction.transactionBatchType;
}
Expand All @@ -187,15 +185,14 @@ const handleFormUpdate = (
};
const handleConnextFormUpdate = (transactions: SafeTransaction[]) => {
console.log('handleConnextFormUpdate', transactions);
transactionBatchType.value = 'connext';
transactions.forEach(tx =>
emits('update:modelValue', { index: tx.nonce, transaction: tx })
);
};
const generateNonce = () => {
let maxNonce = -1; // Inicializar en -1 para que si no hay transacciones, el resultado sea 0
let maxNonce = -1; // Initialize to -1 so that if there are no transactions, the result is 0
allTransactions.value.forEach(transactionWithType => {
if (Array.isArray(transactionWithType.transactions)) {
Expand All @@ -213,10 +210,17 @@ const generateNonce = () => {
}
});
return maxNonce + 1; // Esto será 0 si no hay transacciones
return maxNonce + 1; // This will be 0 if there are no transactions
};
const getNonce = (batch: SafeTransaction | SafeTransaction[]): string => {
if (
transactionToEdit.value.nonce !== undefined &&
transactionToEdit.value.nonce !== null
) {
return transactionToEdit.value.nonce.toString();
}
if (batch) {
let nonceValue = '';
if (Array.isArray(batch)) {
Expand All @@ -225,23 +229,16 @@ const getNonce = (batch: SafeTransaction | SafeTransaction[]): string => {
nonceValue = batch.nonce;
}
// Verificar que nonceValue sea un string que representa un número
if (nonceValue && !isNaN(parseInt(nonceValue))) {
return nonceValue;
}
}
return generateNonce().toString(); // Esto devolverá '0' si no hay transacciones
return generateNonce().toString();
};
</script>

<template>
<div class="mt-3">
{{
console.log(
'parseInt(getNonce(transactionToEdit.transaction)',
getNonce(transactionToEdit.transaction)
)
}}
<div
v-for="(batch, index) in allTransactions"
:key="index"
Expand All @@ -254,13 +251,13 @@ const getNonce = (batch: SafeTransaction | SafeTransaction[]): string => {
:is-details="true"
:nonce="batch.transactions[0].nonce"
:transaction-type="'connext'"
:simulation-state="simulationState"
@remove="removeConnextItem(index)"
@edit="handleConnextEdit(batch.transactions)"
/>
</template>
<template v-else>
<SafeSnapFormTransaction
:key="`form-transaction-${index}`"
:model-value="batch.transactions"
:config="props.config"
:nonce="
Expand All @@ -282,7 +279,6 @@ const getNonce = (batch: SafeTransaction | SafeTransaction[]): string => {
</div>

<teleport to="#modal" v-if="transactionToEdit">
{{ console.log('transactionToEdit', transactionToEdit) }}
<SafeSnapModalSingleTransaction
:standard-model-value="(!Array.isArray(transactionToEdit.transaction) ? transactionToEdit.transaction : {} as SafeTransaction)"
:connext-model-value="
Expand All @@ -302,7 +298,6 @@ const getNonce = (batch: SafeTransaction | SafeTransaction[]): string => {
"
@update:connextModelValue="handleConnextFormUpdate($event)"
@close="handleCloseModal"
@update:simulation-state="simulation => (simulationState = simulation)"
/>
</teleport>
</div>
Expand Down
Loading

0 comments on commit 196f91f

Please sign in to comment.