-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathevm-send.ts
More file actions
143 lines (128 loc) · 4.15 KB
/
Copy pathevm-send.ts
File metadata and controls
143 lines (128 loc) · 4.15 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
import { INewOperation } from '@layerzerolabs/devtools-extensible-cli'
import { createEvmOmniContracts, readPrivateKey } from '../../tasks/evm/wire-evm'
import { ethers } from 'ethers'
class EVMSendOperation implements INewOperation {
vm = 'evm'
operation = 'send'
description = 'Sends an OFT message'
reqArgs = ['oapp_config', 'src_eid', 'dst_eid', 'to', 'amount', 'min_amount']
addArgs = [
{
name: '--src-eid',
arg: {
help: 'The source endpoint ID',
required: false,
},
},
{
name: '--dst-eid',
arg: {
help: 'The destination endpoint ID',
required: false,
},
},
{
name: '--to',
arg: {
help: 'The address to send the message to',
required: false,
},
},
{
name: '--amount',
arg: {
help: 'The amount to send',
required: false,
},
},
{
name: '--min-amount',
arg: {
help: 'The minimum amount to send',
required: false,
},
},
{
name: '--refund-address',
arg: {
help: 'The address to refund the gas fee to',
required: false,
},
},
{
name: '--native-token',
arg: {
help: 'The request is trying to send native token (true/false)',
required: false,
},
},
]
async impl(args: any): Promise<void> {
await sendOFT(args)
}
}
const NewOperation = new EVMSendOperation()
export { NewOperation }
async function sendOFT(args: any): Promise<MessagingFee> {
const srcEid = args.src_eid
const dstEid = args.dst_eid
const to = ethers.utils.hexZeroPad(args.to, 32)
const amount = args.amount
const minAmount = args.min_amount
const sendingNativeToken = args.native_token ? true : false
const privateKey = readPrivateKey(args)
const omniContracts = await createEvmOmniContracts(args, privateKey)
let oft: ethers.Contract
const contract = omniContracts[srcEid.toString()]
if (contract?.contract?.oapp) {
oft = contract.contract.oapp
} else {
throw new Error(`No OApp found for endpoint ID ${srcEid}`)
}
const refundAddress = args.refund_address || (await oft.signer.getAddress())
console.log(`\n🚀 Sending ${amount} units`)
console.log(`\t📝 Using OFT at address: ${oft.address}`)
console.log(`\t👤 From account: ${await oft.signer.getAddress()}`)
console.log(`\t🎯 To account: ${to}`)
console.log(`\t🌐 dstEid: ${dstEid}`)
console.log(`\t🔍 Min amount: ${minAmount}`)
const sendParam: SendParam = {
dstEid: dstEid,
to: to,
amountLD: amount,
minAmountLD: minAmount,
extraOptions: '0x',
composeMsg: '0x',
oftCmd: '0x',
}
const fee: MessagingFee = await oft.quoteSend(sendParam, false)
console.log('\n💰 Quote received:')
console.log('\t🏦 Native fee:', fee.nativeFee.toString())
console.log('\t🪙 LZ token fee:', fee.lzTokenFee.toString())
const value = sendingNativeToken ? BigInt(fee.nativeFee) + BigInt(amount) : BigInt(fee.nativeFee)
console.log('\n💸 Sending message with total value:', value)
const tx = await oft.send(sendParam, fee, refundAddress, {
value: value,
})
console.log('\n📨 Transaction sent:')
console.log('\t🔑 Hash:', tx.hash)
console.log('\t🔍 LayerZero Explorer:', `https://layerzeroscan.com/tx/${tx.hash}`)
console.log('\t📤 From:', tx.from)
console.log('\t📥 To:', tx.to)
console.log('\t💵 Value:', ethers.utils.formatEther(tx.value), 'ETH')
console.log('\t⛽ Gas limit:', tx.gasLimit.toString())
return tx
}
type SendParam = {
dstEid: number
to: string
amountLD: number
minAmountLD: number
extraOptions: string
composeMsg: string
oftCmd: string
}
type MessagingFee = {
nativeFee: bigint
lzTokenFee: bigint
}