/
ActionSendWeb3.vue
185 lines (174 loc) · 6.24 KB
/
ActionSendWeb3.vue
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<script>
import {validationMixin} from 'vuelidate/src/index.js';
import required from 'vuelidate/src/validators/required';
import minLength from 'vuelidate/src/validators/minLength';
import maxLength from 'vuelidate/src/validators/maxLength';
import minValue from 'vuelidate/src/validators/minValue.js';
import maxValue from 'vuelidate/src/validators/maxValue.js';
import Big from 'minterjs-util/src/big.js';
import {buildTransferTx, toErcDecimals} from 'minter-js-web3-sdk/src/web3.js';
import {pretty} from '~/assets/utils.js';
import {HUB_NETWORK_SLUG, HUB_CHAIN_DATA} from '~/assets/variables.js';
import useHubOracle from '~/composables/use-hub-oracle.js';
import useHubToken from '~/composables/use-hub-token.js';
import useWeb3SmartWalletWithRelayReward from 'minter-js-web3-sdk/src/composables/use-web3-smartwallet-relay-reward.js';
import FieldAddress from '~/components/base/FieldAddress.vue';
export default {
components: {
FieldAddress,
},
mixins: [validationMixin],
emits: [
// 'success',
// 'success-modal-close',
// 'override-stats-value',
],
props: {
action: {
type: Object,
},
params: {
type: Object,
default: () => ({}),
},
},
setup() {
const {networkGasPrice, setHubOracleProps} = useHubOracle({
subscribePriceList: true,
});
const {tokenContractAddressFixNative: tokenContractAddress, tokenDecimals, hubCoin, tokenData, setHubTokenProps} = useHubToken();
const {smartWalletAddress, setSmartWalletProps, buildTxForRelayReward, callSmartWallet} = useWeb3SmartWalletWithRelayReward();
return {
networkGasPrice,
setHubOracleProps,
tokenContractAddress,
tokenDecimals,
setHubTokenProps,
hubCoin, tokenData,
smartWalletAddress,
setSmartWalletProps,
buildTxForRelayReward,
callSmartWallet,
};
},
data() {
return {
form: {
token: '',
amount: '',
address: '',
},
};
},
computed: {
/** @type {HubChainDataItem} */
hubChainData() {
return HUB_CHAIN_DATA[HUB_NETWORK_SLUG.BSC];
},
isTokenDecimalsFetched() {
return this.tokenDecimals > 0;
},
},
created() {
// smartWalletProps
this.$watch(
() => ({
privateKey: this.$store.getters.privateKey,
evmAccountAddress: this.$store.getters.evmAddress,
chainId: this.hubChainData.chainId,
gasPriceGwei: this.networkGasPrice,
gasTokenAddress: this.tokenContractAddress,
gasTokenDecimals: this.tokenDecimals,
estimationSkip: true,
}),
(newVal) => this.setSmartWalletProps(newVal),
{deep: true, immediate: true},
);
// hubOracleProps
this.$watch(
() => ({
hubNetworkSlug: this.hubChainData.hubNetworkSlug,
fixInvalidGasPriceWithDummy: false,
}),
(newVal) => this.setHubOracleProps(newVal),
{deep: true, immediate: true},
);
// hubTokenProps
this.$watch(
() => ({
chainId: this.hubChainData.chainId,
tokenSymbol: this.form.token.indexOf('0x') === 0 ? '' : this.form.token,
tokenAddress: this.form.token.indexOf('0x') === 0 ? this.form.token : '',
}),
(newVal) => this.setHubTokenProps(newVal),
{deep: true, immediate: true},
);
},
methods: {
pretty,
async submit() {
if (!this.isTokenDecimalsFetched) {
return;
}
const relayRewardData = await this.buildTxForRelayReward();
const amount = new Big(this.form.amount).minus(relayRewardData.swapLimit).toString();
if (amount <= 0) {
throw new Error('Not enough amount to pay tx fee');
}
let tx;
if (this.form.token === 'BNB') {
tx = {
to: this.form.address,
value: toErcDecimals(amount, this.tokenDecimals),
data: '0x',
};
} else {
tx = buildTransferTx(this.form.token, this.form.address, toErcDecimals(amount, this.tokenDecimals));
}
this.callSmartWallet([].concat(relayRewardData.txList, tx))
.then((result) => {
window.alert(`https://explorer.minter.network/smart-wallet-relay/${this.hubChainData.hubNetworkSlug}/${result.hash}`);
this.clearForm();
})
.catch((error) => {
window.alert(error);
});
},
clearForm() {
this.form.token = '';
this.form.amount = '';
this.form.address = '';
// this.$v.$reset();
},
},
};
</script>
<template>
<form @submit.prevent="submit()">
<div class="form-row">
Your address: {{ smartWalletAddress }}
</div>
<div class="h-field form-row">
<div class="h-field__content">
<div class="h-field__title">Coin symbol or address of token contract to send (e.g. USDTBSC)</div>
<input class="h-field__input" type="text" v-model="form.token">
</div>
</div>
<div class="h-field form-row">
<div class="h-field__content">
<div class="h-field__title">Amount</div>
<input class="h-field__input" type="text" v-model="form.amount" inputmode="decimal">
</div>
</div>
<div class="form-row">
<FieldAddress
v-model.trim="form.address"
:label="$td('To the address', 'form.wallet-send-address')"
placeholder="0x"
/>
</div>
<div class="form-row">
<button class="button button--main button--full" type="submit">{{ $td('Send', 'form.wallet-send-button') }}</button>
</div>
</form>
</template>