-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorigin.js
133 lines (108 loc) · 4.15 KB
/
origin.js
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
require("dotenv").config()
const logger = require("./logger")
// instance cryptum-sdk
const CryptumSDK = require('cryptum-sdk');
//
const ENVIROMENT = process.env.ENVIROMENT // testnet
const MNEMONIC = process.env.MNEMONIC; //
const API_KEY = process.env.API_KEY; //
const PROTOCOL = process.env.PROTOCOL_ORIGIN; // ETHEREUM
//
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const sdk = new CryptumSDK({
environment: ENVIROMENT, // 'testnet' or 'mainnet'
apiKey: API_KEY,
})
// Generte 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 sendMessage = async (senderContractAddress, receiveContractAddress, data, payLink) => {
if (!senderContractAddress || !receiveContractAddress || !data || !payLink) throw Error("Arguments senderContractAddress, receiveContractAddress, data, payLink required!")
const wallet = await getWallet()
const { hash } = await sdk.chainlink.sendMessageCCIP({
wallet,
protocol: PROTOCOL,
destinationProtocol: process.env.PROTOCOL_DESTINATION,
payLink,
text: data,
contractAddress: senderContractAddress,
to: receiveContractAddress,
tokenAddress: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238', // USDC
amount: '5'
})
logger.info({ hash, call_method: function_name, senderContractAddress, receiveContractAddress, data, payLink, protocol: PROTOCOL, destinationProtocol: process.env.PROTOCOL_DESTINATION })
console.log(`Message sended, follow the status via the link: https://ccip.chain.link/`)
console.log(`Provide the hash: ${hash}`)
return hash
}
const getLastSendedMessageDetailsCCIP = async (contractAddress) => {
return await sdk.chainlink.getLastSendMessageDetailsCCIP({
protocol: PROTOCOL,
address: contractAddress
})
}
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]),
'send_message': () => sendMessage(arguments[1], arguments[2], arguments[3], arguments[4] == "true"),
'last_message' : () => getLastSendedMessageDetailsCCIP(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)