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.
Each escrow must be funded before the release funds or resolve dispute. It's important to clarify that you can fund the escrow in any moment, but just for this case, we'll fund it in the beginning.
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...
}
};
References:
ADD REFERENCES
Change Milestone Status
With this endpoint you'll change the status of the milestones, but just the Service Provider will be able to do it.
Payload Type:
export type ChangeMilestoneStatusPayload = {
contractId?: string;
milestoneIndex: string;
newStatus: MilestoneStatus; // you can custom your status
serviceProvider?: string;
};
Execute 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...
}
};
References:
Change Milestone Flag
With this endpoint you'll approve the milestones, but just the Approver will be able to do it.
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...
}
};
References:
IMPORTANT NOTE
All endpoints related to escrow management return the transaction unsigned. This is done by means of a string returned in XDR (External Data Representation) format, which is a format that stellar uses to encode transactions. This string is what you should use to sign the transaction with the wallet of your choice. After being signed it will return the transaction signed in the same way by means of a string in XDR format.
This string is the one that must be sent to the next endpoint for the transaction to be sent to the Stellar network: