Skip to content

Instantly share code, notes, and snippets.

@nCally
Last active June 27, 2021 18:59
Show Gist options
  • Select an option

  • Save nCally/28a2a6a701e94121d3d23e7a3c8ea25a to your computer and use it in GitHub Desktop.

Select an option

Save nCally/28a2a6a701e94121d3d23e7a3c8ea25a to your computer and use it in GitHub Desktop.
Line 30 and 31 - Example how to use the function
import createContract from "../create.contract";
import EsusuService from '../abis/EsusuService.json';
import DAITokenAbi from '../abis/DaiContract.json';
import sendSignedTransaction from '../../utils/sendSignedTransaction';
import esusuInfo from './info';
export default async function (
cycleId: number,
provider: string,
privateKey: string, addresses: Addresses) {
try {
const contract = await createContract(provider, EsusuService, addresses.ESUSU_SERVICE);
const tokenContract = await createContract(provider, DAITokenAbi, addresses.TOKEN);
// get the esusu with the cycleID passed to this function.
// the esusu cycle amount is needed to approve the transaction
const esusu_cycle = await esusuInfo(cycleId, provider, addresses);
// there has to be some way of granting permission for transaction
const approvalData = await tokenContract.methods.approve(addresses.ESUSU_ADAPTER, esusu_cycle.DepositAmount)
await sendSignedTransaction(privateKey, provider, approvalData, addresses.TOKEN)
const data = await contract.methods.JoinEsusu(cycleId)
const receipt = await sendSignedTransaction(privateKey, provider, data, addresses.ESUSU_SERVICE)
return {
status: true,
data: receipt
};
} catch (error) {
console.error(error);
return {
status: false,
data: error
}
}
}
import Web3 from 'web3';
import privateKeyToAddress from './privateKeyToAddress';
export default async (privateKey: string, provider: string, tx: any, contractAddress: string) => {
const web3 = new Web3(provider);
const client = privateKeyToAddress(provider, privateKey);
const networkId = await web3.eth.net.getId();
const gas = (await tx.estimateGas({ from: client }));
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI()
const nonce = await web3.eth.getTransactionCount(client)
const signedTx = await web3.eth.accounts.signTransaction({
to: contractAddress,
data,
gas, gasPrice, nonce, chainId: networkId
}, privateKey)
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
return receipt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment