-
Notifications
You must be signed in to change notification settings - Fork 3
/
withdrawSaiMain.mjs
56 lines (48 loc) · 1.84 KB
/
withdrawSaiMain.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Web3 from "web3";
import EthTx from "ethereumjs-tx";
import cSaiContract from './cSaiContract';
// set up Web3 to use Infura as your web3 provider
const web3 = new Web3(
new Web3.providers.HttpProvider(
"https://mainnet.infura.io/v3/[PROJECT_ID]"
)
);
// declare const variables for your address and private key
const addressFrom = "[YOUR_ADDRESS]";
const privKey = "[YOUR_PRIVATE_KEY]";
// instantiate the cSai contract
const cSaiContractInstance = new web3.eth.Contract(
JSON.parse(cSaiContract.cSaiContractAbi),
cSaiContract.cSaiContractAddress
);
// declare a const variable to pass to the redeemUnderlying function of the cSai contract
const REDEEM_AMOUNT = web3.utils.toHex(1 * 10 ** 18);
// create the encoded abi of the redeemUnderlying function
const redeemUnderlyingEncodedABI = cSaiContractInstance.methods
.redeemUnderlying(REDEEM_AMOUNT)
.encodeABI();
// declare the function to sign a transaction object and send it to the Ethereum network.
function sendSignedTx(transactionObject, cb) {
let transaction = new EthTx(transactionObject);
const privateKey = new Buffer.from(privKey, "hex");
transaction.sign(privateKey);
const serializedEthTx = transaction.serialize().toString("hex");
web3.eth.sendSignedTransaction(`0x${serializedEthTx}`, cb);
}
// construct a transaction object and invoke the sendSignedTx function
web3.eth.getTransactionCount(addressFrom).then(transactionNonce => {
const transactionObject = {
chainId: 1,
nonce: web3.utils.toHex(transactionNonce),
gasLimit: web3.utils.toHex(400000),
gasPrice: web3.utils.toHex(5000000000),
to: cSaiContract.cSaiContractAddress,
from: addressFrom,
data: redeemUnderlyingEncodedABI
};
sendSignedTx(transactionObject, function(error, result){
if(error) return console.log("error ===>", error);
console.log("sent ===>", result);
})
}
);