Skip to content

Commit

Permalink
Wallet updated
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Sep 18, 2019
1 parent 3da8d86 commit 19f95e8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
6 changes: 4 additions & 2 deletions js/franklin_lib/scripts/loadtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ async function main() {
let wallet = await Wallet.fromEthWallet(ethWallet);
// let full_exit_tx = await wallet.emergencyWithdraw({id: 0, address: ethers.constants.AddressZero});
// console.log(full_exit_tx);
let dep_tx = await wallet.deposit({id: 0, address: ''}, parseEther("0.2"));
console.log(dep_tx);
// let dep_tx = await wallet.deposit({id: 0, address: ''}, parseEther("0.2"));
// console.log(dep_tx);

await wallet.updateState();
console.log(wallet.supportedTokens);
console.log(wallet.franklinState);
console.log(wallet.ethState);
// let ethWallet2 = ethers.Wallet.fromMnemonic(process.env.MNEMONIC, "m/44'/60'/0'/0/2").connect(provider);
// let wallet2 = await Wallet.fromEthWallet(ethWallet2);
//
Expand Down
84 changes: 49 additions & 35 deletions js/franklin_lib/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ const IERC20Conract = require("../abi/IERC20");
const franklinContractCode = require("../abi/Franklin");

export type Address = Buffer;
export type AddressLike = Buffer | string;

export function toAddress(addressLike: AddressLike): Address {
if (typeof(addressLike) == "string") {
return Buffer.from(addressLike.substr(2),"hex");
} else {
return addressLike;
}
}


export class FranklinProvider {
Expand Down Expand Up @@ -89,7 +98,6 @@ export interface FranklinAccountState {
interface ETHAccountState {
onchainBalances: BigNumber[],
contractBalances: BigNumber[],
lockedBlocksLeft: number[],
}

export interface TransferTx {
Expand Down Expand Up @@ -226,42 +234,53 @@ export class Wallet {
}

async widthdrawOffchain(token: Token, amount: BigNumberish, fee: BigNumberish) {
let nonce = await this.getNonce();
let tx = {
type: 'Withdraw',
account: this.address,
eth_address: await this.ethWallet.getAddress(),
token: token.id,
amount: bigNumberify(amount).toString(),
fee: bigNumberify(fee).toString(),
nonce: nonce,
amount,
fee,
nonce: await this.getNonce(),
};
let signature = this.walletKeys.signWithdraw(tx);
let tx_req = FranklinProvider.prepareWithdrawRequestForNode(tx, signature);

return await this.provider.submitTx(tx);
return await this.provider.submitTx(tx_req);
}

async emergencyWithdraw(token: Token) {
const franklinDeployedContract = new Contract(this.provider.contractAddress, franklinContractCode.interface, this.ethWallet);
let signature = this.walletKeys.signFullExit({token: token.id, eth_address: await this.ethWallet.getAddress()})
let signature = this.walletKeys.signFullExit({token: token.id, eth_address: await this.ethWallet.getAddress()});
let tx = await franklinDeployedContract.fullExit(serializePointPacked(this.walletKeys.publicKey), token.address, signature,
{gasLimit: bigNumberify("500000"), value: parseEther("0.02")});
return tx.hash;
}

async transfer(address: Address, token: Token, amount: BigNumberish, fee: BigNumberish) {
let nonce = await this.getNonce();
// use packed numbers for signature
async transfer(to: AddressLike, token: Token, amount: BigNumberish, fee: BigNumberish) {
let tx = {
type: 'Transfer',
from: this.address,
to: address,
to: toAddress(to),
token: token.id,
amount: bigNumberify(amount).toString(),
fee: bigNumberify(fee).toString(),
nonce: nonce,
amount,
fee,
nonce: await this.getNonce(),
};
let signature = this.walletKeys.signTransfer(tx);
let tx_req = FranklinProvider.prepareTransferRequestForNode(tx, signature);

return await this.provider.submitTx(tx_req);
}

async close() {
let tx = {
account: this.address,
nonce: await this.getNonce()
};

return await this.provider.submitTx(tx);
let signature = this.walletKeys.signClose(tx);
let tx_req = FranklinProvider.prepareCloseRequestForNode(tx, signature);

return await this.provider.submitTx(tx_req);
}

async getNonce(): Promise<number> {
Expand All @@ -279,24 +298,19 @@ export class Wallet {
async fetchEthState() {
let onchainBalances = new Array<BigNumber>(this.supportedTokens.length);
let contractBalances = new Array<BigNumber>(this.supportedTokens.length);
let lockedBlocksLeft = new Array<number>(this.supportedTokens.length);

const currentBlock = await this.ethWallet.provider.getBlockNumber();

// const franklinDeployedContract = new Contract(this.provider.contractAddress, franklinContractCode.interface, this.ethWallet);
// for(let token of this.supportedTokens) {
// if (token.id == 0) {
// onchainBalances[token.id] = await this.ethWallet.provider.getBalance(this.ethAddress);
// } else {
// const erc20DeployedToken = new Contract(token.address, IERC20Conract.abi, this.ethWallet);
// onchainBalances[token.id] = await erc20DeployedToken.balanceOf(this.ethAddress).then(n => n.toString());
// }
// const balanceStorage = await franklinDeployedContract.balances(this.ethAddress, token.id);
// contractBalances[token.id] = balanceStorage.balance;
// lockedBlocksLeft[token.id] = Math.max(balanceStorage.lockedUntilBlock - currentBlock, 0);
// }

this.ethState = {onchainBalances, contractBalances, lockedBlocksLeft};

const franklinDeployedContract = new Contract(this.provider.contractAddress, franklinContractCode.interface, this.ethWallet);
for(let token of this.supportedTokens) {
if (token.id == 0) {
onchainBalances[token.id] = await this.ethWallet.provider.getBalance(this.ethAddress);
} else {
const erc20DeployedToken = new Contract(token.address, IERC20Conract.abi, this.ethWallet);
onchainBalances[token.id] = await erc20DeployedToken.balanceOf(this.ethAddress).then(n => n.toString());
}
contractBalances[token.id] = await franklinDeployedContract.balancesToWithdraw(this.ethAddress, token.id);
}

this.ethState = {onchainBalances, contractBalances};
}

async fetchFranklinState() {
Expand Down

0 comments on commit 19f95e8

Please sign in to comment.