Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absolute tx deadline #60

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Refresh the list of deployed contracts by running `./scripts/generateDeployments

- [Escrow](https://sepolia.arbiscan.io/address/0x250AB0477346aDFC010585b58FbF61cff1d8f3ea)
- [EscrowToken](https://sepolia.arbiscan.io/address/0xe7B52a7Bc50cb4Ff13d057629082844780b6E28D)
- [EscrowUniversal](https://sepolia.arbiscan.io/address/0x5c4B2Cff2998529f9d405A9b6EbB44BC537c8D7F)
- [EscrowUniversal](https://sepolia.arbiscan.io/address/0xBa2cC03e2e34C5DEDaEbC481b2531e06946353De)

#### Sepolia

Expand Down
120 changes: 60 additions & 60 deletions contracts/deployments/arbitrumSepoliaDevnet/EscrowUniversal.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions contracts/src/Escrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ contract Escrow is IEscrow, IArbitrableV2 {

/// @inheritdoc IEscrow
function createNativeTransaction(
uint256 _timeoutPayment,
uint256 _deadline,
string memory _transactionUri,
address payable _seller,
string memory _templateData,
Expand All @@ -124,7 +124,7 @@ contract Escrow is IEscrow, IArbitrableV2 {
transaction.buyer = payable(msg.sender);
transaction.seller = _seller;
transaction.amount = msg.value;
transaction.deadline = block.timestamp + _timeoutPayment;
transaction.deadline = _deadline;
transaction.templateData = _templateData;
transaction.templateDataMappings = _templateDataMappings;

Expand Down
4 changes: 2 additions & 2 deletions contracts/src/EscrowToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ contract EscrowToken is IEscrow, IArbitrableV2 {
function createERC20Transaction(
uint256 _amount,
IERC20 _token,
uint256 _timeoutPayment,
uint256 _deadline,
string memory _transactionUri,
address payable _seller,
string memory _templateData,
Expand All @@ -149,7 +149,7 @@ contract EscrowToken is IEscrow, IArbitrableV2 {
transaction.amount = _amount;
transaction.token = _token;
// transaction.paymentType = PaymentType.ERC20;
transaction.deadline = block.timestamp + _timeoutPayment;
transaction.deadline = _deadline;
transaction.templateData = _templateData;
transaction.templateDataMappings = _templateDataMappings;

Expand Down
8 changes: 4 additions & 4 deletions contracts/src/EscrowUniversal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ contract EscrowUniversal is IEscrow, IArbitrableV2 {

/// @inheritdoc IEscrow
function createNativeTransaction(
uint256 _timeoutPayment,
uint256 _deadline,
string memory _transactionUri,
address payable _seller,
string memory _templateData,
Expand All @@ -134,7 +134,7 @@ contract EscrowUniversal is IEscrow, IArbitrableV2 {
transaction.seller = _seller;
transaction.amount = msg.value;
transaction.token = NATIVE;
transaction.deadline = block.timestamp + _timeoutPayment;
transaction.deadline = _deadline;
transaction.templateData = _templateData;
transaction.templateDataMappings = _templateDataMappings;

Expand All @@ -154,7 +154,7 @@ contract EscrowUniversal is IEscrow, IArbitrableV2 {
function createERC20Transaction(
uint256 _amount,
IERC20 _token,
uint256 _timeoutPayment,
uint256 _deadline,
string memory _transactionUri,
address payable _seller,
string memory _templateData,
Expand All @@ -167,7 +167,7 @@ contract EscrowUniversal is IEscrow, IArbitrableV2 {
transaction.seller = _seller;
transaction.amount = _amount;
transaction.token = _token;
transaction.deadline = block.timestamp + _timeoutPayment;
transaction.deadline = _deadline;
transaction.templateData = _templateData;
transaction.templateDataMappings = _templateDataMappings;

Expand Down
8 changes: 4 additions & 4 deletions contracts/src/interfaces/IEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ interface IEscrow {
// ************************************* //

/// @dev Create a transaction.
/// @param _timeoutPayment Time after which a party can automatically execute the arbitrable transaction.
/// @param _deadline Time after which a party can automatically execute the arbitrable transaction.
/// @param _transactionUri The IPFS Uri Hash of the transaction.
/// @param _seller The recipient of the transaction.
/// @param _templateData The dispute template data.
/// @param _templateDataMappings The dispute template data mappings.
/// @return transactionID The index of the transaction.
function createNativeTransaction(
uint256 _timeoutPayment,
uint256 _deadline,
string memory _transactionUri,
address payable _seller,
string memory _templateData,
Expand All @@ -91,7 +91,7 @@ interface IEscrow {
/// @dev Create a transaction.
/// @param _amount The amount of tokens in this transaction.
/// @param _token The ERC20 token contract.
/// @param _timeoutPayment Time after which a party can automatically execute the arbitrable transaction.
/// @param _deadline Time after which a party can automatically execute the arbitrable transaction.
/// @param _transactionUri The IPFS Uri Hash of the transaction.
/// @param _seller The recipient of the transaction.
/// @param _templateData The dispute template data.
Expand All @@ -100,7 +100,7 @@ interface IEscrow {
function createERC20Transaction(
uint256 _amount,
IERC20 _token,
uint256 _timeoutPayment,
uint256 _deadline,
string memory _transactionUri,
address payable _seller,
string memory _templateData,
Expand Down
2 changes: 1 addition & 1 deletion subgraph/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kleros/escrow-v2-subgraph",
"version": "2.0.0",
"version": "2.0.1",
"license": "MIT",
"scripts": {
"update:arbitrum-sepolia-devnet": "./scripts/update.sh arbitrumSepoliaDevnet arbitrum-sepolia",
Expand Down
4 changes: 2 additions & 2 deletions subgraph/subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ dataSources:
name: EscrowUniversal
network: arbitrum-sepolia
source:
address: '0x5c4B2Cff2998529f9d405A9b6EbB44BC537c8D7F'
address: '0xBa2cC03e2e34C5DEDaEbC481b2531e06946353De'
abi: EscrowUniversal
startBlock: 42653663
startBlock: 53588833
mapping:
kind: ethereum/events
apiVersion: 0.0.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const DepositPaymentButton: React.FC = () => {
resetContext,
} = useNewTransactionContext();

const [currentTime, setCurrentTime] = useState(Date.now());
const [finalRecipientAddress, setFinalRecipientAddress] = useState(sellerAddress);
const publicClient = usePublicClient();
const navigateAndScrollTop = useNavigateAndScrollTop();
Expand All @@ -50,32 +49,19 @@ const DepositPaymentButton: React.FC = () => {
const { address } = useAccount();
const { chain } = useNetwork();
const ensResult = useEnsAddress({ name: sellerAddress, chainId: 1 });
const deadlineTimestamp = useMemo(() => new Date(deadline).getTime(), [deadline]);
const timeoutPayment = useMemo(() => (deadlineTimestamp - currentTime) / 1000, [deadlineTimestamp, currentTime]);
const deadlineTimestamp = useMemo(() => BigInt(Math.floor(new Date(deadline).getTime() / 1000)), [deadline]);
const isNativeTransaction = sendingToken?.address === "native";
const transactionValue = useMemo(
() => (isNativeTransaction ? parseEther(sendingQuantity) : parseUnits(sendingQuantity, 18)),
[isNativeTransaction, sendingQuantity]
);
const isDeadlinePassed = currentTime >= deadlineTimestamp;

useEffect(() => {
if (isDeadlinePassed) {
navigateAndScrollTop("/new-transaction/deadline");
}
}, [isDeadlinePassed, navigateAndScrollTop]);

useEffect(() => {
const intervalId = setInterval(() => setCurrentTime(Date.now()), 1000);
return () => clearInterval(intervalId);
}, []);

useEffect(() => {
setFinalRecipientAddress(ensResult.data || sellerAddress);
}, [sellerAddress, ensResult.data]);

const { data: allowance } = useContractRead({
enabled: !isNativeTransaction && !isDeadlinePassed,
const { data: allowance, refetch: refetchAllowance } = useContractRead({
enabled: !isNativeTransaction,
kemuru marked this conversation as resolved.
Show resolved Hide resolved
address: sendingToken?.address,
abi: erc20ABI,
functionName: "allowance",
Expand Down Expand Up @@ -120,7 +106,7 @@ const DepositPaymentButton: React.FC = () => {
seller: sellerAddress,
amount: sendingQuantity,
token: isNativeTransaction ? "native" : sendingToken?.address,
timeoutPayment: timeoutPayment,
deadline: deadlineTimestamp.toString(),
transactionUri: transactionUri,
},
category: "Escrow",
Expand All @@ -140,14 +126,14 @@ const DepositPaymentButton: React.FC = () => {
address,
isNativeTransaction,
sendingToken?.address,
timeoutPayment,
deadlineTimestamp,
transactionUri,
]
);

const { config: createNativeTransactionConfig } = usePrepareEscrowUniversalCreateNativeTransaction({
enabled: isNativeTransaction && ethAddressPattern.test(finalRecipientAddress) && !isDeadlinePassed,
args: [BigInt(Math.floor(timeoutPayment)), transactionUri, finalRecipientAddress, templateData, ""],
enabled: isNativeTransaction && ethAddressPattern.test(finalRecipientAddress),
args: [deadlineTimestamp, transactionUri, finalRecipientAddress, templateData, ""],
kemuru marked this conversation as resolved.
Show resolved Hide resolved
value: transactionValue,
});

Expand All @@ -156,12 +142,11 @@ const DepositPaymentButton: React.FC = () => {
!isNativeTransaction &&
!isUndefined(allowance) &&
allowance >= transactionValue &&
ethAddressPattern.test(finalRecipientAddress) &&
!isDeadlinePassed,
ethAddressPattern.test(finalRecipientAddress),
args: [
transactionValue,
sendingToken?.address,
BigInt(Math.floor(timeoutPayment)),
deadlineTimestamp,
transactionUri,
finalRecipientAddress,
templateData,
Expand All @@ -174,7 +159,7 @@ const DepositPaymentButton: React.FC = () => {
const { writeAsync: createERC20Transaction } = useEscrowUniversalCreateErc20Transaction(createERC20TransactionConfig);

const { config: approveConfig } = usePrepareContractWrite({
enabled: !isNativeTransaction && !isDeadlinePassed,
enabled: !isNativeTransaction,
kemuru marked this conversation as resolved.
Show resolved Hide resolved
address: sendingToken?.address,
abi: erc20ABI,
functionName: "approve",
Expand All @@ -192,6 +177,7 @@ const DepositPaymentButton: React.FC = () => {
publicClient
);
setIsApproved(wrapResult.status);
await refetchAllowance();
kemuru marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
console.error("Approval failed:", error);
setIsApproved(false);
Expand Down Expand Up @@ -226,7 +212,7 @@ const DepositPaymentButton: React.FC = () => {
return (
<StyledButton
isLoading={isSending}
disabled={isSending || isDeadlinePassed}
disabled={isSending}
kemuru marked this conversation as resolved.
Show resolved Hide resolved
text={isNativeTransaction || isApproved ? "Deposit the Payment" : "Approve Token"}
onClick={isNativeTransaction || isApproved ? handleCreateTransaction : handleApproveToken}
/>
Expand Down
Loading