Here you'll find the basic flow in order to use Trustless Work API. In this flows tutorial, we'll use as an example the integration of Trustless Work in our dApp.
Cada escrow debe ser financiado antes de liberar fondos o resolver disputas. Es importante aclarar que puedes financias el escrow en cualquier momento, pero para este caso, lo financiaremos desde el principio.
export const fundEscrow = async (payload: FundEscrowPayload) => {
try {
// Get the address
const { address } = await kit.getAddress();
// Execute the endpoint
const response = await http.post("/escrow/fund-escrow", payload);
// Get the unsigned transaction hash
const { unsignedTransaction } = response.data;
// Sign the transaction by wallet
const { signedTxXdr } = await signTransaction(unsignedTransaction, {
address,
networkPassphrase: WalletNetwork.TESTNET,
});
// Send the transaction to Stellar Network
const tx = await http.post("/helper/send-transaction", {
signedXdr: signedTxXdr,
});
const { data } = tx;
return data;
} catch (error: unknown) {
// catch code...
}
};
Referencias:
These endpoints don't have any specific order to execute them
Editar Hitos
Puedes editar los hitos en cualquier momento, pero sólo la entidad de la plataforma (platform entity) podrá hacerlo. Sólo los hitos pendientes serán editables.
export const editMilestones = async (payload: EditMilestonesPayload) => {
try {
// Get the address
const { address } = await kit.getAddress();
// Execute the endpoint
const response = await http.put(
"/escrow/update-escrow-by-contract-id",
payload,
);
// Get the unsigned transaction hash
const { unsignedTransaction } = response.data;
// Sign the transaction by wallet
const { signedTxXdr } = await signTransaction(unsignedTransaction, {
address,
networkPassphrase: WalletNetwork.TESTNET,
});
// Send the transaction to Stellar Network
const tx = await http.post("/helper/send-transaction", {
signedXdr: signedTxXdr,
});
const { data } = tx;
return data;
} catch (error: unknown) {
// catch code...
}
};
Referencias:
ADD REFERENCES
Cambiar el estado del hito
Con este endpoint podrás cambiar el estado de los hitos, pero sólo el proveedor de servicios (service provider) podrá hacerlo.
Payload Type:
export type ChangeMilestoneStatusPayload = {
contractId?: string;
milestoneIndex: string;
newStatus: MilestoneStatus; // you can custom your status
serviceProvider?: string;
};
Ejecutar Endpoint:
export const changeMilestoneStatus = async (
payload: ChangeMilestoneStatusPayload,
) => {
try {
// Get the address
const { address } = await kit.getAddress();
// Execute the endpoint
const response = await http.post(
"/escrow/change-milestone-status",
payload,
);
// Get the unsigned transaction hash
const { unsignedTransaction } = response.data;
// Sign the transaction by wallet
const { signedTxXdr } = await signTransaction(unsignedTransaction, {
address,
networkPassphrase: WalletNetwork.TESTNET,
});
// Send the transaction to Stellar Network
const tx = await http.post("/helper/send-transaction", {
signedXdr: signedTxXdr,
});
const { data } = tx;
return data;
} catch (error: unknown) {
// catch code...
}
};
Referencias:
Cambiar flag del hito
Con este endpoint podrás aprobar los hitos, pero sólo el aprobador (approver) podrá hacerlo.
export const changeMilestoneFlag = async (
payload: ChangeMilestoneFlagPayload,
) => {
try {
// Get the address
const { address } = await kit.getAddress();
// Execute the endpoint
const response = await http.post(
"/escrow/change-milestone-flag",
payload,
);
// Get the unsigned transaction hash
const { unsignedTransaction } = response.data;
// Sign the transaction by wallet
const { signedTxXdr } = await signTransaction(unsignedTransaction, {
address,
networkPassphrase: WalletNetwork.TESTNET,
});
// Send the transaction to Stellar Network
const tx = await http.post("/helper/send-transaction", {
signedXdr: signedTxXdr,
});
const { data } = tx;
return data;
} catch (error: unknown) {
// catch code...
}
};
Referencias:
Cambiar flag de la disputa
En cualquier momento, el aprobador (approver) o proveedor de servicio (service provider) puede iniciar una disputa. Esto no puede ser ejecutado sin fondos en el balance del escrow.
export const startDispute = async (
payload: StartDisputePayload,
) => {
try {
// Get the address
const { address } = await kit.getAddress();
// Execute the endpoint
const response = await http.post(
"/escrow/change-dispute-flag",
payload,
);
// Get the unsigned transaction hash
const { unsignedTransaction } = response.data;
// Sign the transaction by wallet
const { signedTxXdr } = await signTransaction(unsignedTransaction, {
address,
networkPassphrase: WalletNetwork.TESTNET,
});
// Send the transaction to Stellar Network
const tx = await http.post("/helper/send-transaction", {
signedXdr: signedTxXdr,
});
const { data } = tx;
return data;
} catch (error: unknown) {
// catch code...
}
};
Referencias:
Caso #1: Si todos los hitos son completados y aprobados, podrás liberar los fondos.
Distribuir ganancias del escrow
Cuando el escrow se encuentra listo para ser liberado, puedes hacerlo con este endpoint. Sólo el firmante de la liberación (release signer) puede hacerlo.
export const resolveDispute = async (
payload: ResolveDisputePayload,
) => {
try {
// Get the address
const { address } = await kit.getAddress();
// Execute the endpoint
const response = await http.post(
"/escrow/resolving-disputes",
payload,
);
// Get the unsigned transaction hash
const { unsignedTransaction } = response.data;
// Sign the transaction by wallet
const { signedTxXdr } = await signTransaction(unsignedTransaction, {
address,
networkPassphrase: WalletNetwork.TESTNET,
});
// Send the transaction to Stellar Network
const tx = await http.post("/helper/send-transaction", {
signedXdr: signedTxXdr,
});
const { data } = tx;
return data;
} catch (error: unknown) {
// catch code...
}
};
Referencias:
Nota importante
Todos los endpoints relacionados con la gestión de escrows devuelven la transacción sin firmar.
Esto se realiza mediante una cadena en formato XDR (External Data Representation), que es el formato que Stellar utiliza para codificar transacciones. Esta cadena XDR es la que debes usar para firmar la transacción con la billetera de tu elección. Una vez firmada, la transacción se devuelve firmada en el mismo formato, es decir, como un string en formato XDR.
Este string es el que debe ser enviado al siguiente endpoint para que la transacción sea enviado a la red de Stellar: