Skip to content

Commit

Permalink
Updated plugin to work with WAX
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Apr 26, 2024
1 parent 1a4c574 commit 3789c00
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"prepare": "make"
},
"dependencies": {
"@wharfkit/resources": "^1.1.0",
"@wharfkit/resources": "^1.2.2",
"tslib": "^2.1.0"
},
"peerDependencies": {
Expand Down
10 changes: 4 additions & 6 deletions src/exception.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {API} from '@wharfkit/session'
import {APIError} from '@wharfkit/session'

export function getException(
response: API.v1.SendTransactionResponse
): API.v1.SendTransactionResponseException | null {
if (response.processed.except) {
return response.processed.except
export function getException(response): APIError | null {
if (response.error) {
return response.error
}
return null
}
97 changes: 56 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ const chains: Record<string, ChainConfig> = {
sampleAccount: 'eosmechanics',
symbol: Asset.Symbol.from('4,EOS'),
},
// WAX
'1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4': {
features: [ChainFeatures.BuyRAM, ChainFeatures.PowerUp],
sampleAccount: 'boost.wax',
symbol: Asset.Symbol.from('8,WAX'),
},
}

/** Multiply all resource purchases to provide extra based on inaccurate estimates */
Expand Down Expand Up @@ -180,45 +186,54 @@ export class TransactPluginAutoCorrect extends AbstractTransactPlugin {
const resolved = await context.resolve(request)

// Call compute_transaction against the resolved transaction to detect any issues.
const response = await context.client.v1.chain.compute_transaction(resolved.transaction)

// Extract any exceptions from the response
const exception = getException(response)
if (exception) {
switch (exception.name) {
case 'tx_net_usage_exceeded': {
const {net_usage} = exception.stack[0].data
const needed = net_usage * multiplier
if (config.features.includes(ChainFeatures.PowerUp)) {
return this.powerup(context, resolved, resources, 0, needed)
}
break
}
case 'tx_cpu_usage_exceeded': {
const {billed, billable} = exception.stack[0].data
const needed = (billed - billable) * multiplier
if (config.features.includes(ChainFeatures.PowerUp)) {
return this.powerup(context, resolved, resources, needed, 0)
}
break
}
case 'ram_usage_exceeded': {
const {available, needs} = exception.stack[0].data
const needed = (needs - available) * multiplier
if (config.features.includes(ChainFeatures.BuyRAM)) {
return this.buyram(context, resolved, resources, needed)
return context.client.v1.chain
.compute_transaction(resolved.transaction)
.then(() => {
return request
})
.catch((response) => {
// Extract any exceptions from the response
if (response.error) {
switch (response.error.name) {
case 'tx_net_usage_exceeded': {
const [, net_usage] = response.error.details[0].message.match(
/transaction net usage is too high: (\d+) > 0/
)
const needed = Number(net_usage) * multiplier
if (config.features.includes(ChainFeatures.PowerUp)) {
return this.powerup(context, resolved, resources, 0, needed)
}
break
}
case 'tx_cpu_usage_exceeded': {
const [, cpu_usage] = response.error.details[0].message.match(
/billed CPU time \((\d+) us\) is greater than the maximum billable CPU time for the transaction/
)
const needed = Number(cpu_usage) * multiplier
if (config.features.includes(ChainFeatures.PowerUp)) {
return this.powerup(context, resolved, resources, needed, 0)
}
break
}
case 'ram_usage_exceeded': {
const [, , needs, has] = response.error.details[0].message.match(
/account (\w.+) has insufficient ram; needs (\d+) bytes has (\d+) bytes/
)
const needed = (Number(needs) - Number(has)) * multiplier
if (config.features.includes(ChainFeatures.BuyRAM)) {
return this.buyram(context, resolved, resources, needed)
}
break
}
default: {
// no errors detected
break
}
}
break
}
default: {
// no errors detected
break
}
}
}

// Return the request
return request
// Return the request
return request
})
}
async buyram(
context: TransactContext,
Expand All @@ -234,7 +249,7 @@ export class TransactPluginAutoCorrect extends AbstractTransactPlugin {
}

// Determine price of resources
const price = Asset.fromUnits(ram.price_per(needed).value * 10000, config.symbol)
const price = Asset.from(ram.price_per(needed).value, config.symbol)

// Keep a running total of the price
if (this.price) {
Expand Down Expand Up @@ -287,8 +302,8 @@ export class TransactPluginAutoCorrect extends AbstractTransactPlugin {
}

// If powering up, always set a minimum to avoid API speed variance
if (cpu < 5000) {
cpu = 5000
if (cpu < 2500) {
cpu = 2500
}

if (net < 10000) {
Expand All @@ -298,7 +313,7 @@ export class TransactPluginAutoCorrect extends AbstractTransactPlugin {
// Determine price of resources
const price = Asset.from(
Number(powerup.cpu.price_per(this.sample, cpu)) +
Number(powerup.net.price_per(this.sample, net)),
Number(powerup.net.price_per(this.sample, net)) * multiplier,
config.symbol
)

Expand Down
14 changes: 10 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,14 @@
dependencies:
tslib "^2.1.0"

"@wharfkit/resources@^1.1.0":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@wharfkit/resources/-/resources-1.2.1.tgz#02cbc3b2a986d622154bb71820b0af60bf0de59d"
integrity sha512-K5GCvps3wHnGk7z5wtQqtrrpt5gz18YUTYkuPXa6fPY/zim4QLFED5uNe9eULJ2dwNAJPZ9quGD8TxKtaCazrA==
"@wharfkit/resources@^1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@wharfkit/resources/-/resources-1.2.2.tgz#5c58140745d013533739b1ee285cbd1031c3f063"
integrity sha512-WOsI5pIklx3rkz1OmUNISyYbG6cwaAOcHShRCeUrpEwWykjW91iEjslFtoW1+ZyQ0aJOUc9v6721jvS7gP3OPg==
dependencies:
"@wharfkit/antelope" "^1.0.0"
bn.js "^4.11.9"
js-big-decimal "^2.0.7"
tslib "^2.1.0"

"@wharfkit/session@^1.1.0-rcfinal":
Expand Down Expand Up @@ -1718,6 +1719,11 @@ jest-worker@^26.2.1:
merge-stream "^2.0.0"
supports-color "^7.0.0"

js-big-decimal@^2.0.7:
version "2.0.7"
resolved "https://registry.yarnpkg.com/js-big-decimal/-/js-big-decimal-2.0.7.tgz#fb9b44b4c1eae08903cb191c0cf37b82f3a8d7c4"
integrity sha512-XGc79t2Iv3b7LFlYaTT8WoQBuWL4K81aST+dq2YGHV6giedbnoG0s33ju24Uw/BGqLYfPPgn4HGRrPS2mfKk3Q==

js-sdsl@^4.1.4:
version "4.2.0"
resolved "https://registry.yarnpkg.com/js-sdsl/-/js-sdsl-4.2.0.tgz#278e98b7bea589b8baaf048c20aeb19eb7ad09d0"
Expand Down

0 comments on commit 3789c00

Please sign in to comment.