@ape.swap/bonds-sdk 1.0.600 → 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.
@@ -60,50 +60,57 @@ var BondModal = function (_a) {
60
60
  var account = _a.account, isActive = _a.isActive, isOpen = _a.isOpen, accountChainId = _a.accountChainId, onClose = _a.onClose, bondAddress = _a.bondAddress;
61
61
  if (!isOpen)
62
62
  return null;
63
- var _b = useWriteContract(), hash = _b.data, isPending = _b.isPending, writeContract = _b.writeContract;
63
+ var _b = useWriteContract(), hash = _b.data, isPending = _b.isPending, writeContract = _b.writeContract, writeContractAsync = _b.writeContractAsync;
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(103).div(100);
80
- amount = convertToTokenValue(inputValue, decimals);
81
- console.log('BillAddress:', billAddress);
82
- console.log('Amount:', amount);
83
- console.log('MaxPrice:', maxPrice.toFixed(0));
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, hash_1, 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:
@@ -113,25 +120,24 @@ var BondModal = function (_a) {
113
120
  case 1:
114
121
  _a.trys.push([1, 3, , 4]);
115
122
  amount = convertToTokenValue(inputValue, decimals);
116
- return [4 /*yield*/, writeContract({
123
+ console.log('Amount:', amount);
124
+ return [4 /*yield*/, writeContractAsync({
117
125
  address: tokenAddress,
118
126
  abi: ERC_20_ABI,
119
127
  functionName: 'approve',
120
128
  args: [account, amount]
121
129
  })];
122
130
  case 2:
123
- hash_1 = _a.sent();
124
- alert(hash_1);
125
- console.log(hash_1);
126
- console.log('ApproveTransaction Hash__!:', hash_1);
127
- if (hash_1 != null) {
128
- setApproveTx(hash_1);
129
- console.log('setApproveTx:', hash_1);
131
+ tx = _a.sent();
132
+ console.log('ApproveTransaction:', tx);
133
+ if (tx) {
134
+ setApproveTx(tx);
135
+ console.log('setApproveTx:', tx);
130
136
  }
131
137
  return [3 /*break*/, 4];
132
138
  case 3:
133
- error_1 = _a.sent();
134
- console.error('Approval failed:', error_1);
139
+ error_2 = _a.sent();
140
+ console.error('Approval failed:', error_2);
135
141
  alert('Approval Failed');
136
142
  return [3 /*break*/, 4];
137
143
  case 4: return [2 /*return*/];
@@ -176,36 +182,46 @@ var BondModal = function (_a) {
176
182
  setInputValue(event.target.value);
177
183
  };
178
184
  var _h = useState(null), tokenBalance = _h[0], setTokenBalance = _h[1];
185
+ var _j = useState(null), tokenDecimals = _j[0], setTokenDecimals = _j[1];
179
186
  useEffect(function () {
180
187
  if (isActive && account && bondData.length > 0) {
181
- getTokenBalance(bondData[0].chainId, bondData[0].principalToken, account).then(function (balance) {
182
- 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);
183
194
  });
184
195
  }
185
196
  }, [isActive, account, bondData]);
186
197
  var getTokenBalance = function (chainId, tokenAddress, walletAddress) { return __awaiter(void 0, void 0, void 0, function () {
187
- var urls, provider, contract, balance, balanceInEther, error_2;
198
+ var urls, provider, contract, decimals, balance, balanceInEther, error_3;
188
199
  return __generator(this, function (_a) {
189
200
  switch (_a.label) {
190
201
  case 0:
191
- _a.trys.push([0, 2, , 3]);
202
+ _a.trys.push([0, 3, , 4]);
192
203
  urls = PUBLIC_RPC_URLS;
193
204
  provider = new ethers.providers.JsonRpcProvider(urls[chainId][0]);
194
205
  if (!provider) {
195
206
  throw new Error("No provider found for chainId: ".concat(chainId));
196
207
  }
197
208
  contract = new ethers.Contract(tokenAddress, ERC_20_ABI, provider);
198
- return [4 /*yield*/, contract.balanceOf(walletAddress)];
209
+ return [4 /*yield*/, contract.decimals()];
199
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:
200
216
  balance = _a.sent();
201
- balanceInEther = ethers.utils.formatUnits(balance, 18);
217
+ balanceInEther = ethers.utils.formatUnits(balance, decimals);
202
218
  console.log("Token Balance: ".concat(balanceInEther));
203
- return [2 /*return*/, balanceInEther];
204
- case 2:
205
- error_2 = _a.sent();
206
- console.error('Error fetching token balance', error_2);
207
- return [2 /*return*/, 'Error fetching token balance'];
208
- 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*/];
209
225
  }
210
226
  });
211
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.600",
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",