Skip to content

Commit

Permalink
feat(javascripts): general message passing (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs47 committed May 30, 2022
1 parent ea58fee commit 2860b8e
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test-evm-versions": "bash test/test-evm-versions.bash",
"build": "rm -rf build && waffle waffle.json",
"lint": "solhint 'src/**/*.sol' && eslint 'test/**/*.js'",
"prettier": "prettier --write 'test/**/*.js' && prettier --write 'src/**/*.sol'",
"prettier": "prettier --write 'test/**/*.js' && prettier --write 'src/**/*.sol' && prettier --write 'scripts/**/*.js'",
"flatten": "rm -rf build/flattened && waffle flatten waffle.json"
},
"repository": {
Expand Down
103 changes: 103 additions & 0 deletions scripts/crosschain-token-execute.js
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);
});
106 changes: 106 additions & 0 deletions scripts/crosschain-token-transfer.js
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);
});
Loading

0 comments on commit 2860b8e

Please sign in to comment.