@ton-agent-kit/plugin-escrow 1.0.3 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -0
- package/package.json +1 -1
- package/src/actions/auto-release-escrow.ts +72 -0
- package/src/actions/claim-reward.ts +48 -0
- package/src/actions/confirm-delivery.ts +60 -0
- package/src/actions/create-escrow.ts +44 -26
- package/src/actions/fallback-settle.ts +51 -0
- package/src/actions/get-escrow-info.ts +22 -4
- package/src/actions/join-dispute.ts +56 -0
- package/src/actions/open-dispute.ts +56 -0
- package/src/actions/refund-escrow.ts +31 -0
- package/src/actions/release-escrow.ts +30 -0
- package/src/actions/seller-stake.ts +69 -0
- package/src/actions/vote-refund.ts +50 -0
- package/src/actions/vote-release.ts +50 -0
- package/src/contracts/Escrow_Escrow.code.boc +0 -0
- package/src/contracts/Escrow_Escrow.ts +887 -37
- package/src/index.ts +60 -6
- package/src/utils.ts +75 -84
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Address, fromNano } from "@ton/core";
|
|
3
|
+
import { defineAction, toFriendlyAddress } from "@ton-agent-kit/core";
|
|
4
|
+
import { loadEscrows, sellerStakeOnContract, getContractState, getLatestTxHash } from "../utils";
|
|
5
|
+
|
|
6
|
+
export const sellerStakeAction = defineAction({
|
|
7
|
+
name: "seller_stake_escrow",
|
|
8
|
+
description:
|
|
9
|
+
"Seller (beneficiary) deposits their stake into an escrow that requires reputation collateral. " +
|
|
10
|
+
"Must be called before buyer deposits. Stake amount can be adjusted by reputation: " +
|
|
11
|
+
"score 90-100 = 50% of base, 60-89 = 100%, 30-59 = 150%, below minRepScore = blocked.",
|
|
12
|
+
schema: z.object({
|
|
13
|
+
escrowId: z.string().optional().describe("Escrow ID"),
|
|
14
|
+
contractAddress: z.string().optional().describe("Contract address (alternative to escrowId)"),
|
|
15
|
+
stakeAmount: z.string().optional().describe("Stake amount in TON. If not provided, uses baseSellerStake from escrow."),
|
|
16
|
+
}),
|
|
17
|
+
handler: async (agent, params) => {
|
|
18
|
+
const escrows = loadEscrows();
|
|
19
|
+
let escrow: any;
|
|
20
|
+
if (params.escrowId) {
|
|
21
|
+
escrow = escrows[params.escrowId];
|
|
22
|
+
} else if (params.contractAddress) {
|
|
23
|
+
escrow = Object.values(escrows).find((e: any) => e.contractAddress === params.contractAddress);
|
|
24
|
+
}
|
|
25
|
+
if (!escrow) {
|
|
26
|
+
return { staked: false, message: "Escrow not found" };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const contractAddress = Address.parse(escrow.contractAddress);
|
|
30
|
+
|
|
31
|
+
// Read on-chain state to check requirements
|
|
32
|
+
try {
|
|
33
|
+
const state = await getContractState(agent, contractAddress);
|
|
34
|
+
|
|
35
|
+
if (!state.requireSellerStake) {
|
|
36
|
+
return { staked: false, message: "This escrow does not require seller stake" };
|
|
37
|
+
}
|
|
38
|
+
if (state.sellerStaked) {
|
|
39
|
+
return { staked: false, message: "Seller has already staked" };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const stakeAmount = params.stakeAmount || fromNano(state.baseSellerStake);
|
|
43
|
+
|
|
44
|
+
await sellerStakeOnContract(agent, contractAddress, stakeAmount);
|
|
45
|
+
|
|
46
|
+
const txHash = await getLatestTxHash(
|
|
47
|
+
agent.wallet.address.toRawString(),
|
|
48
|
+
agent.network,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
staked: true,
|
|
53
|
+
amount: stakeAmount + " TON",
|
|
54
|
+
escrowId: escrow.id,
|
|
55
|
+
contractAddress: escrow.contractAddress,
|
|
56
|
+
friendlyContract: toFriendlyAddress(contractAddress, agent.network),
|
|
57
|
+
stakeTxHash: txHash,
|
|
58
|
+
message: `Seller staked ${stakeAmount} TON. Buyer can now deposit.`,
|
|
59
|
+
};
|
|
60
|
+
} catch (err: any) {
|
|
61
|
+
return {
|
|
62
|
+
staked: false,
|
|
63
|
+
escrowId: escrow.id,
|
|
64
|
+
error: err.message,
|
|
65
|
+
message: `Failed to stake: ${err.message}`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Address } from "@ton/core";
|
|
3
|
+
import { defineAction, toFriendlyAddress } from "@ton-agent-kit/core";
|
|
4
|
+
import { loadEscrows, saveEscrows, voteRefundOnContract, getLatestTxHash } from "../utils";
|
|
5
|
+
|
|
6
|
+
export const voteRefundAction = defineAction({
|
|
7
|
+
name: "vote_refund",
|
|
8
|
+
description:
|
|
9
|
+
"Vote to refund escrow funds to the depositor during a multi-arbiter dispute. Must be called by a registered arbiter. When majority is reached, funds are automatically refunded.",
|
|
10
|
+
schema: z.object({
|
|
11
|
+
escrowId: z.string().describe("Escrow ID to vote refund on"),
|
|
12
|
+
}),
|
|
13
|
+
handler: async (agent, params) => {
|
|
14
|
+
const escrows = loadEscrows();
|
|
15
|
+
const escrow = escrows[params.escrowId];
|
|
16
|
+
if (!escrow) {
|
|
17
|
+
return { voted: false, message: `Escrow not found: ${params.escrowId}` };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const contractAddress = Address.parse(escrow.contractAddress);
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
await voteRefundOnContract(agent, contractAddress);
|
|
24
|
+
|
|
25
|
+
const txHash = await getLatestTxHash(
|
|
26
|
+
agent.wallet.address.toRawString(),
|
|
27
|
+
agent.network,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
voted: true,
|
|
32
|
+
vote: "refund",
|
|
33
|
+
escrowId: params.escrowId,
|
|
34
|
+
contractAddress: escrow.contractAddress,
|
|
35
|
+
friendlyContract: toFriendlyAddress(contractAddress, agent.network),
|
|
36
|
+
voter: agent.wallet.address.toRawString(),
|
|
37
|
+
voteTxHash: txHash,
|
|
38
|
+
message: `Voted to refund escrow ${params.escrowId}. Contract will auto-execute when majority is reached.`,
|
|
39
|
+
};
|
|
40
|
+
} catch (err: any) {
|
|
41
|
+
return {
|
|
42
|
+
voted: false,
|
|
43
|
+
vote: "refund",
|
|
44
|
+
escrowId: params.escrowId,
|
|
45
|
+
error: err.message,
|
|
46
|
+
message: `Vote failed: ${err.message}`,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Address } from "@ton/core";
|
|
3
|
+
import { defineAction, toFriendlyAddress } from "@ton-agent-kit/core";
|
|
4
|
+
import { loadEscrows, saveEscrows, voteReleaseOnContract, getLatestTxHash } from "../utils";
|
|
5
|
+
|
|
6
|
+
export const voteReleaseAction = defineAction({
|
|
7
|
+
name: "vote_release",
|
|
8
|
+
description:
|
|
9
|
+
"Vote to release escrow funds to the beneficiary during a multi-arbiter dispute. Must be called by a registered arbiter. When majority is reached, funds are automatically released.",
|
|
10
|
+
schema: z.object({
|
|
11
|
+
escrowId: z.string().describe("Escrow ID to vote release on"),
|
|
12
|
+
}),
|
|
13
|
+
handler: async (agent, params) => {
|
|
14
|
+
const escrows = loadEscrows();
|
|
15
|
+
const escrow = escrows[params.escrowId];
|
|
16
|
+
if (!escrow) {
|
|
17
|
+
return { voted: false, message: `Escrow not found: ${params.escrowId}` };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const contractAddress = Address.parse(escrow.contractAddress);
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
await voteReleaseOnContract(agent, contractAddress);
|
|
24
|
+
|
|
25
|
+
const txHash = await getLatestTxHash(
|
|
26
|
+
agent.wallet.address.toRawString(),
|
|
27
|
+
agent.network,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
voted: true,
|
|
32
|
+
vote: "release",
|
|
33
|
+
escrowId: params.escrowId,
|
|
34
|
+
contractAddress: escrow.contractAddress,
|
|
35
|
+
friendlyContract: toFriendlyAddress(contractAddress, agent.network),
|
|
36
|
+
voter: agent.wallet.address.toRawString(),
|
|
37
|
+
voteTxHash: txHash,
|
|
38
|
+
message: `Voted to release escrow ${params.escrowId}. Contract will auto-execute when majority is reached.`,
|
|
39
|
+
};
|
|
40
|
+
} catch (err: any) {
|
|
41
|
+
return {
|
|
42
|
+
voted: false,
|
|
43
|
+
vote: "release",
|
|
44
|
+
escrowId: params.escrowId,
|
|
45
|
+
error: err.message,
|
|
46
|
+
message: `Vote failed: ${err.message}`,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
});
|
|
Binary file
|