-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.ts
More file actions
64 lines (56 loc) · 2.47 KB
/
send.ts
File metadata and controls
64 lines (56 loc) · 2.47 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
import bs58 from 'bs58'
import { BigNumber } from 'ethers'
import { task, types } from 'hardhat/config'
import { ActionType, HardhatRuntimeEnvironment } from 'hardhat/types'
import { makeBytes32 } from '@layerzerolabs/devtools'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { getLayerZeroScanLink } from '../solana'
interface TaskArguments {
dstEid: number
amount: string
to: string
contractName: string
erc20Address: string
}
const action: ActionType<TaskArguments> = async (
{ dstEid, amount, to, contractName, erc20Address },
hre: HardhatRuntimeEnvironment
) => {
const signer = await hre.ethers.getNamedSigner('deployer')
// @ts-ignore
const token = (await hre.ethers.getContract(contractName)).connect(signer)
// @ts-ignore
const erc20Token = (await hre.ethers.getContractAt('IERC20', erc20Address)).connect(signer)
const allowance = await erc20Token.allowance(signer.address, token.address)
if (allowance < amount) {
const approvalTxResponse = await erc20Token.approve(token.address, amount)
const approvalTxReceipt = await approvalTxResponse.wait()
console.log(`approve: ${amount}: ${approvalTxReceipt.transactionHash}`)
}
const amountLD = BigNumber.from(amount)
const sendParam = {
dstEid,
to: makeBytes32(bs58.decode(to)),
amountLD: amountLD.toString(),
minAmountLD: amountLD.mul(9_000).div(10_000).toString(),
extraOptions: '0x',
composeMsg: '0x',
oftCmd: '0x',
}
const [msgFee] = await token.functions.quoteSend(sendParam, false)
const txResponse = await token.functions.send(sendParam, msgFee, signer.address, {
value: msgFee.nativeFee,
gasLimit: 500_000,
})
const txReceipt = await txResponse.wait()
console.log(`send: ${amount} to ${to}: ${txReceipt.transactionHash}`)
console.log(
`Track cross-chain transfer here: ${getLayerZeroScanLink(txReceipt.transactionHash, dstEid == EndpointId.SOLANA_V2_TESTNET)}`
)
}
task('send', 'Sends a transaction', action)
.addParam('dstEid', 'Destination endpoint ID', undefined, types.int, false)
.addParam('amount', 'Amount to send in wei', undefined, types.string, false)
.addParam('to', 'Recipient address', undefined, types.string, false)
.addParam('erc20Address', 'ERC20 address', undefined, types.string, false)
.addOptionalParam('contractName', 'Name of the contract in deployments folder', 'VirtualOFTAdapter', types.string)