Commit 286c7526 authored by XFT-dev's avatar XFT-dev
Browse files

adding scripts

parent 9313cadd
# Shifting frontend
website directory: browser based frontend (requires metamask for interaction).
scripts directory: scripts for interacting with the contracts.
\ No newline at end of file
const Web3 = require("web3");
const fs = require('fs');
const readline = require("readline");
let web3 = new Web3(Web3.givenProvider || "ws://localhost:8545")
let shift_abi = fs.readFileSync('shift-abi.json');
let shift = new web3.eth.Contract(JSON.parse(shift_abi), "0x6bE4C94052BF34e09d67C255D9Fe564c5abd4f95");
let token_abi = fs.readFileSync('token-abi.json');
let tokenA = new web3.eth.Contract(JSON.parse(token_abi), "0x1190fb1B00D20656549cdFF689E55D072BA764f3");
let tokenB = new web3.eth.Contract(JSON.parse(token_abi), "0x0e1BFf82509994170026f47f1d0857fB275c3003");
web3.eth.accounts.wallet.add(web3.eth.accounts.privateKeyToAccount("0x0"));
async function approve(num) {
let amount = web3.utils.toWei(parseFloat(num).toString(), 'ether');
const gasPrice = await web3.eth.getGasPrice();
const gasEstimate = await tokenA.methods.approve(shift._address, amount).estimateGas({ from: web3.eth.accounts.wallet[0].address });
if (amount => 0) {
let approvalAmount;
tokenA.methods.allowance(web3.eth.accounts.wallet[0].address, shift._address).call({ from: web3.eth.accounts.wallet[0].address }).then(data => approvalAmount = data);
if (approvalAmount >= amount) {
console.log("already approved");
} else {
tokenA.methods.approve(shift._address, amount).send({ from: web3.eth.accounts.wallet[0].address, gasPrice: gasPrice, gas: gasEstimate }).then(console.log);
}
} else {
console.log("input a number");
}
await sleep(20000);
}
async function shifter(num) {
let A_aggregator = "0xECe365B379E1dD183B20fc5f022230C044d51404";
let B_aggregator = "0xab4a352ac35dFE83221220D967Db41ee61A0DeFa";
let amount = web3.utils.toWei(parseFloat(num).toString(), 'ether');
const gasPrice = await web3.eth.getGasPrice();
const gasEstimate = await shift.methods.shift(amount, A_aggregator, B_aggregator).estimateGas({ from: web3.eth.accounts.wallet[0].address });
if (amount > 0) {
shift.methods.shift(amount, A_aggregator, B_aggregator).send({ from: web3.eth.accounts.wallet[0].address, gasPrice: gasPrice, gas: gasEstimate }).then(console.log);
}
}
async function faucet() {
const gasPrice = await web3.eth.getGasPrice();
const gasEstimate = await shift.methods.faucet().estimateGas({ from: web3.eth.accounts.wallet[0].address });
shift.methods.faucet().send({ from: web3.eth.accounts.wallet[0].address, gasPrice: gasPrice, gas: gasEstimate }).then(console.log);
await sleep(6000);
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function handle_shift(n) {
await approve(n);
await shifter(n);
}
async function handle_args(arg) {
switch (arg) {
case "0":
await faucet();
break;
case "1":
rl.question("Amount of tokens to shift: ", function (given_amount) {
handle_shift(given_amount);
});
break;
case "2":
await tokenA.methods.balanceOf(web3.eth.accounts.wallet[0].address).call().then(t => console.log(`current xftTM ${t/1e18}`));
break;
case "3":
await tokenB.methods.balanceOf(web3.eth.accounts.wallet[0].address).call().then(t => console.log(`current xftTB ${t/1e18}`));
break;
}
rl.close();
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log(`
Request funds from the faucet -> 0
shift tokens -> 1
get xftTM balance -> 2
get xftTB balance -> 3
EXIT -> 4
`);
rl.question("Please select an option: ", function (option) {
switch (option) {
case "4":
rl.close();
default:
handle_args(option);
}
});
rl.on("close", function () {
console.log("\nBYE -_-");
process.exit(0);
});
{
"name": "script",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"web3": "^1.3.5"
}
}
[
{
"inputs": [
{
"internalType": "address",
"name": "_tokenA",
"type": "address"
},
{
"internalType": "address",
"name": "_tokenB",
"type": "address"
},
{
"internalType": "address",
"name": "_chainlink",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Shifted",
"type": "event"
},
{
"inputs": [],
"name": "faucet",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "address",
"name": "_A",
"type": "address"
},
{
"internalType": "address",
"name": "_B",
"type": "address"
}
],
"name": "shift",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
\ No newline at end of file
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "Paused",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "bytes32",
"name": "role",
"type": "bytes32"
},
{
"indexed": true,
"internalType": "bytes32",
"name": "previousAdminRole",
"type": "bytes32"
},
{
"indexed": true,
"internalType": "bytes32",
"name": "newAdminRole",
"type": "bytes32"
}
],
"name": "RoleAdminChanged",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "bytes32",
"name": "role",
"type": "bytes32"
},
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "sender",
"type": "address"
}
],
"name": "RoleGranted",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "bytes32",
"name": "role",
"type": "bytes32"
},
{
"indexed": true,
"internalType": "address",
"name": "account",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "sender",
"type": "address"
}
],
"name": "RoleRevoked",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "Unpaused",
"type": "event"
},
{
"inputs": [],
"name": "BURNER_ROLE",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "DEFAULT_ADMIN_ROLE",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MINTER_ROLE",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "PAUSER_ROLE",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "burn",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "from",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "burn",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "burnFrom",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "decimals",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "subtractedValue",
"type": "uint256"
}
],
"name": "decreaseAllowance",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "role",
"type": "bytes32"
}
],
"name": "getRoleAdmin",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "role",
"type": "bytes32"
},
{
"internalType": "address",
"name": "account",
"type": "address"