Skip to content

Commit

Permalink
sendEvmMsgs refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marxeille committed Jul 5, 2024
1 parent f0d0ecc commit de04ceb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 84 deletions.
16 changes: 7 additions & 9 deletions packages/mobile/src/components/input/currency-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ export const CurrencySelector: FunctionComponent<{
}

useEffect(() => {
InteractionManager.runAfterInteractions(() => {
const queryBalances = queriesStore
.get(chainId)
.queryBalances.getQueryBech32Address(addressToFetch);
const tokens = queryBalances.balances;
const displayTokens = sortTokens(filterTokens(tokens));

setDisplayTokens(displayTokens);
});
const queryBalances = queriesStore
.get(chainId)
.queryBalances.getQueryBech32Address(addressToFetch);
const tokens = queryBalances.balances;
const displayTokens = sortTokens(filterTokens(tokens));

setDisplayTokens(displayTokens);
}, [chainId, addressToFetch]);

const selectedKey = amountConfig.sendCurrency.coinMinimalDenom;
Expand Down
144 changes: 69 additions & 75 deletions packages/stores/src/account/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,80 +673,40 @@ export class AccountSetBase<MsgOpts, Queries> {
}
}

async sendEvmMsgs(
type: string | "unknown",
msgs: Msg,
fee: StdFeeEthereum,
signOptions?: OWalletSignOptions,
onTxEvents?:
| ((tx: any) => void)
| {
onBroadcastFailed?: (e?: Error) => void;
onBroadcasted?: (txHash: Uint8Array) => void;
onFulfill?: (tx: any) => void;
}
) {
runInAction(() => {
this._isSendingMsg = type;
async handleErc20Transaction(msgs, fee) {
const { value } = msgs;
const provider = this.chainGetter.getChain(this.chainId).rpc;
const web3 = new Web3(provider);
const contract = new web3.eth.Contract(ERC20_ABI, value.contract_addr, {
from: value.from,
});
const data = contract.methods
.transfer(value.recipient, value.amount)
.encodeABI();

const txObj = {
gas: web3.utils.toHex(value.gas),
to: value.contract_addr,
value: "0x0",
from: value.from,
data,
};

let txHash: string;

try {
if (msgs.type === "erc20") {
const { value } = msgs;
const provider = this.chainGetter.getChain(this.chainId).rpc;
const web3 = new Web3(provider);
const contract = new web3.eth.Contract(
// @ts-ignore
ERC20_ABI,
value.contract_addr,
{ from: value.from }
);
const data = contract.methods
.transfer(value.recipient, value.amount)
.encodeABI();

const txObj = {
gas: web3.utils.toHex(value.gas),
to: value.contract_addr,
value: "0x0", // Must be 0x0, maybe this field is not in use while send erc20 tokens, but still need
from: value.from,
data,
};

const result = await this.broadcastErc20EvmMsgs(txObj, fee);

txHash = result.txHash;
} else {
const result = await this.broadcastEvmMsgs(msgs, fee, signOptions);
txHash = result.txHash;
}
if (!txHash) throw Error("Transaction Rejected");
} catch (e: any) {
runInAction(() => {
this._isSendingMsg = false;
});

if (this.opts.preTxEvents?.onBroadcastFailed) {
this.opts.preTxEvents.onBroadcastFailed(e);
}

if (
onTxEvents &&
"onBroadcastFailed" in onTxEvents &&
onTxEvents.onBroadcastFailed
) {
onTxEvents.onBroadcastFailed(e);
}
return await this.broadcastErc20EvmMsgs(txObj, fee);
}

throw e;
async handleEvmTransaction(msgs, fee, signOptions) {
if (msgs.type === "erc20") {
return await this.handleErc20Transaction(msgs, fee);
} else {
return await this.broadcastEvmMsgs(msgs, fee, signOptions);
}
}

let onBroadcasted: ((txHash: Uint8Array) => void) | undefined;
handleTxEvents(txHash, onTxEvents) {
let onBroadcasted;
let onFulfill;

let onFulfill: ((tx: any) => void) | undefined;
console.log(txHash, "result result");
if (onTxEvents) {
if (typeof onTxEvents === "function") {
onFulfill = onTxEvents;
Expand All @@ -756,17 +716,11 @@ export class AccountSetBase<MsgOpts, Queries> {
}
}

const rpc = this.chainGetter.getChain(this.chainId).rest;

runInAction(() => {
this._isSendingMsg = false;
});
if (this.opts.preTxEvents?.onBroadcasted) {
//@ts-ignore
this.opts.preTxEvents.onBroadcasted(txHash);
}

if (onBroadcasted) {
//@ts-ignore
onBroadcasted(txHash);
}

Expand All @@ -782,9 +736,49 @@ export class AccountSetBase<MsgOpts, Queries> {
return;
}

const rpc = this.chainGetter.getChain(this.chainId).rest;
this.waitForPendingTransaction(rpc, txHash, onFulfill);
}

async sendEvmMsgs(type, msgs, fee, signOptions, onTxEvents) {
runInAction(() => {
this._isSendingMsg = type;
});

let txHash;

try {
const result = await this.handleEvmTransaction(msgs, fee, signOptions);
txHash = result.txHash;

if (!txHash) throw Error("Transaction Rejected");
} catch (e) {
runInAction(() => {
this._isSendingMsg = false;
});

if (this.opts.preTxEvents?.onBroadcastFailed) {
this.opts.preTxEvents.onBroadcastFailed(e);
}

if (
onTxEvents &&
"onBroadcastFailed" in onTxEvents &&
onTxEvents.onBroadcastFailed
) {
onTxEvents.onBroadcastFailed(e);
}

throw e;
}

runInAction(() => {
this._isSendingMsg = false;
});

this.handleTxEvents(txHash, onTxEvents);
}

async sendBtcMsgs(
type: string | "unknown",
msgs: any,
Expand Down

0 comments on commit de04ceb

Please sign in to comment.