-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex4-transferir-realtokenizado-varejo.ts
More file actions
151 lines (132 loc) · 4.52 KB
/
ex4-transferir-realtokenizado-varejo.ts
File metadata and controls
151 lines (132 loc) · 4.52 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { ethers } from "hardhat";
import {
getBalanceCBDCSync,
getBalanceRTSync,
getPLInformation,
TimeoutExecution
} from "../utils/utils";
import IendpointContractABI from "../abi/IEndpoint.json";
import CbdcABI from "../abi/CBDC.json";
import RealTokenizadoABI from "../abi/RealTokenizado.json";
import { Log } from "ethers";
interface TypedEventLog extends Log {
args: {
_reqNonce: bigint;
};
}
async function example4() {
const {
endpointContractAddr,
cbdcResourceId,
wdResourceId,
rtResourceId
} = await getPLInformation();
const [deployerSigner, clientSigner] = await ethers.getSigners();
const chainIdDestination = process.env.DEST_CHAINID ?? 0;
const destClientAcc = process.env.DEST_CLIENT_ACC ?? 0;
const amountToMintAndSwap = ethers.parseUnits("10", 2);
const endpointContract = await ethers.getContractAt(
IendpointContractABI,
endpointContractAddr,
deployerSigner
);
const walletDefault = await endpointContract.resourceIdToContractAddress(
wdResourceId
);
const realTokenizadoAddr = await endpointContract.resourceIdToContractAddress(
rtResourceId
);
const cbdcContractAddr = await endpointContract.resourceIdToContractAddress(
cbdcResourceId
);
const realTokenizadoContract = await ethers.getContractAt(
RealTokenizadoABI,
realTokenizadoAddr,
deployerSigner
);
console.log("[DEBUG] Minting Real Tokenizado for the client at origin...");
const txMint = await realTokenizadoContract.mint(clientSigner.address, amountToMintAndSwap);
await txMint.wait();
const cbdcContract = await ethers.getContractAt(
CbdcABI,
cbdcContractAddr,
clientSigner
);
const balancRTBefore = await getBalanceRTSync(
endpointContract,
rtResourceId,
clientSigner,
clientSigner.address
) ?? BigInt(0);
console.log("[DEBUG] balancRTBefore:", balancRTBefore);
const balanceCDBCBefore = await getBalanceCBDCSync(
endpointContract,
cbdcResourceId,
deployerSigner,
walletDefault
) ?? BigInt(0);
console.log("[DEBUG] balanceCDBCBefore:", balanceCDBCBefore);
console.log("[DEBUG] Approving RealTokenizado amount for CBDC contract address...");
const txApproveRT = await realTokenizadoContract
.connect(clientSigner)
.approve(
cbdcContractAddr,
amountToMintAndSwap
);
await txApproveRT.wait();
console.log(`[DEBUG] swap ...`);
const txSwap = await cbdcContract.swap(
chainIdDestination,
cbdcResourceId,
destClientAcc,
amountToMintAndSwap
);
await txSwap.wait();
const recipeTxSwap = await txSwap.wait();
let prevBlockNumber = recipeTxSwap?.blockNumber ?? 0;
let swapNonce = BigInt(0);
const dispatchEvents = await cbdcContract.queryFilter(
cbdcContract.filters.swapDispatched(),
prevBlockNumber,
prevBlockNumber
) as TypedEventLog[];
if (dispatchEvents.length > 0) {
swapNonce = dispatchEvents[0].args._reqNonce;
}
console.log("[DEBUG] swapNonce:", swapNonce);
console.time("Waiting swap acknowledgement");
const swapAcknowledged = await TimeoutExecution(async (retry) => {
const currentBlockNumber = await ethers.provider.getBlockNumber();
const events = await cbdcContract.queryFilter(
cbdcContract.filters.swapAcknowledged(),
prevBlockNumber,
currentBlockNumber
) as TypedEventLog[];
prevBlockNumber = currentBlockNumber;
if (events.length > 0 && events[0].args._reqNonce == swapNonce) {
return [true, events[0].args._reqNonce];
} else return [false, BigInt(0)];
});
console.timeEnd("Waiting swap acknowledgement");
console.log("[DEBUG] swapAcknowledged:", swapAcknowledged);
const balancRTAfter = await getBalanceRTSync(
endpointContract,
rtResourceId,
clientSigner,
clientSigner.address
) ?? BigInt(0);
console.log("[DEBUG] balancRTAfter:", balancRTAfter);
const balanceCDBCAfter = await getBalanceCBDCSync(
endpointContract,
cbdcResourceId,
deployerSigner,
walletDefault
) ?? BigInt(0);
console.log("[DEBUG] balanceCDBCAfter:", balanceCDBCAfter);
}
example4()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});