@ape.swap/bonds-sdk 1.0.601 → 1.0.602

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -64,46 +64,53 @@ var BondModal = function (_a) {
64
64
  var _c = useWaitForTransactionReceipt({
65
65
  hash: hash,
66
66
  }), isConfirming = _c.isLoading, isConfirmed = _c.isSuccess;
67
- //const { account, isActive } = useWeb3React();
68
67
  var _d = useState([]), bondData = _d[0], setBondData = _d[1];
69
68
  //This is a temp solution, but we will force approval each time, and then once set we will set status here and allow
70
69
  //user to buy
71
70
  //Check with Fede if this needs to be temp, if we force approval everytime, isn't this fine?
72
71
  var handleBuy = function () { return __awaiter(void 0, void 0, void 0, function () {
73
- var billAddress, decimals, maxPrice, amount;
72
+ var billAddress, decimals, maxPrice, amount, tx, error_1;
74
73
  return __generator(this, function (_a) {
75
- billAddress = bondData[0].billAddress;
76
- decimals = 18;
77
- try {
78
- console.log('HANDLEBUY CLICKED');
79
- maxPrice = new BigNumber(bondData[0].trueBillPrice).times(102).div(100);
80
- amount = convertToTokenValue(inputValue, decimals);
81
- console.log('BillAddress:', billAddress);
82
- console.log('Amount:', amount);
83
- console.log('MaxPrice:', maxPrice);
84
- writeContract({
85
- address: billAddress,
86
- abi: BOND_ABI,
87
- functionName: 'deposit',
88
- args: [amount, maxPrice.toFixed(0), account],
89
- });
90
- console.log('BuyTransaction:', hash);
91
- if (hash) {
92
- setBuyTx(hash);
93
- console.log('setBuyTx:', hash);
94
- }
95
- }
96
- catch (error) {
97
- console.error('Buy error', error);
98
- alert('Buy Failed');
74
+ switch (_a.label) {
75
+ case 0:
76
+ billAddress = bondData[0].billAddress;
77
+ decimals = 18;
78
+ _a.label = 1;
79
+ case 1:
80
+ _a.trys.push([1, 3, , 4]);
81
+ console.log('HANDLEBUY CLICKED');
82
+ maxPrice = new BigNumber(bondData[0].trueBillPrice).times(102).div(100);
83
+ amount = convertToTokenValue(inputValue, decimals);
84
+ console.log('BillAddress:', billAddress);
85
+ console.log('Amount:', amount);
86
+ console.log('MaxPrice:', maxPrice);
87
+ return [4 /*yield*/, writeContractAsync({
88
+ address: billAddress,
89
+ abi: BOND_ABI,
90
+ functionName: 'deposit',
91
+ args: [amount, maxPrice.toFixed(0), account],
92
+ })];
93
+ case 2:
94
+ tx = _a.sent();
95
+ console.log('BuyTransaction:', hash);
96
+ if (tx) {
97
+ setBuyTx(tx);
98
+ console.log('setBuyTx:', hash);
99
+ }
100
+ return [3 /*break*/, 4];
101
+ case 3:
102
+ error_1 = _a.sent();
103
+ console.error('Buy error', error_1);
104
+ alert('Buy Failed');
105
+ return [3 /*break*/, 4];
106
+ case 4: return [2 /*return*/];
99
107
  }
100
- return [2 /*return*/];
101
108
  });
102
109
  }); };
103
110
  var _e = useState(null), buyTx = _e[0], setBuyTx = _e[1];
104
111
  //You approve the token, not the contract (this code will have to be updated for zap)
105
112
  var handleApprove = function () { return __awaiter(void 0, void 0, void 0, function () {
106
- var tokenAddress, decimals, amount, tx, error_1;
113
+ var tokenAddress, decimals, amount, tx, error_2;
107
114
  return __generator(this, function (_a) {
108
115
  switch (_a.label) {
109
116
  case 0:
@@ -129,8 +136,8 @@ var BondModal = function (_a) {
129
136
  }
130
137
  return [3 /*break*/, 4];
131
138
  case 3:
132
- error_1 = _a.sent();
133
- console.error('Approval failed:', error_1);
139
+ error_2 = _a.sent();
140
+ console.error('Approval failed:', error_2);
134
141
  alert('Approval Failed');
135
142
  return [3 /*break*/, 4];
136
143
  case 4: return [2 /*return*/];
@@ -175,36 +182,46 @@ var BondModal = function (_a) {
175
182
  setInputValue(event.target.value);
176
183
  };
177
184
  var _h = useState(null), tokenBalance = _h[0], setTokenBalance = _h[1];
185
+ var _j = useState(null), tokenDecimals = _j[0], setTokenDecimals = _j[1];
178
186
  useEffect(function () {
179
187
  if (isActive && account && bondData.length > 0) {
180
- getTokenBalance(bondData[0].chainId, bondData[0].principalToken, account).then(function (balance) {
181
- setTokenBalance(balance);
188
+ getTokenBalance(bondData[0].chainId, bondData[0].principalToken, account).then(function (_a) {
189
+ var balanceInEther = _a.balanceInEther, decimals = _a.decimals;
190
+ setTokenBalance(balanceInEther);
191
+ setTokenDecimals(decimals);
192
+ }).catch(function (error) {
193
+ console.error('Error fetching token balance', error);
182
194
  });
183
195
  }
184
196
  }, [isActive, account, bondData]);
185
197
  var getTokenBalance = function (chainId, tokenAddress, walletAddress) { return __awaiter(void 0, void 0, void 0, function () {
186
- var urls, provider, contract, balance, balanceInEther, error_2;
198
+ var urls, provider, contract, decimals, balance, balanceInEther, error_3;
187
199
  return __generator(this, function (_a) {
188
200
  switch (_a.label) {
189
201
  case 0:
190
- _a.trys.push([0, 2, , 3]);
202
+ _a.trys.push([0, 3, , 4]);
191
203
  urls = PUBLIC_RPC_URLS;
192
204
  provider = new ethers.providers.JsonRpcProvider(urls[chainId][0]);
193
205
  if (!provider) {
194
206
  throw new Error("No provider found for chainId: ".concat(chainId));
195
207
  }
196
208
  contract = new ethers.Contract(tokenAddress, ERC_20_ABI, provider);
197
- return [4 /*yield*/, contract.balanceOf(walletAddress)];
209
+ return [4 /*yield*/, contract.decimals()];
198
210
  case 1:
211
+ decimals = _a.sent();
212
+ console.log('Decimals:', decimals);
213
+ console.log(decimals);
214
+ return [4 /*yield*/, contract.balanceOf(walletAddress)];
215
+ case 2:
199
216
  balance = _a.sent();
200
- balanceInEther = ethers.utils.formatUnits(balance, 18);
217
+ balanceInEther = ethers.utils.formatUnits(balance, decimals);
201
218
  console.log("Token Balance: ".concat(balanceInEther));
202
- return [2 /*return*/, balanceInEther];
203
- case 2:
204
- error_2 = _a.sent();
205
- console.error('Error fetching token balance', error_2);
206
- return [2 /*return*/, 'Error fetching token balance'];
207
- case 3: return [2 /*return*/];
219
+ return [2 /*return*/, { balanceInEther: balanceInEther, decimals: decimals }];
220
+ case 3:
221
+ error_3 = _a.sent();
222
+ console.error('Error fetching token balance', error_3);
223
+ return [2 /*return*/, { balanceInEther: '0', decimals: 0 }]; // Return a default object
224
+ case 4: return [2 /*return*/];
208
225
  }
209
226
  });
210
227
  }); };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Ape Bond SDK",
4
4
  "author": "Ape Bond",
5
5
  "license": "MIT",
6
- "version": "1.0.601",
6
+ "version": "1.0.602",
7
7
  "main": "dist/index.js",
8
8
  "module": "dist/index.es.js",
9
9
  "types": "dist/index.d.ts",