-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.ts
108 lines (96 loc) · 4.9 KB
/
web3.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Web3, Eip712TypedData } from "web3";
import { TransferSchedulerContractAddress, transferSchedulerABI } from './constants';
import { ScheduledTransfer, TransferScheduledEventLog, QueuedTransferRecords, AddressNonceRecord, Status } from './types';
// Function to create typed data for Scheduled Transfer
export function createTypedData(
chainId: number,
scheduledTransfer: ScheduledTransfer
): Eip712TypedData {
return {
primaryType: 'ScheduledTransfer',
domain: {
name: 'TransferScheduler',
version: '1',
chainId: chainId,
verifyingContract: TransferSchedulerContractAddress
},
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" }
],
ScheduledTransfer: [
{ name: 'owner', type: 'address' },
{ name: 'nonce', type: 'uint96' },
{ name: 'token', type: 'address' },
{ name: 'to', type: 'address' },
{ name: 'amount', type: 'uint128' },
{ name: 'spender', type: 'address' },
{ name: 'notBeforeDate', type: 'uint40' },
{ name: 'notAfterDate', type: 'uint40' },
{ name: 'maxBaseFee', type: 'uint40' },
]
},
message: scheduledTransfer
};
}
export async function getGasTokenAddress(web3: Web3) {
const scheduledTransferContract = new web3.eth.Contract(transferSchedulerABI, TransferSchedulerContractAddress);
const gasTokenAddress: string = await scheduledTransferContract.methods.getRelayGasToken().call();
return gasTokenAddress;
}
export async function getRelayCharge(web3: Web3, maxBaseFee: number) {
const scheduledTransferContract = new web3.eth.Contract(transferSchedulerABI, TransferSchedulerContractAddress);
const relayGasCommissionPercentage: bigint = await scheduledTransferContract.methods.getRelayGasCommissionPercentage().call();
const relayGasUsage: bigint = await scheduledTransferContract.methods.relayGasUsage().call();
const relayCharge = (maxBaseFee * Number(relayGasUsage) * (100 + Number(relayGasCommissionPercentage)) / 100);
return relayCharge;
}
export async function fetchQueuedTransfers(web3: Web3, address: `0x${string}`, status: Status) {
const scheduledTransferContract = new web3.eth.Contract(transferSchedulerABI, TransferSchedulerContractAddress);
const transferScheduledEventLogs: TransferScheduledEventLog[] = [];
try {
const queuedTransferRecords: QueuedTransferRecords = await scheduledTransferContract.methods.getScheduledTransfers(address, status).call();
for (let i = 0; i < queuedTransferRecords.length; i++) {
const transfer = queuedTransferRecords[i];
//const nonce = transfer.nonce.toString();
const blockNumber = transfer.blockNumber.toString();
const events = await scheduledTransferContract.getPastEvents('TransferScheduled', {
filter: { owner: address },
fromBlock: BigInt(blockNumber),
toBlock: BigInt(blockNumber)
});
for (let event of events) {
if (typeof event === 'object' && 'returnValues' in event) {
transferScheduledEventLogs.push(event.returnValues as TransferScheduledEventLog);
}
}
}
} catch (err: any) {
console.error(err);
}
return transferScheduledEventLogs;
}
export async function queueScheduledTransfer(web3: Web3, scheduledTransfer: ScheduledTransfer, signature: string, from: string) {
const scheduledTransferContract = new web3.eth.Contract(transferSchedulerABI, TransferSchedulerContractAddress);
return await scheduledTransferContract.methods.queueScheduledTransfer(scheduledTransfer.owner,
scheduledTransfer.nonce,
scheduledTransfer.token,
scheduledTransfer.to,
scheduledTransfer.amount,
scheduledTransfer.notBeforeDate,
scheduledTransfer.notAfterDate,
scheduledTransfer.maxBaseFee,
signature).send({ from: from });
}
export async function getTransfers(web3: Web3, address: `0x${string}`, nonce: number) {
const scheduledTransferContract = new web3.eth.Contract(transferSchedulerABI, TransferSchedulerContractAddress);
const addressNonceRecord: AddressNonceRecord = await scheduledTransferContract.methods.transfers(address, nonce).call();
return addressNonceRecord;
}
export async function cancelScheduledTransfer(web3: Web3, address: `0x${string}`, nonce: number) {
const scheduledTransferContract = new web3.eth.Contract(transferSchedulerABI, TransferSchedulerContractAddress);
return await scheduledTransferContract.methods.cancelScheduledTransfer(address, nonce).send();
}