-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdestination.js
More file actions
155 lines (123 loc) · 4.37 KB
/
destination.js
File metadata and controls
155 lines (123 loc) · 4.37 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
151
152
153
154
155
// AVALANCHE
require("dotenv").config()
const JSONdb = require('simple-json-db');
const logger = require("./logger")
// instance cryptum-sdk
const CryptumSDK = require('cryptum-sdk');
const db = new JSONdb('storage.json')
//
const ENVIROMENT = process.env.ENVIROMENT // testnet
const MNEMONIC = process.env.MNEMONIC; //
const API_KEY = process.env.API_KEY; //
const PROTOCOL = process.env.PROTOCOL_DESTINATION; // ETHEREUM
//
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const sdk = new CryptumSDK({
environment: ENVIROMENT, // 'testnet' or 'mainnet'
apiKey: API_KEY,
})
// Generate wallet
const getWallet = async () => await sdk.wallet.generateWallet({
protocol: PROTOCOL,
mnemonic: MNEMONIC
})
const getContractAddressByHash = async (hash) => {
const { contractAddress } = await sdk.transaction.getTransactionReceiptByHash({
protocol: PROTOCOL,
hash
})
return contractAddress
}
const createContractCCIP = async () => {
const wallet = await getWallet()
console.log(`wallet address ${wallet.address}`)
const { hash } = await sdk.chainlink.createCCIP({
protocol: PROTOCOL,
wallet
})
console.log(`Waiting for contract address`)
let contractAddress
let attempts = 1;
// loop para buscar o endereço de contrato,
// isso se faz necessário pois temos que esperar as confirmação do bloco, isso pode variar de acordo com a rede
await delay(5000)
while (!contractAddress) {
try {
console.log(`attempt: ${attempts}`)
contractAddress = await getContractAddressByHash(hash)
} catch (e) {
attempts++
await delay(5000)
}
}
logger.info({ contractAddress, protocol: PROTOCOL })
return contractAddress
}
const addFund = async (contractAddress, token, amount) => {
if (!contractAddress || !token || !amount) throw Error("Arguments contractAddress, token and amount required!")
const wallet = await getWallet()
console.log(`wallet address ${wallet.address}`)
const { hash } = await sdk.token.transfer({
wallet,
protocol: PROTOCOL,
token,
destination: contractAddress, // contractAddress
amount: amount,
})
logger.info({ hash, call_method: function_name, token, amount, protocol: PROTOCOL })
console.log({ hash })
return hash
}
const allowSender = async (contractAddress, senderAddress, allowed) => {
const wallet = await getWallet()
const { hash } = await sdk.chainlink.allowSenderCCIP({
protocol: PROTOCOL,
wallet,
address: contractAddress,
senderAddress,
allowed
})
logger.info({ hash, call_method: function_name, senderAddress, allowed, contractAddress })
return hash
}
const getLastReceivedMessageDetailsCCIP = async (contractAddress) => {
return await sdk.chainlink.getLastReceivedMessageDetailsCCIP({
protocol: PROTOCOL,
address: contractAddress
})
}
const receiveMessage = async (contractAddress) => {
if (!contractAddress) throw Error("contractAddress required!")
while (true) {
console.log(`Waiting new message...`)
const message = await getLastReceivedMessageDetailsCCIP(contractAddress)
if (message.messageId === "0x0000000000000000000000000000000000000000000000000000000000000000") {
await delay(30000)
continue
};
const message_db = db.get(message.messageId)
if (message_db) {
await delay(30000)
continue;
}
console.log(`New message received ${message.messageId}`)
db.set(message.messageId, message)
}
}
const arguments = process.argv.slice(2)
const [function_name] = arguments;
if (!function_name) throw Error("Argument required!")
const functions = {
'create_ccip': createContractCCIP,
'add_funds': () => addFund(arguments[1], arguments[2], arguments[3]),
'allow_sender': () => allowSender(arguments[1], arguments[2], arguments[3] == "true"),
'receive_message': () => receiveMessage(arguments[1]),
'last_received_message': () => getLastReceivedMessageDetailsCCIP(arguments[1])
}
const method = functions[function_name]
if (!method) throw Error("Function not found!")
logger.info({ call_method: function_name })
console.log(`function selected: ${function_name}`)
method()
.then(console.log)
.catch(console.log)