Hi,
Often you need to call or transact with a specific contract function, for a maintenance task, changing of the address or top up a balance or such. An easy way to do this is just to load up node
/ ts-node
interpreter REPL loop and do it from there. Then import the needed accounts as private keys directly. OpenZeppelin SDK does a good on this, but often this approach is unknown or not considered for programmers who are not familiar working with interpreters.
Here is an example how to increase approve()
for a smart contract from ts-node
prompt.
Go to your OpenZeppelin SDK / Truffle project folder. Start ts-node
:
ts-node
Then, to paste in many lines of code, type in .editor
command on the ts-node prompt.
Then you can copy-paste in snippets and edit them in the REPL itself, and immediate execute them.
Here is an example of how to set up OpenZeppelin SDK, get a private key imported, load contract proxy classes and ABIs and make a transaction
import { ZWeb3, Contracts } from '@openzeppelin/upgrades';
import { Account } from 'eth-lib/lib';
import { createProvider } from './src/utils/deploy';
// Some inputs and configuration
const OWNER_PRIVATE_KEY = '...'; // No 0x prefix
const SWAP_BUDGET = '500000000000000000000000';
const INFURA_PROJECT_ID = '...';
async function run(): Promise<void> {
// Initialze
const provider = createProvider([OWNER_PRIVATE_KEY], INFURA_PROJECT_ID, 'ropsten');
ZWeb3.initialize(provider);
// Instiate contracts
const TokenFaucet = Contracts.getFromLocal('TokenFaucet');
const FirstBloodTokenMock = Contracts.getFromLocal('FirstBloodTokenMock');
const faucet = TokenFaucet.at('0xC5dec1bB818fbC753D8aFE3aC9275268F8204695');
const oldToken = FirstBloodTokenMock.at('0x9517FC874877A4510A3dE617d0c2517D29E5aD32');
console.log('Connected to', await ZWeb3.getNetworkName(), 'network');
const owner = Account.fromPrivate(`0x${OWNER_PRIVATE_KEY}`).address;
const promise = oldToken.methods.transfer(faucet.address, SWAP_BUDGET.toString()).send({ from: owner });
console.log('Sending transaction');
const receipt = await promise;
console.log('Transaction complete', receipt);
process.exit(0);
}
run();
This is even simpler with web3.py
and Python, but not that many people here are Python programmers. Hopefully await
keyword is supported on the top level soon, as it will make the example even more simpler.
Full code here: https://github.com/Dawn-Protocol/dawn-erc20-erc777#manipulating-contracts-in-a-console