-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascripts): general message passing (#103)
- Loading branch information
Showing
8 changed files
with
313 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
'use strict'; | ||
|
||
require('dotenv').config(); | ||
|
||
const { | ||
Contract, | ||
Wallet, | ||
utils, | ||
providers: { JsonRpcProvider }, | ||
} = require('ethers'); | ||
|
||
const { printLog, printObj } = require('./logging'); | ||
|
||
const { join, resolve } = require('node:path'); | ||
|
||
const { existsSync } = require('node:fs'); | ||
|
||
// these environment variables should be defined in an '.env' file | ||
const contractsPath = resolve(process.env.CONTRACTS_PATH || './build'); | ||
const url = process.env.URL; | ||
const privKey = process.env.PRIVATE_KEY; | ||
const sourceChain = process.env.SOURCE_CHAIN; | ||
const commandIDhex = process.env.COMMAND_ID; | ||
const symbol = process.env.SYMBOL; | ||
const amount = process.env.AMOUNT; | ||
const gatewayAddress = process.env.GATEWAY_ADDRESS; | ||
|
||
printObj({ | ||
'environment_variables:': { | ||
CONTRACTS_PATH: contractsPath || null, | ||
URL: url || null, | ||
PRIVATE_KEY: privKey || null, | ||
SOURCE_CHAIN: sourceChain || null, | ||
COMMAND_ID: commandIDhex || null, | ||
SYMBOL: symbol || null, | ||
AMOUNT: amount || null, | ||
GATEWAY_ADDRESS: gatewayAddress || null, | ||
}, | ||
}); | ||
|
||
if ( | ||
!( | ||
url && | ||
privKey && | ||
sourceChain && | ||
commandIDhex && | ||
symbol && | ||
amount && | ||
gatewayAddress | ||
) | ||
) { | ||
console.error( | ||
`One or more of the required environment variable not defined. Make sure to declare these variables in an .env file.`, | ||
); | ||
process.exit(1); | ||
} | ||
|
||
// the ABIs for the contracts below must be manually downloaded/compiled | ||
const IAxelarGatewayPath = join(contractsPath, 'IAxelarGateway.json'); | ||
|
||
if (!existsSync(IAxelarGatewayPath)) { | ||
console.error( | ||
`Missing IAxelarGateway ABI. Make sure IAxelarGateway.json is present in ${contractsPath}`, | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const IAxelarGateway = require(IAxelarGatewayPath); | ||
|
||
const provider = new JsonRpcProvider(url); | ||
const wallet = new Wallet(privKey, provider); | ||
const gateway = new Contract(gatewayAddress, IAxelarGateway.abi, wallet); | ||
|
||
const hash = utils.keccak256(utils.arrayify(Buffer.from([]))); | ||
const commandID = utils.arrayify( | ||
commandIDhex.startsWith('0x') ? commandIDhex : '0x' + commandIDhex, | ||
); | ||
|
||
printLog( | ||
`validating contract call with token for chain ${sourceChain} and destination address ${wallet.address}`, | ||
); | ||
|
||
gateway | ||
.validateContractCallAndMint( | ||
commandID, | ||
sourceChain, | ||
wallet.address, | ||
hash, | ||
symbol, | ||
amount, | ||
) | ||
.then(async (tx) => { | ||
await tx.wait(); | ||
printLog( | ||
`successfully validated contract call with token for chain ${sourceChain} and destination address ${wallet.address} at tx ${tx.hash}`, | ||
); | ||
printObj({ validated: tx.hash }); | ||
process.exit(0); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
'use strict'; | ||
|
||
require('dotenv').config(); | ||
|
||
const { | ||
Contract, | ||
Wallet, | ||
providers: { JsonRpcProvider }, | ||
} = require('ethers'); | ||
|
||
const { printLog, printObj } = require('./logging'); | ||
|
||
const { join, resolve } = require('node:path'); | ||
|
||
const { existsSync } = require('node:fs'); | ||
|
||
// these environment variables should be defined in an '.env' file | ||
const contractsPath = resolve(process.env.CONTRACTS_PATH || './build'); | ||
const url = process.env.URL; | ||
const privKey = process.env.PRIVATE_KEY; | ||
const destinationChain = process.env.DESTINATION_CHAIN; | ||
const symbol = process.env.SYMBOL; | ||
const amount = process.env.AMOUNT; | ||
const gatewayAddress = process.env.GATEWAY_ADDRESS; | ||
|
||
printObj({ | ||
'environment_variables:': { | ||
CONTRACTS_PATH: contractsPath || null, | ||
URL: url || null, | ||
PRIVATE_KEY: privKey || null, | ||
DESTINATION_CHAIN: destinationChain || null, | ||
SYMBOL: symbol || null, | ||
AMOUNT: amount || null, | ||
GATEWAY_ADDRESS: gatewayAddress || null, | ||
}, | ||
}); | ||
|
||
if ( | ||
!(url && privKey && destinationChain && symbol && amount && gatewayAddress) | ||
) { | ||
console.error( | ||
`One or more of the required environment variable not defined. Make sure to declare these variables in an .env file.`, | ||
); | ||
process.exit(1); | ||
} | ||
|
||
// the ABIs for the contracts below must be manually downloaded/compiled | ||
const IAxelarGatewayPath = join(contractsPath, 'IAxelarGateway.json'); | ||
const IERC20Path = join(contractsPath, 'IERC20.json'); | ||
|
||
if (!(existsSync(IAxelarGatewayPath) && existsSync(IERC20Path))) { | ||
console.error( | ||
`Missing one or more ABIs/bytecodes. Make sure IAxelarGateway.json and IERC20.json are present in ${contractsPath}`, | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const IAxelarGateway = require(IAxelarGatewayPath); | ||
const IERC20 = require(IERC20Path); | ||
|
||
const provider = new JsonRpcProvider(url); | ||
const wallet = new Wallet(privKey, provider); | ||
const gateway = new Contract(gatewayAddress, IAxelarGateway.abi, wallet); | ||
const payload = Buffer.from([]); | ||
let transactions = {}; | ||
|
||
printLog(`approving amount of ${amount}${symbol}`); | ||
|
||
gateway | ||
.tokenAddresses(symbol) | ||
.then((tokenAddress) => { | ||
const token = new Contract(tokenAddress, IERC20.abi, wallet); | ||
return token.approve(gatewayAddress, amount); | ||
}) | ||
.then(async (tx) => { | ||
await tx.wait(); | ||
printLog( | ||
`successfully approved amount of ${amount}${symbol} at tx ${tx.hash}`, | ||
); | ||
printLog( | ||
`calling contract with token for chain ${destinationChain} and destination address ${wallet.address}`, | ||
); | ||
transactions.approve = tx.hash; | ||
}) | ||
.then(() => | ||
gateway.callContractWithToken( | ||
destinationChain, | ||
wallet.address, | ||
payload, | ||
symbol, | ||
amount, | ||
), | ||
) | ||
.then(async (tx) => { | ||
await tx.wait(); | ||
printLog( | ||
`successfully called contract with token for chain ${destinationChain} and destination address ${wallet.address} at tx ${tx.hash}`, | ||
); | ||
transactions.mint = tx.hash; | ||
printObj(transactions); | ||
process.exit(0); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.