Skip to content

Commit

Permalink
Fix CrowdfundReplay. Use WoC for recunstructing tx chain since Bitail…
Browse files Browse the repository at this point in the history
…s is slow...
  • Loading branch information
msinkec committed Feb 9, 2024
1 parent a7b1a64 commit 5086c4e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
20 changes: 14 additions & 6 deletions tests/crowdfundReplay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
findSig,
bsv,
ContractId,
DefaultProvider,
TestWallet,
} from 'scrypt-ts'
import { CrowdfundReplay } from '../src/contracts/crowdfundReplay'
import { getDefaultSigner, randomPrivateKey } from './utils/helper'
import chaiAsPromised from 'chai-as-promised'
import { myPublicKey } from './utils/privateKey'
import { myAddress, myPrivateKey, myPublicKey } from './utils/privateKey'
import { getTransaction, replayToLatest } from './utils/replayHelper'
use(chaiAsPromised)

Expand All @@ -24,7 +26,7 @@ if (process.env.NETWORK === 'testnet') {
let contractId: ContractId

before(async () => {
await CrowdfundReplay.compile()
await CrowdfundReplay.loadArtifact()

const [privateKey1, publicKey1, ,] = randomPrivateKey()
const [privateKey2, publicKey2, ,] = randomPrivateKey()
Expand All @@ -36,11 +38,14 @@ if (process.env.NETWORK === 'testnet') {
BigInt(deadline),
10n
)

await instance.connect(
getDefaultSigner([privateKey1, privateKey2, privateKey3])
)

const deployTx = await instance.deploy()
console.log('Deployment:', deployTx.id)

contractId = {
txId: deployTx.id,
outputIndex: 0,
Expand All @@ -64,12 +69,13 @@ if (process.env.NETWORK === 'testnet') {
const pubKey = PubKey(toHex(donator))
const nextInstance = instance.next()
nextInstance.applyOffchainUpdatesForDonate(pubKey, amount)
await instance.methods.donate(pubKey, amount, {
const callRes = await instance.methods.donate(pubKey, amount, {
next: {
instance: nextInstance,
balance: instance.balance + Number(amount),
},
} as MethodCallOptions<CrowdfundReplay>)
console.log('Donate:', callRes.tx.id)
return nextInstance
}

Expand All @@ -79,25 +85,27 @@ if (process.env.NETWORK === 'testnet') {
amount: bigint
) {
const pubKey = PubKey(toHex(donator))
const { next } = await instance.methods.refund(
const callRes = await instance.methods.refund(
pubKey,
amount,
(sigResps) => findSig(sigResps, donator),
{
pubKeyOrAddrToSign: donator,
} as MethodCallOptions<CrowdfundReplay>
)
return next!.instance
console.log('Refund:', callRes.tx.id)
return callRes.next!.instance
}

async function collect(instance: CrowdfundReplay) {
await instance.methods.collect(
const callRes = await instance.methods.collect(
(sigResps) => findSig(sigResps, myPublicKey),
{
lockTime: deadline,
pubKeyOrAddrToSign: myPublicKey,
} as MethodCallOptions<CrowdfundReplay>
)
console.log('Collect:', callRes.tx.id)
}

it('should pass', async () => {
Expand Down
30 changes: 22 additions & 8 deletions tests/utils/replayHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,28 @@ export async function getCodeHash(outpoint: Outpoint): Promise<string> {
export async function getSpentIn(
outpoint: Outpoint
): Promise<SpentIn | undefined> {
const url = `https://test-api.bitails.io/tx/${outpoint.txId}/output/${outpoint.outputIndex}`
const data = await axios.get(url).then((r) => r.data)
return data.spent
? {
tx: await getTransaction(data.spentIn.txid),
atInputIndex: data.spentIn.inputIndex,
}
: undefined
const url = `https://api.whatsonchain.com/v1/bsv/test/tx/${outpoint.txId}/${outpoint.outputIndex}/spent`
try {
const response = await axios.get(url)
const data = response.data

if (data.txid) {
const tx = await getTransaction(data.txid) // Assuming getTransaction is another async function you've defined
return {
tx: tx,
atInputIndex: data.vin,
}
} else {
return undefined
}
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 404) {
// 404 returned if not spent...
return undefined
} else {
throw error
}
}
}

export async function getSpentChainItem(
Expand Down

0 comments on commit 5086c4e

Please sign in to comment.