Skip to content

Commit ec43670

Browse files
committed
fix pool contract deployment
1 parent d950822 commit ec43670

File tree

1 file changed

+71
-64
lines changed

1 file changed

+71
-64
lines changed

src/zilswap-v2/ZilSwapV2.ts

Lines changed: 71 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import { Transaction, TxReceipt as _TxReceipt, Wallet } from '@zilliqa-js/account'
1+
import { Transaction, TxReceipt as _TxReceipt, TxStatus as _TxStatus, Wallet } from '@zilliqa-js/account'
22
import { CallParams, Contract, Value } from '@zilliqa-js/contract'
3-
import { TransactionError } from '@zilliqa-js/core'
3+
import { Provider } from '@zilliqa-js/core'
44
import { fromBech32Address, toBech32Address } from '@zilliqa-js/crypto'
55
import { MessageType, NewEventSubscription, StatusType } from '@zilliqa-js/subscriptions'
66
import { BN, Long, units } from '@zilliqa-js/util'
77
import { Zilliqa } from '@zilliqa-js/zilliqa'
88
import { Mutex } from 'async-mutex'
99
import { BigNumber } from 'bignumber.js'
10-
import 'isomorphic-fetch'
1110

1211
import { BatchRequest, sendBatchRequest } from '../batch'
1312
import { APIS, BASIS, CHAIN_VERSIONS, Network, WSS, ZILSWAPV2_CONTRACTS, ZIL_HASH } from '../constants'
1413
import { isLocalStorageAvailable, toPositiveQa, unitlessBigNumber } from '../utils'
1514
import { OnStateUpdate, Zilo } from '../zilo'
1615
import { LONG_ALPHA, PRECISION, SHORT_ALPHA } from './utils'
1716

18-
import POOL_CODE from "./contracts/ZilSwapPool.scilla"
17+
import poolContractPath from "./contracts/ZilSwapPool.scilla"
1918

20-
declare module '*.scilla' {}
19+
declare module '*.scilla' { }
2120

2221
export { Network }
2322

