curio-wrapper-truffle/scripts/wrap-and-transfer-set.js

59 lines
1.9 KiB
JavaScript

const Wrapper = artifacts.require("CurioERC1155Wrapper");
const IMastersFedi = artifacts.require("IMastersFedi");
const seq = n => n < 1 ? [] : [...seq(n - 1), n];
const to = process.argv[6];
const quantity = parseInt(process.argv[7]);
module.exports = async (callback) => {
if (/0x[0-9a-fA-F]{40}/.test(to) && quantity > 0) {
const wrapper = await Wrapper.deployed();
const [account,] = await web3.eth.getAccounts();
const ids = seq(9);
const quantities = Array(9).fill(quantity);
let approveSuccess = false;
console.log("Approving all contracts for transfer");
try {
for (let id = 1; id <= 9; ++id) {
const tokenContractAddress = await wrapper.contracts(id);
const tokenContract = await IMastersFedi.at(tokenContractAddress);
console.log(`Approving ID ${id}...`);
await tokenContract.approve(wrapper.address, quantity);
}
approveSuccess = true;
}
catch (e) {
console.error(`FAILED: ${e}`);
}
if (approveSuccess) {
console.log("Executing wrap transaction...");
let wrapResult;
try {
wrapResult = await wrapper.wrapBatch(
ids,
quantities
);
console.log(`Wrap transaction: ${wrapResult.tx}`);
}
catch (e) {
console.error(`FAILED: ${e}`);
}
if (wrapResult) {
console.log("Executing batch transfer...");
const transferResult = await wrapper.safeBatchTransferFrom(account, to, ids, quantities, []);
console.log(`Transfer transaction: ${transferResult.tx}`);
}
}
}
else {
console.error("Invalid to address or quantity");
}
callback();
}