Skip to content

Commit

Permalink
fix: spark payback action wrapper (#313)
Browse files Browse the repository at this point in the history
Update Spark Payback action and action wrapper
  • Loading branch information
robercano authored May 28, 2024
1 parent 804bbe4 commit a7bbfbb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/core-contracts/contracts/actions/spark/Payback.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ contract SparkPayback is Executable, UseStore {

payback.amount = store().readUint(bytes32(payback.amount), paramsMap[1], address(this));

if (payback.onBehalf == address(0)) {
payback.onBehalf = address(this);
}

IPool(registry.getRegisteredService(SPARK_LENDING_POOL)).repay(
payback.asset,
payback.paybackAll ? type(uint256).max : payback.amount,
2,
address(this)
payback.onBehalf
);

store().write(bytes32(payback.amount));
Expand All @@ -42,4 +46,4 @@ contract SparkPayback is Executable, UseStore {
function parseInputs(bytes memory _callData) public pure returns (PaybackData memory params) {
return abi.decode(_callData, (PaybackData));
}
}
}
1 change: 1 addition & 0 deletions packages/core-contracts/contracts/core/types/Spark.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct PaybackData {
address asset;
uint256 amount;
bool paybackAll;
address onBehalf;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ActionCall, BaseAction, InputSlotsMapping } from '@summerfi/protocol-plugins-common'
import { IAddress } from '@summerfi/sdk-common'
import { ITokenAmount } from '@summerfi/sdk-common/common'

export class SparkPaybackAction extends BaseAction<typeof SparkPaybackAction.Config> {
public static readonly Config = {
name: 'SparkPayback',
version: 2,
parametersAbi: ['(address asset, uint256 amount, bool paybackAll)'],
parametersAbi: ['(address asset, uint256 amount, bool paybackAll, address onBehalf)'],
storageInputs: ['asset', 'amountToPayback'],
storageOutputs: ['paybackedAmount'],
} as const
Expand All @@ -14,6 +15,7 @@ export class SparkPaybackAction extends BaseAction<typeof SparkPaybackAction.Con
params: {
paybackAmount: ITokenAmount
paybackAll: boolean
onBehalf: IAddress
},
paramsMapping?: InputSlotsMapping,
): ActionCall {
Expand All @@ -23,6 +25,7 @@ export class SparkPaybackAction extends BaseAction<typeof SparkPaybackAction.Con
asset: params.paybackAmount.token.address.value,
amount: BigInt(params.paybackAmount.toBaseUnit()),
paybackAll: params.paybackAll,
onBehalf: params.onBehalf.value,
},
],
mapping: paramsMapping,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class SparkPaybackWithdrawActionBuilder extends BaseActionBuilder<steps.P
]

async build(params: ActionBuilderParams<steps.PaybackWithdrawStep>): Promise<void> {
const { context, step, addressBookManager, user } = params
const { context, step, addressBookManager, user, positionsManager } = params

if (!isSparkLendingPool(step.inputs.position.pool)) {
throw new Error('Invalid Spark lending pool')
Expand Down Expand Up @@ -57,6 +57,7 @@ export class SparkPaybackWithdrawActionBuilder extends BaseActionBuilder<steps.P
paybackAll: getValueFromReference(step.inputs.paybackAmount)
.toBN()
.gt(step.inputs.position.debtAmount.toBN()),
onBehalf: positionsManager.address,
},
connectedInputs: {},
connectedOutputs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('SparkPaybackAction Action', () => {
{
paybackAmount: tokenAmount,
paybackAll: true,
onBehalf: onBehalf,
},
[2, 6, 7, 9],
)
Expand All @@ -47,6 +48,7 @@ describe('SparkPaybackAction Action', () => {
asset: tokenAmount.token.address.value,
amount: BigInt(tokenAmount.toBaseUnit()),
paybackAll: true,
onBehalf: onBehalf.value,
},
])
expect(actionDecodedArgs?.mapping).toEqual([2, 6, 7, 9])
Expand Down

0 comments on commit a7bbfbb

Please sign in to comment.