Skip to content

Commit

Permalink
feat(logic): add locked coins predicate implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Jan 19, 2023
1 parent 8e7251d commit 7a926c5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
51 changes: 51 additions & 0 deletions x/logic/predicate/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,54 @@ func BankSpendableCoins(vm *engine.VM, account, balances engine.Term, cont engin
return engine.Delay(promises...)
})
}

func BankLockedCoins(vm *engine.VM, account, balances engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise {
return engine.Delay(func(ctx context.Context) *engine.Promise {
sdkContext, err := util.UnwrapSDKContext(ctx)
if err != nil {
return engine.Error(err)
}
bankKeeper := sdkContext.Value(types.BankKeeperContextKey).(types.BankKeeper)

bech32Addr := sdk.AccAddress(nil)
switch acc := env.Resolve(account).(type) {
case engine.Variable:
case engine.Atom:
bech32Addr, err = sdk.AccAddressFromBech32(acc.String())
if err != nil {
return engine.Error(fmt.Errorf("bank_locked_coins/2: %w", err))
}
default:
return engine.Error(fmt.Errorf("bank_locked_coins/2: cannot unify account address with %T", acc))
}

if bech32Addr != nil {
fetchedBalances := LockedCoinsSorted(sdkContext, bankKeeper, bech32Addr)
return engine.Unify(vm, CoinsToTerm(fetchedBalances), balances, cont, env)
}

allBalances := bankKeeper.GetAccountsBalances(sdkContext)
promises := make([]func(ctx context.Context) *engine.Promise, 0, len(allBalances))
for _, balance := range allBalances {
address := balance.Address
bech32Addr, err = sdk.AccAddressFromBech32(address)
if err != nil {
return engine.Error(fmt.Errorf("bank_locked_coins/2: %w", err))
}
coins := LockedCoinsSorted(sdkContext, bankKeeper, bech32Addr)

promises = append(
promises,
func(ctx context.Context) *engine.Promise {
return engine.Unify(
vm,
Tuple(engine.NewAtom(address), CoinsToTerm(coins)),
Tuple(account, balances),
cont,
env,
)
})
}
return engine.Delay(promises...)
})
}
7 changes: 4 additions & 3 deletions x/logic/predicate/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,10 @@ func TestBank(t *testing.T) {
Coins: sdk.NewCoins(sdk.NewCoin("uatom", sdk.NewInt(7411))),
},
},
query: `bank_locked_coins(Accounts, SpendableCoins).`,
query: `bank_locked_coins(Accounts, LockedCoins).`,
wantResult: []types.TermResults{
{"Accounts": "okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm", "SpendableCoins": "[uknow-420]"},
{"Accounts": "okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38", "SpendableCoins": "[uatom-589]"},
{"Accounts": "okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm", "LockedCoins": "[uknow-800]"},
{"Accounts": "okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38", "LockedCoins": "[uatom-7411]"},
},
},
{
Expand Down Expand Up @@ -457,6 +457,7 @@ func TestBank(t *testing.T) {
interpreter := testutil.NewInterpreterMust(ctx)
interpreter.Register2(engine.NewAtom("bank_balances"), BankBalances)
interpreter.Register2(engine.NewAtom("bank_spendable_coins"), BankSpendableCoins)
interpreter.Register2(engine.NewAtom("bank_locked_coins"), BankLockedCoins)

err := interpreter.Compile(ctx, tc.program)
So(err, ShouldBeNil)
Expand Down
6 changes: 6 additions & 0 deletions x/logic/predicate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func SpendableCoinsSorted(sdkContext sdk.Context, bankKeeper types.BankKeeper, b
return fetchedBalances
}

// LockedCoinsSorted returns the list of spendable coins for the given address, sorted by coin denomination.
func LockedCoinsSorted(sdkContext sdk.Context, bankKeeper types.BankKeeper, bech32Addr sdk.AccAddress) sdk.Coins {
fetchedBalances := bankKeeper.LockedCoins(sdkContext, bech32Addr)
return BalancesSorted(fetchedBalances)
}

// CoinsToTerm converts the given coins to a term of the form:
//
// [-(Denom, Amount), -(Denom, Amount), ...]
Expand Down
2 changes: 1 addition & 1 deletion x/logic/testutil/expected_keepers_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a926c5

Please sign in to comment.