@@ -291,46 +290,46 @@ export class ZilSwapV2 {
291290
* {@link https://www.investopedia.com/terms/b/basispoint.asp basis points}.
292291
* 10000 basis points = 100%
293292
*/
294-
public async deployAndAddPool(token0ID: string, token1ID: string, initAmpBps: string): Promise<(Contract | ObservedTx)[]> {
295-
// Check logged in
296-
this.checkAppLoadedWithUser()
293+
// public async deployAndAddPool(token0ID: string, token1ID: string, initAmpBps: string): Promise<(Contract | ObservedTx)[]> {
294+
// // Check logged in
295+
// this.checkAppLoadedWithUser()
297296

298-
let token0Hash = this.getHash(token0ID)
299-
let token1Hash = this.getHash(token1ID)
297+
// let token0Hash = this.getHash(token0ID)
298+
// let token1Hash = this.getHash(token1ID)
300299

301-
if (parseInt(token0Hash, 16) > parseInt(token1Hash, 16)) [token0Hash, token1Hash] = [token1Hash, token0Hash]
300+
// if (parseInt(token0Hash, 16) > parseInt(token1Hash, 16)) [token0Hash, token1Hash] = [token1Hash, token0Hash]
302301

303-
const token0Contract: Contract = this.getContract(token0Hash)
304-
const token1Contract: Contract = this.getContract(token1Hash)
302+
// const token0Contract: Contract = this.getContract(token0Hash)
303+
// const token1Contract: Contract = this.getContract(token1Hash)
305304

306-
const t0State = await this.fetchContractInit(token0Contract)
307-
const t1State = await this.fetchContractInit(token1Contract)
305+
// const t0State = await this.fetchContractInit(token0Contract)
306+
// const t1State = await this.fetchContractInit(token1Contract)
308307

309-
const pair = `${t0State.find((i: Value) => i.vname == 'symbol').value}-${t1State.find((i: Value) => i.vname == 'symbol').value}`
310-
const name = `ZilSwap V2 ${pair} LP Token`
311-
const symbol = `ZWAPv2LP.${pair}`
308+
// const pair = `${t0State.find((i: Value) => i.vname == 'symbol').value}-${t1State.find((i: Value) => i.vname == 'symbol').value}`
309+
// const name = `ZilSwap V2 ${pair} LP Token`
310+
// const symbol = `ZWAPv2LP.${pair}`
312311

313-
const init = [
314-
this.param('_scilla_version', 'Uint32', '0'),
315-
this.param('init_token0', 'ByStr20', token0Hash),
316-
this.param('init_token1', 'ByStr20', token1Hash),
317-
this.param('init_factory', 'ByStr20', this.contractHash),
318-
this.param('init_amp_bps', 'Uint128', initAmpBps),
319-
this.param('contract_owner', 'ByStr20', this.contractHash),
320-
this.param('name', 'String', name),
321-
this.param('symbol', 'String', symbol),
322-
this.param('decimals', 'Uint32', '12'),
323-
this.param('init_supply', 'Uint128', '0'),
324-
];
312+
// const init = [
313+
// this.param('_scilla_version', 'Uint32', '0'),
314+
// this.param('init_token0', 'ByStr20', token0Hash),
315+
// this.param('init_token1', 'ByStr20', token1Hash),
316+
// this.param('init_factory', 'ByStr20', this.contractHash),
317+
// this.param('init_amp_bps', 'Uint128', initAmpBps),
318+
// this.param('contract_owner', 'ByStr20', this.contractHash),
319+
// this.param('name', 'String', name),
320+
// this.param('symbol', 'String', symbol),
321+
// this.param('decimals', 'Uint32', '12'),
322+
// this.param('init_supply', 'Uint128', '0'),
323+
// ];
325324

326-
// Deploy pool
327-
const pool = await this.deployPoolContract(init)
325+
// // Deploy pool
326+
// const pool = await this.deployPoolContract(init)
328327

329-
// Add pool
330-
const tx = await this.addPool(pool.address!.toLowerCase())
328+
// // Add pool
329+
// const tx = await this.addPool(pool.address!.toLowerCase())
331330

332-
return [pool, tx]
333-
}
331+
// return [pool, tx]
332+
// }
334333

335334
/**
336335
* Deploys new pool contract only, without adding to the router.
@@ -344,7 +343,7 @@ export class ZilSwapV2 {
344343
* {@link https://www.investopedia.com/terms/b/basispoint.asp basis points}.
345344
* 10000 = 100%
346345
*/
347-
public async deployPool(token0ID: string, token1ID: string, initAmpBps: string): Promise<Contract> {
346+
public async deployPool(token0ID: string, token1ID: string, initAmpBps: string): Promise<ObservedTx> {
348347
// Check logged in
349348
this.checkAppLoadedWithUser()
350349

@@ -377,8 +376,7 @@ export class ZilSwapV2 {
377376
];
378377

379378
// Call deployContract
380-
const pool = await this.deployPoolContract(init)
381-
return pool
379+
return await this.deployPoolContract(init)
382380
}
383381

384382
/**
@@ -1730,40 +1728,49 @@ export class ZilSwapV2 {
17301728
private async deployPoolContract(init: Value[]) {
17311729
console.log("Deploying ZilSwapV2Pool...")
17321730
console.log(init)
1733-
const contract = this.zilliqa.contracts.new(POOL_CODE, init)
1734-
const [deployTx, state] = await contract.deployWithoutConfirm(this._txParams, false)
1735-
1736-
// Check for txn acceptance
1737-
if (!deployTx.id) {
1738-
throw new Error(JSON.stringify(state.error || 'Failed to get tx id!', null, 2))
1739-
}
1740-
console.info(`Deployment transaction id: ${deployTx.id}`)
1731+
const code = await fetch(poolContractPath).then(r => r.text());
1732+
const { result: minGasPrice } = await this.zilliqa.blockchain.getMinimumGasPrice();
1733+
const contract = (this.walletProvider || this.zilliqa).contracts.new(code, init)
1734+
const transaction = new Transaction({
1735+
version: this._txParams.version,
1736+
toAddr: ZIL_HASH,
1737+
data: JSON.stringify(init),
1738+
code,
1739+
amount: units.toQa(0, units.Units.Zil),
1740+
gasPrice: new BN(minGasPrice!),
1741+
gasLimit: Long.fromNumber(80000),
1742+
},
1743+
(this.walletProvider?.provider ?? this.zilliqa.provider) as Provider,
1744+
_TxStatus.Initialised,
1745+
false,
1746+
false,
1747+
);
17411748

1742-
const confirmedTx = await deployTx.confirm(deployTx.id, 50, 1000);
1749+
const deadline = this.deadlineBlock()
17431750

1744-
// Check for txn execution success
1745-
if (!confirmedTx.txParams.receipt!.success) {
1746-
const errors = confirmedTx.txParams.receipt!.errors || {}
1747-
const errMsgs = JSON.stringify(
1748-
Object.keys(errors).reduce((acc, depth) => {
1749-
const errorMsgList = errors[depth].map((num: any) => TransactionError[num])
1750-
return { ...acc, [depth]: errorMsgList }
1751-
}, {}))
1752-
const error = `Failed to deploy contract\n${errMsgs}`
1753-
throw new Error(error)
1751+
let tx: any
1752+
if (this.walletProvider) {
1753+
// ugly hack for zilpay provider
1754+
tx = await this.walletProvider?.blockchain.createTransaction(transaction);
1755+
tx.id = tx.ID
1756+
tx.isRejected = function (this: { errors: any[]; exceptions: any[] }) {
1757+
return this.errors.length > 0 || this.exceptions.length > 0
1758+
}
1759+
} else {
1760+
tx = await this.zilliqa.blockchain.createTransactionWithoutConfirm(transaction);
17541761
}
17551762

1756-
// Add to observedTx
1763+
console.log("obv tx", tx)
17571764
const observeTxn = {
1758-
hash: confirmedTx.id!,
1759-
deadline: this.deadlineBlock(),
1765+
hash: tx.id!,
1766+
deadline,
17601767
}
17611768
await this.observeTx(observeTxn)
17621769

1763-
console.log(`The contract address is: ${state.address!}`)
1770+
// const { result: address } = await this.zilliqa.blockchain.getContractAddressFromTransactionID(observeTxn.hash);
1771+
// console.log(`The contract address is: ${address!}`)
17641772

1765-
const deployedContract = this.getContract(state.address!)
1766-
return deployedContract
1773+
return observeTxn;
17671774
}
17681775

17691776
public async callContract(

0 commit comments

Comments
 (0)