Skip to content

Commit

Permalink
txn-builder: set-oracle-data (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
monstrobishi authored Jun 1, 2021
1 parent de2fa2a commit 6554eff
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { MasterNodeRegTestContainer, GenesisKeys } from '@defichain/testcontainers'
import { getProviders, MockProviders } from '../provider.mock'
import { P2WPKHTransactionBuilder } from '../../src'
import { calculateTxid, fundEllipticPair, sendTransaction } from '../test.utils'
import { WIF } from '@defichain/jellyfish-crypto'
import BigNumber from 'bignumber.js'

const container = new MasterNodeRegTestContainer()
let providers: MockProviders
let builder: P2WPKHTransactionBuilder

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()

providers = await getProviders(container)
providers.setEllipticPair(WIF.asEllipticPair(GenesisKeys[GenesisKeys.length - 1].owner.privKey))
builder = new P2WPKHTransactionBuilder(providers.fee, providers.prevout, providers.elliptic)

// Prep 1000 DFI Token for testing
await container.waitForWalletBalanceGTE(1001)
})

afterAll(async () => {
await container.stop()
})

describe('set oracle data', () => {
beforeEach(async () => {
await container.waitForWalletBalanceGTE(1)
})

it('should appoint and then set oracle data', async () => {
// Fund 10 DFI UTXO
await fundEllipticPair(container, providers.ellipticPair, 10)
await providers.setupMocks() // required to move utxos

// Appoint Oracle
const script = await providers.elliptic.script()
const appointTxn = await builder.oracles.appointOracle({
script: script,
weightage: 1,
priceFeeds: [
{
token: 'TEST',
currency: 'USD'
}
]
}, script)

const oracleId = calculateTxid(appointTxn)
await sendTransaction(container, appointTxn)

// Set Oracle Data
const setDataTxn = await builder.oracles.setOracleData({
oracleId: oracleId,
timestamp: new BigNumber('1621567932'),
tokens: [
{
token: 'TEST',
prices: [
{
currency: 'USD',
amount: new BigNumber('1.0')
}
]
}
]
}, script)

// Ensure the created txn is correct.
const outs = await sendTransaction(container, setDataTxn)
expect(outs[0].value).toStrictEqual(0)
expect(outs[1].value).toBeLessThan(10)
expect(outs[1].value).toBeGreaterThan(9.999)
expect(outs[1].scriptPubKey.addresses[0]).toStrictEqual(await providers.getAddress())

// Ensure you don't send all your balance away during set oracle data
const prevouts = await providers.prevout.all()
expect(prevouts.length).toStrictEqual(1)
expect(prevouts[0].value.toNumber()).toBeLessThan(10)
expect(prevouts[0].value.toNumber()).toBeGreaterThan(9.999)

// Ensure oracle is updated and has correct values
const getOracleDataResult = await container.call('getoracledata', [oracleId])
expect(getOracleDataResult.priceFeeds.length).toStrictEqual(1)
expect(getOracleDataResult.priceFeeds[0].token).toStrictEqual('TEST')
expect(getOracleDataResult.priceFeeds[0].currency).toStrictEqual('USD')
expect(getOracleDataResult.tokenPrices[0].token).toStrictEqual('TEST')
expect(getOracleDataResult.tokenPrices[0].currency).toStrictEqual('USD')
expect(getOracleDataResult.tokenPrices[0].amount).toStrictEqual(1.0)
expect(getOracleDataResult.tokenPrices[0].timestamp).toStrictEqual(1621567932)
})
})

// TODO(monstrobishi): test account state once RPC calls are in place
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppointOracle, RemoveOracle, UpdateOracle } from '@defichain/jellyfish-transaction/dist/script/defi/dftx_oracles'
import { AppointOracle, RemoveOracle, SetOracleData, UpdateOracle } from '@defichain/jellyfish-transaction/dist/script/defi/dftx_oracles'
import { OP_CODES, Script, TransactionSegWit } from '@defichain/jellyfish-transaction'
import { P2WPKHTxnBuilder } from './txn_builder'
import { TxnBuilderError, TxnBuilderErrorType } from './txn_builder_error'
Expand Down Expand Up @@ -59,4 +59,18 @@ export class TxnBuilderOracles extends P2WPKHTxnBuilder {
changeScript
)
}

/**
* Sets data on an oracle. Currently requires Foundation Authorization.
*
* @param {SetOracleData} setOracleData txn to create
* @param {Script} changeScript to send unspent to after deducting the (converted + fees)
* @returns {Promise<TransactionSegWit>}
*/
async setOracleData (setOracleData: SetOracleData, changeScript: Script): Promise<TransactionSegWit> {
return await super.createDeFiTx(
OP_CODES.OP_DEFI_TX_SET_ORACLE_DATA(setOracleData),
changeScript
)
}
}

0 comments on commit 6554eff

Please sign in to comment.