azimuth-cli
Version:
Urbit Bridge for the command line
71 lines (52 loc) • 2.31 kB
JavaScript
const ob = require('urbit-ob')
const ajs = require('azimuth-js')
const _ = require('lodash')
const {files, validate, eth, findPoints} = require('../../utils')
exports.command = 'transfer'
exports.desc = 'Transfer one or more points, either to the wallet address or to the provided target addess.'
exports.builder = function(yargs) {
yargs.option('reset-network-key',{
describe: 'If the network key should be reset in the process of the transfer. Do not set to true when moving to a HD wallet address.',
default: false,
type: 'boolean',
});
}
exports.handler = async function (argv)
{
const workDir = files.ensureWorkDir(argv.workDir);
const privateKey = await eth.getPrivateKey(argv);
const ctx = await eth.createContext(argv);
const ethAccount = eth.getAccount(ctx.web3, privateKey);
const wallets = argv.useWalletFiles ? findPoints.getWallets(workDir) : null;
const points = findPoints.getPoints(argv, workDir, wallets);
console.log(`Will transfer ${points.length} points`);
for (const p of points)
{
let patp = ob.patp(p);
console.log(`Trying to transfer ${patp} (${p}).`);
let wallet = argv.useWalletFiles ? wallets[patp] : null;
let targetAddress =
argv.address != undefined
? argv.address
: argv.useWalletFiles
? wallet.ownership.keys.address :
null; //fail
targetAddress = validate.address(targetAddress, true);
//TODO: for some reason isOwner always returns true, even if an address is passed which p is not owner of.
// But this is fine for now, because we have the second check: canTransferPoint
// let isOwner = ajs.azimuth.isOwner(ctx.contracts, p, targetAddress);
// if(isOwner){
// console.log(`Target address ${targetAddress} is already owner of ${patp}.`);
// continue;
// }
var res = await ajs.check.canTransferPoint(ctx.contracts, p, ethAccount.address, targetAddress);
if(!res.result){
console.log(`Cannot transfer ${patp}: ${res.reason}`);
continue;
}
//create and send tx
let tx = ajs.ecliptic.transferPoint(ctx.contracts, p, targetAddress, argv.resetNetworkKey);
await eth.setGasSignSendAndSaveTransaction(ctx, tx, privateKey, argv, workDir, patp, 'transfer');
} //end for each point
process.exit(0);
};